Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

I18n, look up a translation with the default locale when it's missed with another specific locale

#2637

I've added an I18n option to allow to look up a translation with the default locale when it's missed with another specific locale.

This is a useful option if you don't want your page will crash when misses a translation and you don't want to use the default option in each translation. It's inspired by the gettext behaviour.

For example, I have this code in an initializer:


  I18n.use_default_locale_on_missing_translation = true
  I18n.default_locale = :en

And two translation files, en.yml and es.yml:


  en:
    hello: 'hello'
    hello_world: 'hello world'
 
  es:
    hello_world: 'hola mundo'

When I execute this code:


  I18n.t :hello, :locale => :es

Rails returns hello instead of send an error

Reported by calavera · May 12th, 2009 @ 04:13 PM

State: wontfix
Milestone: 2.x
Assigned to: Sven Fuchs Sven Fuchs
Importance: Low

Activity

  1. Pedro Pimentel (zukunftsalick)
    Pedro Pimentel (zukunftsalick)

    We were just looking for this kind of solution few days ago. For us is better to display english translation than displaying a missing key text.

    great

    May 12th, 2009 @ 04:24 PM

  2. Luismi Cavallé
    Luismi Cavallé

    Globalize2 also has a very good solution for this problem: http://wiki.github.com/joshmh/gl...

    May 12th, 2009 @ 05:15 PM

  3. Luismi Cavallé
    Luismi Cavallé

    Globalize2 also has a very good solution to this problem: http://wiki.github.com/joshmh/gl...

    May 12th, 2009 @ 05:15 PM

  4. Raul Murciano
    Raul Murciano

    +1, nice patch! My users will also prefer an english text better than a "missing key" warning.

    May 12th, 2009 @ 05:24 PM

  5. Cheah Chu Yeow
    Cheah Chu Yeow
    • Title changed from [PATCH] I18n, look up a translation with the default locale when it's missed with another specific locale to I18n, look up a translation with the default locale when it's missed with another specific locale
    1. All tests pass after applying your patch and the code in the patch looks good!

    This will be very awesome, I was just working on I18n not just yesterday and would have appreciated this a lot for production code.

    May 12th, 2009 @ 05:37 PM

  6. Cheah Chu Yeow
    Cheah Chu Yeow

    Ugh I meant +1 in my last comment. I wonder how that got interpreted as a new ordered list item!

    May 12th, 2009 @ 05:39 PM

  7. Manu Campos
  8. Cheah Chu Yeow
    Cheah Chu Yeow
    • Tag changed from active_support, documentation, i18n, improvement, patch, test to active_support, documentation, i18n, improvement, patch, test, verified

    May 12th, 2009 @ 06:12 PM

  9. José Valim
    José Valim

    We already had this discussion on the I18n group when we decided to not add this functionality. But since it was implemented as an option, +1 for me.

    The patch works here, but anyway I think it should be sent to the Rails I18n tracker:

    http://i18n.lighthouseapp.com/projects/14948-rails-i18n/tickets?q=all

    It would be nice to check if it works on Rails setup as well (it probably works by default):

    config.i18n.use_default_locale_on_missing_translation = true
    

    May 13th, 2009 @ 08:03 PM

  10. calavera
    calavera

    Yep, I didn't know there were a separate project. I'm sending it to the I18n tracker as well.

    It already works on Rails setup by default, this is the code that initialize the I18n options:

    @@@ruby configuration.i18n.each do |setting, value|

        if setting == :load_path
          I18n.load_path += value
        else
          I18n.send("#{setting}=", value)
        end
    

    end

    
    

    May 13th, 2009 @ 09:16 PM

  11. sam
    sam

    Nice Job Calavera!

    Incidentally, this is one of the main reasons I still use Gettext, it allows me to concentrate on the main language and add translations progressively.

    Cheers, sam

    May 14th, 2009 @ 02:48 PM

  12. Claudio Poli
    Claudio Poli

    Would love to see it implemented, as an option is the way to go.

    May 15th, 2009 @ 12:18 PM

  13. Emili Parreño
    Emili Parreño

    +1 nice! I like this patch, very usefull.

    May 18th, 2009 @ 08:38 AM

  14. Sven Fuchs
    Sven Fuchs
    • State changed from new to wontfix
    • Assigned user set to Sven Fuchs

    Clearly, -1 on this patch.

    For one thing it changes the shipped I18n gem so any changes should obviously be applied over there.

    Also, fallbacks can be a hairy issue. If hardcoding anything you'd at least want to provide support for falling back from, e.g., de-DE to de, but then quickly get into various special cases (e.g. Scandinavian locales). So in the end you'll want to let the user customize fallback rules.

    Globalize2 ships support for this and you can use the fallback stuff separately from the rest of the library. If Globalize2's fallback support is not good enough I'd suggest either improving it or implementing your needs as a different plugin.

    All that said, of course I can see we'll want to provide this kind of support (as an optional module) in the I18n gem. But I'd want to proceed on our conservative route of cherry-picking proven things from plugins or other battle-tested solutions.

    July 8th, 2009 @ 07:26 PM

  15. Francesc Esplugas
    Francesc Esplugas

    Sorry Sven but I don't agree on deciding that this wont be fixed.

    Having a fallback language it's something Gettext has been doing for ages and I18n should do the same by default, in fact there's a lot of people who expects this behavior.

    Meanwhile I've extended the object class to achieve the behavior I wanted to have.

    class Object
    
      def _(msg, *args)
        options = args.extract_options!
        options[:default] = msg
        I18n.t(msg, options)
      end
    
    end
    

    July 8th, 2009 @ 11:42 PM

  16. Sven Fuchs
    Sven Fuchs

    Francesc, I'm sure a lot of people use fallbacks. That doesn't mean it must be included to Rails though right now though. Rails I18n was built to support no other locale than :en out of the box. The reason for this is that it can be incredibly hard to figure out "the right way" to do it - and Gettext is not always a good example.

    Please keep in mind that experimenting with advanced features in plugin-land, then comparing solutions, maybe cherrypicking a few things from the best breeds and then including them into the I18n lib in a defensive way is part of the plan :)

    Does Globalize2 fallback support solve your problem? If so we should better document (e.g. in the I18n guide) that it's there and how it can be used. If not, please provide feedback so it can be improved.

    July 8th, 2009 @ 11:54 PM

  17. Francesc Esplugas
    Francesc Esplugas

    In my opinion falling back to the message user has provided if no translation available it's a good solution, this makes me think that although my application is in english I always have to translate it creating locale files. In my opinion:

    t("Hello World") should become "Hello World" if no translation available.

    Users don't have to see those "missing translation" & "en, Hello World" warnings.

    Gettext maybe it's not a good example, but makes sense to return the same message if no translation is available. (Other thing is how it's implemented)

    (I must say I understand it's hard to figure the right way to do it.)

    July 9th, 2009 @ 08:03 AM

  18. Ryan Bigg
    Ryan Bigg
    • Tag cleared.
    • Importance changed from to Low

    Automatic cleanup of spam.

    October 9th, 2010 @ 09:44 PM

  19. bingbing