belgoros
belgoros

Reputation: 3908

Rails 6 internationalization - what has changed?

I started a fresh new Rails 6 project and got stuck to figure out why what obviously just worked (it's not the first app I start), fails... So I created a dummy simply rails app with no additional gems and a home#index page:

<h1>Home#index</h1>
<p>
    <%= t('hello.world') %>
</p>

Then I added a translation for the above key to config/locales/en.yml:

en:
  hello: "Hello !"
    world: "Hello World!"

and respected 1 tab indentation. When navigating to localhost:3000/home/index I got the weird error:

/Users/serguei/.rvm/gems/ruby-2.7.0/gems/i18n-1.8.2/lib/i18n.rb:195: warning: The called method `translate' is defined here
  Rendered home/index.html.erb within layouts/application (Duration: 6.2ms | Allocations: 3150)
Completed 500 Internal Server Error in 16ms (ActiveRecord: 0.0ms | Allocations: 5045)



ActionView::Template::Error (can not load translations from /Users/serguei/projects/rails/draft-app/config/locales/en.yml: #<Psych::SyntaxError: (<unknown>): did not find expected key while parsing a block mapping at line 2 column 3>):
    1: <h1>Home#index</h1>
    2: <p>
    3:  <%= t('hello.world') %>
    4: </p>

app/views/home/index.html.erb:3

When changed the called translation to just hello:

<h1>Home#index</h1>
<p>
    <%= t('hello') %>
</p>

and removing the last line from the en.yml file:

en:
  hello: "Hello !"

it works. Why so? What has changed since Rails 5 there? Can't we use nested translations anymore in locales files? Rails guides have nothing special about that. Or am I missing something?

Adding rails-i18n gem to the Gemfile didn't solve the problem.

Upvotes: 1

Views: 1096

Answers (1)

Rajdeep Singh
Rajdeep Singh

Reputation: 17834

If you want to nest it, then you can't assign a string value to the parent, do this instead

en:
  hello:
    world: "Hello World!"

Then, in erb, this will work

<%= t('hello.world') %>

Give it a try.

Upvotes: 3

Related Questions