ruby-i18n / i18n

Internationalization (i18n) library for Ruby
MIT License
977 stars 408 forks source link

[BUG] config.i18n.fallbacks not respected on Rails 7 #645

Closed yheuhtozr closed 1 year ago

yheuhtozr commented 1 year ago

What I tried to do

class Application < Rails::Application
  config.load_defaults 7.0
  config.i18n.fallbacks = { "en-AU": %i[en-GB en-US en] }
end

What I expected to happen

$ rails c
Loading development environment (Rails 7.0.4)          
irb(main):001:0> I18n.fallbacks
=> {:"en-AU"=>[:"en-GB", :"en-US", :en]}

What actually happened

$ rails c
Loading development environment (Rails 7.0.4)          
irb(main):001:0> I18n.fallbacks
=> {:en=>[:en]}

Versions of i18n, rails, and anything else you think is necessary

Workaround

https://github.com/ruby-i18n/i18n/blob/fbc9b2b502ec373519d45a7b6da4ddc9243404c4/lib/i18n/locale/fallbacks.rb#L67-L80

It seems to work as expected[^1] if you add one line between the line 76-77 as follows:

        if args.count == 1 && !block_given?
          mappings = args.first
          mappings.each do |from, to|
            from, to = from.to_sym, Array(to)
            to.each do |_to|
              @map[from] ||= []
              @map[from] << _to.to_sym
            end
          end
          self.replace(@map) # <- insert this!
        else
          @map.map(*args, &block)
        end

[^1]: not exactly, it'll be like {:"en-AU"=>[:"en-GB", :"en-US", :en], :en=>[:en]}

radar commented 1 year ago

Notably, this happens in Rails 6.1 and Rails 6.0 as well.

guillaumebriday-pa commented 1 year ago

I got the same issue, do you know how to solve it?

It only happened with Rails 7

nflorentin commented 6 months ago

The issue is still present. I wonder why the MR was reverted.

Fallbacks are impossible to configure with ruby-i18n 1.14.1 (tested on rails 7.1.3).

Here is a reproducible script:

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  git_source(:github) { |repo| "https://github.com/#{repo}.git" }

  gem "rails", "~> 7.1"
  gem "i18n", "1.14.1"
end

require "active_support/railtie"
require "minitest/autorun"

class TestApp < Rails::Application
  config.load_defaults 7.1

  config.eager_load = false

  config.i18n.default_locale = :en
  config.i18n.available_locales = [:'en-GB', :'en-UK', :'en-US', :en]

  config.i18n.fallbacks = { :'en-GB' => [:en], :'en-UK' => [:en], :'en-US' => [:en] }

  # not working either
  # config.i18n.fallbacks = [:'en-US', :en]
  # config.i18n.fallbacks.map = { :'en-GB' => [:en], :'en-UK' => [:en], :'en-US' => [:en] }

  # makes the test pass
  # I18n.fallbacks = { :'en-GB' => [:en], :'en-UK' => [:en], :'en-US' => [:en], :en => [:en] }
end

Rails.application.initialize!

class I18nFallbacksTest < ActiveSupport::TestCase
  test "fallbacks" do
    assert_not_empty I18n.fallbacks
  end
end