Skip to content

Clarification Needed: Behavior of i18n.load_path and Nested Dictionaries #53912

Closed
@shrkw

Description

This is not a bug report but rather a request for clarification about the specification.
I would like to confirm the behavior of i18n.load_path.

According to the Rails Guide, it is described as follows, but the actual behavior was different. Even with the default locale loading mechanism, files inside nested directories were loaded.

NOTE: The default locale loading mechanism in Rails does not load locale files in nested dictionaries, like we have here. So, for this to work, we must explicitly tell Rails to look further:

# config/application.rb
config.i18n.load_path += Dir[Rails.root.join("config", "locales", "**", "*.{rb,yml}")]

https://github.com/rails/rails/blob/main/guides/source/i18n.md?plain=1#L592

Is this simply because the documentation has not been updated, or is the behavior described in the documentation the intended one?

Steps to reproduce

I created a new Rails application without any config changes and placed files with the following structure:

$ tree config/locales/
config/locales/
├── en.yml
└── foo
    └── bar
        └── baz
            └── ja.yml

3 directories, 2 files

The result was as follows, and the files inside the nested directories were successfully loaded.

$ bin/rails console
Loading development environment (Rails 7.1.5)
irb(main):001> Rails.application.config.i18n.load_path
=> 
["/workspaces/sandbox-ruby/sample-7/config/locales/en.yml",
 "/workspaces/sandbox-ruby/sample-7/config/locales/foo/bar/baz/en.yml",
 "/usr/local/lib/ruby/gems/3.1.0/gems/web-console-4.2.1/lib/web_console/locales/en.yml"]
irb(main):002> I18n.t("foo.bar.hello")
=> "hello2"

Expected behavior

Actual behavior

System configuration

Rails version: 7.1.5

Ruby version: ruby 3.1.4p223 (2023-03-30 revision 957bb7cb81) [aarch64-linux]

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions