我开始了一个新的Rails 6新项目,并被困在弄清楚为什么显然起作用的东西(这不是我启动的第一个应用程序)失败了...因此,我创建了一个虚拟的简单Rails应用程序,该应用程序没有其他宝石,并且没有home#index
页面:
<h1>Home#index</h1>
<p>
<%= t('hello.world') %>
</p>
然后我将上述键的译文添加到config/locales/en.yml
:
en:
hello: "Hello !"
world: "Hello World!"
并遵守1个制表符缩进。导航到localhost:3000/home/index
时出现奇怪的错误:
/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
将调用的翻译更改为仅hello
:
<h1>Home#index</h1>
<p>
<%= t('hello') %>
</p>
并从en.yml
文件中删除最后一行:
en:
hello: "Hello !"
有效。为什么这样?自从Rails 5出现了什么变化?我们不能在语言环境文件中使用嵌套翻译了吗? Rails guides对此没有什么特别的。还是我错过了什么?
向宝石文件添加rails-i18n宝石不能解决问题。
如果要嵌套它,则不能将字符串值分配给父级,而是执行此操作