Rails 5 I18n default_locale and fallback locale












7















How do I set a fallback locale in Rails 5.2?



Right now it prints ''(blank) for attribute names if not found in locale. I would like it to fallback to :en when that happens.



Rails 5 specific?



# config/application.rb 
config.load_defaults 5.0
config.i18n.default_locale = :nb


run specs and get:



#<ActiveRecord::RecordInvalid: Det oppstod en feil: er allerede i bruk>
see the missing part here:
#<ActiveRecord::RecordInvalid: Det oppstod en feil: [MISSING RECORD NAME] er allerede i bruk>

# switch to
# config.i18n.default_locale = :en
#<ActiveRecord::RecordInvalid: Validation failed: Report date has already been taken>


UPDATE:



@Nate got the fallback config correct in his answer below (https://stackoverflow.com/a/53453211/252799)



That wasn't the issue though. The problem, I learned, was in my nb.yml.





  • Experiment 1.
    Deleting the entire content of nb.yml, leaving just hello world. Works: It prints the attribute (associated record) name.


  • Experiment 2.
    Replacing our nb.yml with the official Rails nb.yml: https://github.com/svenfuchs/rails-i18n/blob/master/rails/locale/nb.yml : Works. I get the helpful norwegian-english message Det oppstod feil: Company må eksistere. Not pretty, but helpful and what I wanted!


  • Experiment 3. Proof. The nb.yml reproduces the problem.


Two approaches



# A: SHOW attribute name
nb:
errors:
format: "%{attribute} %{message}"

# B: HIDE attribute name
nb:
errors:
format: "%{message}"


Which leads to another problem.



Approach B: HIDE makes you blind serverside. the specs, logs and console will show Validation Failed: can't be blank but it looks good in UI.



I posted another Q: Simpleform errors without attribute name, but using attribute name in logs, console, specs










share|improve this question





























    7















    How do I set a fallback locale in Rails 5.2?



    Right now it prints ''(blank) for attribute names if not found in locale. I would like it to fallback to :en when that happens.



    Rails 5 specific?



    # config/application.rb 
    config.load_defaults 5.0
    config.i18n.default_locale = :nb


    run specs and get:



    #<ActiveRecord::RecordInvalid: Det oppstod en feil: er allerede i bruk>
    see the missing part here:
    #<ActiveRecord::RecordInvalid: Det oppstod en feil: [MISSING RECORD NAME] er allerede i bruk>

    # switch to
    # config.i18n.default_locale = :en
    #<ActiveRecord::RecordInvalid: Validation failed: Report date has already been taken>


    UPDATE:



    @Nate got the fallback config correct in his answer below (https://stackoverflow.com/a/53453211/252799)



    That wasn't the issue though. The problem, I learned, was in my nb.yml.





    • Experiment 1.
      Deleting the entire content of nb.yml, leaving just hello world. Works: It prints the attribute (associated record) name.


    • Experiment 2.
      Replacing our nb.yml with the official Rails nb.yml: https://github.com/svenfuchs/rails-i18n/blob/master/rails/locale/nb.yml : Works. I get the helpful norwegian-english message Det oppstod feil: Company må eksistere. Not pretty, but helpful and what I wanted!


    • Experiment 3. Proof. The nb.yml reproduces the problem.


    Two approaches



    # A: SHOW attribute name
    nb:
    errors:
    format: "%{attribute} %{message}"

    # B: HIDE attribute name
    nb:
    errors:
    format: "%{message}"


    Which leads to another problem.



    Approach B: HIDE makes you blind serverside. the specs, logs and console will show Validation Failed: can't be blank but it looks good in UI.



    I posted another Q: Simpleform errors without attribute name, but using attribute name in logs, console, specs










    share|improve this question



























      7












      7








      7








      How do I set a fallback locale in Rails 5.2?



      Right now it prints ''(blank) for attribute names if not found in locale. I would like it to fallback to :en when that happens.



      Rails 5 specific?



      # config/application.rb 
      config.load_defaults 5.0
      config.i18n.default_locale = :nb


      run specs and get:



      #<ActiveRecord::RecordInvalid: Det oppstod en feil: er allerede i bruk>
      see the missing part here:
      #<ActiveRecord::RecordInvalid: Det oppstod en feil: [MISSING RECORD NAME] er allerede i bruk>

      # switch to
      # config.i18n.default_locale = :en
      #<ActiveRecord::RecordInvalid: Validation failed: Report date has already been taken>


      UPDATE:



      @Nate got the fallback config correct in his answer below (https://stackoverflow.com/a/53453211/252799)



      That wasn't the issue though. The problem, I learned, was in my nb.yml.





      • Experiment 1.
        Deleting the entire content of nb.yml, leaving just hello world. Works: It prints the attribute (associated record) name.


      • Experiment 2.
        Replacing our nb.yml with the official Rails nb.yml: https://github.com/svenfuchs/rails-i18n/blob/master/rails/locale/nb.yml : Works. I get the helpful norwegian-english message Det oppstod feil: Company må eksistere. Not pretty, but helpful and what I wanted!


      • Experiment 3. Proof. The nb.yml reproduces the problem.


      Two approaches



      # A: SHOW attribute name
      nb:
      errors:
      format: "%{attribute} %{message}"

      # B: HIDE attribute name
      nb:
      errors:
      format: "%{message}"


      Which leads to another problem.



      Approach B: HIDE makes you blind serverside. the specs, logs and console will show Validation Failed: can't be blank but it looks good in UI.



      I posted another Q: Simpleform errors without attribute name, but using attribute name in logs, console, specs










      share|improve this question
















      How do I set a fallback locale in Rails 5.2?



      Right now it prints ''(blank) for attribute names if not found in locale. I would like it to fallback to :en when that happens.



      Rails 5 specific?



      # config/application.rb 
      config.load_defaults 5.0
      config.i18n.default_locale = :nb


      run specs and get:



      #<ActiveRecord::RecordInvalid: Det oppstod en feil: er allerede i bruk>
      see the missing part here:
      #<ActiveRecord::RecordInvalid: Det oppstod en feil: [MISSING RECORD NAME] er allerede i bruk>

      # switch to
      # config.i18n.default_locale = :en
      #<ActiveRecord::RecordInvalid: Validation failed: Report date has already been taken>


      UPDATE:



      @Nate got the fallback config correct in his answer below (https://stackoverflow.com/a/53453211/252799)



      That wasn't the issue though. The problem, I learned, was in my nb.yml.





      • Experiment 1.
        Deleting the entire content of nb.yml, leaving just hello world. Works: It prints the attribute (associated record) name.


      • Experiment 2.
        Replacing our nb.yml with the official Rails nb.yml: https://github.com/svenfuchs/rails-i18n/blob/master/rails/locale/nb.yml : Works. I get the helpful norwegian-english message Det oppstod feil: Company må eksistere. Not pretty, but helpful and what I wanted!


      • Experiment 3. Proof. The nb.yml reproduces the problem.


      Two approaches



      # A: SHOW attribute name
      nb:
      errors:
      format: "%{attribute} %{message}"

      # B: HIDE attribute name
      nb:
      errors:
      format: "%{message}"


      Which leads to another problem.



      Approach B: HIDE makes you blind serverside. the specs, logs and console will show Validation Failed: can't be blank but it looks good in UI.



      I posted another Q: Simpleform errors without attribute name, but using attribute name in logs, console, specs







      ruby-on-rails-5 rails-i18n






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 27 '18 at 13:26







      oma

















      asked Nov 20 '18 at 17:21









      omaoma

      13.3k105684




      13.3k105684
























          1 Answer
          1






          active

          oldest

          votes


















          5





          +500









          Rails 5.2 allows you to set config.i18n.fallbacks to control which fallback locale(s) are able to be used. Set it to an array of symbols to control which locales should be used when a translation is not found. When multiple are set, it goes through the array one by one until a transliterate works (or doesn't).



          In your case, you'll want the following in your config/application.rb:



          config.i18n.default_locale = :nb
          config.i18n.fallbacks = [:en]


          This way, if a locale is not specified, it uses the :nb locale (your default locale). Then, any time a translate fails in any locale, it will try the :en locale as a fallback.



          Note: if none of your fallback locales have a translation for the key you're trying to translate, you'll still get the translation missing error.



          For a more complete answer, here's an example with multiple fallbacks. They will be checked in the order specified:



          config.i18n.default_locale = :nb
          config.i18n.fallbacks = [:en, :es, :de]





          share|improve this answer


























          • bounty instructions "No guessing, show me it works.". You present it like guesswork, please provide authoritive answer. Thanks!

            – oma
            Nov 24 '18 at 13:13











          • @oma not sure if you saw, but I updated my response. I tried it out on Rails 5.2.0 as well.

            – Nate
            Nov 24 '18 at 18:54











          • Nate, it does in fact fallback to :en as you say. It didn't solve my problem though, I guess I am asking the wrong question, I made a mistake assuming what the problem was. I think I know what the problem is, it's us/me (as 99% of the time). Our nb.yml is the issue. A fresh Rails 5.2 app with the official nb.yml github.com/svenfuchs/rails-i18n/blob/master/rails/locale/nb.yml doesn't have the same problem. If I replace it with our nb.yml, the problem re-surface. I'll update my question, add info about what I learned and point to your answer for the fallback.

            – oma
            Nov 27 '18 at 7:41













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53398275%2frails-5-i18n-default-locale-and-fallback-locale%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          5





          +500









          Rails 5.2 allows you to set config.i18n.fallbacks to control which fallback locale(s) are able to be used. Set it to an array of symbols to control which locales should be used when a translation is not found. When multiple are set, it goes through the array one by one until a transliterate works (or doesn't).



          In your case, you'll want the following in your config/application.rb:



          config.i18n.default_locale = :nb
          config.i18n.fallbacks = [:en]


          This way, if a locale is not specified, it uses the :nb locale (your default locale). Then, any time a translate fails in any locale, it will try the :en locale as a fallback.



          Note: if none of your fallback locales have a translation for the key you're trying to translate, you'll still get the translation missing error.



          For a more complete answer, here's an example with multiple fallbacks. They will be checked in the order specified:



          config.i18n.default_locale = :nb
          config.i18n.fallbacks = [:en, :es, :de]





          share|improve this answer


























          • bounty instructions "No guessing, show me it works.". You present it like guesswork, please provide authoritive answer. Thanks!

            – oma
            Nov 24 '18 at 13:13











          • @oma not sure if you saw, but I updated my response. I tried it out on Rails 5.2.0 as well.

            – Nate
            Nov 24 '18 at 18:54











          • Nate, it does in fact fallback to :en as you say. It didn't solve my problem though, I guess I am asking the wrong question, I made a mistake assuming what the problem was. I think I know what the problem is, it's us/me (as 99% of the time). Our nb.yml is the issue. A fresh Rails 5.2 app with the official nb.yml github.com/svenfuchs/rails-i18n/blob/master/rails/locale/nb.yml doesn't have the same problem. If I replace it with our nb.yml, the problem re-surface. I'll update my question, add info about what I learned and point to your answer for the fallback.

            – oma
            Nov 27 '18 at 7:41


















          5





          +500









          Rails 5.2 allows you to set config.i18n.fallbacks to control which fallback locale(s) are able to be used. Set it to an array of symbols to control which locales should be used when a translation is not found. When multiple are set, it goes through the array one by one until a transliterate works (or doesn't).



          In your case, you'll want the following in your config/application.rb:



          config.i18n.default_locale = :nb
          config.i18n.fallbacks = [:en]


          This way, if a locale is not specified, it uses the :nb locale (your default locale). Then, any time a translate fails in any locale, it will try the :en locale as a fallback.



          Note: if none of your fallback locales have a translation for the key you're trying to translate, you'll still get the translation missing error.



          For a more complete answer, here's an example with multiple fallbacks. They will be checked in the order specified:



          config.i18n.default_locale = :nb
          config.i18n.fallbacks = [:en, :es, :de]





          share|improve this answer


























          • bounty instructions "No guessing, show me it works.". You present it like guesswork, please provide authoritive answer. Thanks!

            – oma
            Nov 24 '18 at 13:13











          • @oma not sure if you saw, but I updated my response. I tried it out on Rails 5.2.0 as well.

            – Nate
            Nov 24 '18 at 18:54











          • Nate, it does in fact fallback to :en as you say. It didn't solve my problem though, I guess I am asking the wrong question, I made a mistake assuming what the problem was. I think I know what the problem is, it's us/me (as 99% of the time). Our nb.yml is the issue. A fresh Rails 5.2 app with the official nb.yml github.com/svenfuchs/rails-i18n/blob/master/rails/locale/nb.yml doesn't have the same problem. If I replace it with our nb.yml, the problem re-surface. I'll update my question, add info about what I learned and point to your answer for the fallback.

            – oma
            Nov 27 '18 at 7:41
















          5





          +500







          5





          +500



          5




          +500





          Rails 5.2 allows you to set config.i18n.fallbacks to control which fallback locale(s) are able to be used. Set it to an array of symbols to control which locales should be used when a translation is not found. When multiple are set, it goes through the array one by one until a transliterate works (or doesn't).



          In your case, you'll want the following in your config/application.rb:



          config.i18n.default_locale = :nb
          config.i18n.fallbacks = [:en]


          This way, if a locale is not specified, it uses the :nb locale (your default locale). Then, any time a translate fails in any locale, it will try the :en locale as a fallback.



          Note: if none of your fallback locales have a translation for the key you're trying to translate, you'll still get the translation missing error.



          For a more complete answer, here's an example with multiple fallbacks. They will be checked in the order specified:



          config.i18n.default_locale = :nb
          config.i18n.fallbacks = [:en, :es, :de]





          share|improve this answer















          Rails 5.2 allows you to set config.i18n.fallbacks to control which fallback locale(s) are able to be used. Set it to an array of symbols to control which locales should be used when a translation is not found. When multiple are set, it goes through the array one by one until a transliterate works (or doesn't).



          In your case, you'll want the following in your config/application.rb:



          config.i18n.default_locale = :nb
          config.i18n.fallbacks = [:en]


          This way, if a locale is not specified, it uses the :nb locale (your default locale). Then, any time a translate fails in any locale, it will try the :en locale as a fallback.



          Note: if none of your fallback locales have a translation for the key you're trying to translate, you'll still get the translation missing error.



          For a more complete answer, here's an example with multiple fallbacks. They will be checked in the order specified:



          config.i18n.default_locale = :nb
          config.i18n.fallbacks = [:en, :es, :de]






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 24 '18 at 17:09

























          answered Nov 23 '18 at 21:39









          NateNate

          1,281211




          1,281211













          • bounty instructions "No guessing, show me it works.". You present it like guesswork, please provide authoritive answer. Thanks!

            – oma
            Nov 24 '18 at 13:13











          • @oma not sure if you saw, but I updated my response. I tried it out on Rails 5.2.0 as well.

            – Nate
            Nov 24 '18 at 18:54











          • Nate, it does in fact fallback to :en as you say. It didn't solve my problem though, I guess I am asking the wrong question, I made a mistake assuming what the problem was. I think I know what the problem is, it's us/me (as 99% of the time). Our nb.yml is the issue. A fresh Rails 5.2 app with the official nb.yml github.com/svenfuchs/rails-i18n/blob/master/rails/locale/nb.yml doesn't have the same problem. If I replace it with our nb.yml, the problem re-surface. I'll update my question, add info about what I learned and point to your answer for the fallback.

            – oma
            Nov 27 '18 at 7:41





















          • bounty instructions "No guessing, show me it works.". You present it like guesswork, please provide authoritive answer. Thanks!

            – oma
            Nov 24 '18 at 13:13











          • @oma not sure if you saw, but I updated my response. I tried it out on Rails 5.2.0 as well.

            – Nate
            Nov 24 '18 at 18:54











          • Nate, it does in fact fallback to :en as you say. It didn't solve my problem though, I guess I am asking the wrong question, I made a mistake assuming what the problem was. I think I know what the problem is, it's us/me (as 99% of the time). Our nb.yml is the issue. A fresh Rails 5.2 app with the official nb.yml github.com/svenfuchs/rails-i18n/blob/master/rails/locale/nb.yml doesn't have the same problem. If I replace it with our nb.yml, the problem re-surface. I'll update my question, add info about what I learned and point to your answer for the fallback.

            – oma
            Nov 27 '18 at 7:41



















          bounty instructions "No guessing, show me it works.". You present it like guesswork, please provide authoritive answer. Thanks!

          – oma
          Nov 24 '18 at 13:13





          bounty instructions "No guessing, show me it works.". You present it like guesswork, please provide authoritive answer. Thanks!

          – oma
          Nov 24 '18 at 13:13













          @oma not sure if you saw, but I updated my response. I tried it out on Rails 5.2.0 as well.

          – Nate
          Nov 24 '18 at 18:54





          @oma not sure if you saw, but I updated my response. I tried it out on Rails 5.2.0 as well.

          – Nate
          Nov 24 '18 at 18:54













          Nate, it does in fact fallback to :en as you say. It didn't solve my problem though, I guess I am asking the wrong question, I made a mistake assuming what the problem was. I think I know what the problem is, it's us/me (as 99% of the time). Our nb.yml is the issue. A fresh Rails 5.2 app with the official nb.yml github.com/svenfuchs/rails-i18n/blob/master/rails/locale/nb.yml doesn't have the same problem. If I replace it with our nb.yml, the problem re-surface. I'll update my question, add info about what I learned and point to your answer for the fallback.

          – oma
          Nov 27 '18 at 7:41







          Nate, it does in fact fallback to :en as you say. It didn't solve my problem though, I guess I am asking the wrong question, I made a mistake assuming what the problem was. I think I know what the problem is, it's us/me (as 99% of the time). Our nb.yml is the issue. A fresh Rails 5.2 app with the official nb.yml github.com/svenfuchs/rails-i18n/blob/master/rails/locale/nb.yml doesn't have the same problem. If I replace it with our nb.yml, the problem re-surface. I'll update my question, add info about what I learned and point to your answer for the fallback.

          – oma
          Nov 27 '18 at 7:41






















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53398275%2frails-5-i18n-default-locale-and-fallback-locale%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to change which sound is reproduced for terminal bell?

          Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

          Can I use Tabulator js library in my java Spring + Thymeleaf project?