Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I18n localisation files do not support subdirectories. #598

Closed
lux-shaun opened this issue Jul 21, 2022 · 10 comments
Closed

I18n localisation files do not support subdirectories. #598

lux-shaun opened this issue Jul 21, 2022 · 10 comments
Milestone

Comments

@lux-shaun
Copy link
Contributor

Locale files have to be directly in src/_locales to work (e.g. src/_locales/en.yml), which limits the ability to organise multiple files.

It looks like changing the load path in localizable.rb from

Dir["#{in_source_dir("_locales")}/*.{json,rb,yml}"].each do |locale_path|
  I18n.load_path << locale_path
end

to

Dir["#{in_source_dir("_locales")}/**/*.{json,rb,yml}"].each do |locale_path|
  I18n.load_path << locale_path
end

would resolve this, or even one-lining it as Rails does with I18n.load_path += Dir["#{in_source_dir("_locales")}/**/*.{json,rb,yml}"].

@jaredcwhite
Copy link
Member

@robodinoshaun That makes sense to me. Would you be willing to submit a PR with the proposed fix (I'm fine with either option)?

@lux-shaun
Copy link
Contributor Author

Sure, I should be able to get around to that soon.

A loosely related suggestion would be updating the fallback chain for regional language variants to include the base locale (e.g. fr-CN -> fr -> en rather than simply fr-CN -> en)

@jaredcwhite
Copy link
Member

@robodinoshaun That seems reasonable. Out of curiosity, is that something Rails does automatically? Or is it a manual config step?

@lux-shaun
Copy link
Contributor Author

@jaredcwhite yeah, it does.

@lux-shaun
Copy link
Contributor Author

@jaredcwhite Actually, it's a function of ruby-i18n itself.
https://github.com/ruby-i18n/i18n/blob/master/lib/i18n/locale/fallbacks.rb

irb(main):001:0> require 'i18n'
irb(main):003:0> require 'i18n/backend/fallbacks'
irb(main):007:0> I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)
=> I18n::Backend::Simple
irb(main):010:0> I18n.fallbacks['es-ES']
=> [:"es-ES", :es]

This doesn't include the default locale, but Rails has a config setting that ultimately sets I18n.fallbacks = [I18n.default_locale], which includes it at the end of the chain. Demonstrated here:

irb(main):003:0> I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)
=> I18n::Backend::Simple
irb(main):004:0> I18n.fallbacks['fr-CA']
=> [:"fr-CA", :fr]
irb(main):005:0> I18n.fallbacks = [I18n.default_locale]
=> [:en]
irb(main):006:0> I18n.fallbacks['fr-CA']
=> [:"fr-CA", :fr, :en]

@jaredcwhite
Copy link
Member

@robodinoshaun Interesting! I'll have to take a second look at this along with #607…the sort of wonky looking code that was in there for fallbacks I had added for a reason, but now I can't for the life of me remember the reason! (should have added a comment!)

@lux-shaun
Copy link
Contributor Author

lux-shaun commented Jul 27, 2022

Interesting. If there IS a reason it was necessary, I would be curious as to what it is.

In which case I guess the required change would be to add the top-level directly to the code as written.
I considered this initially but when I realised how the I18n library handled it I felt it was a better solution.

        I18n.fallbacks = (config[:available_locales] + [:en]).uniq.to_h do |available_locale|
          [available_locale, [available_locale, available_locale.to_s.first(2).to_sym, locale, :en].uniq]
        end

The tests in the PR should still prove useful either way, I think.

@sandstrom
Copy link
Contributor

sandstrom commented Aug 30, 2022

For reference, we put this into a custom plugin builder.

But supporting sub-dirs by default would be neat.

# plugins/builders/custom_setup.rb
class Builders::CustomSetup < SiteBuilder
  def build
    I18n.load_path << Dir["#{site.source}/_locales/**/*.yml"]
  end
end

@jaredcwhite jaredcwhite added this to the 1.2 milestone Sep 23, 2022
@jaredcwhite
Copy link
Member

I think this is addressed now in v1.2.

@sandstrom
Copy link
Contributor

Awesome! @jaredcwhite

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants