这个 StackOverflow问题给了我一些关于Rails i18n文件的良好结构的思考,所以我想我会分享另一个结构来重构Rails i18n yml文件供您考虑/批评.
鉴于我想
t('.some_translation')在我的视图中使用简写的"懒惰"查找,并且知道在应用程序中使用翻译的地方,对于config/locales/en.yml文件,看起来像这样:
activerecord:
attributes:
user:
email: Email
name: Name
password: Password
password_confirmation: Confirmation
models:
user: User
users:
fields:
email: Email
name: Name
password: Password
confirmation: Confirmation
sessions:
new:
email: Email
password: Password
Run Code Online (Sandbox Code Playgroud)
我可以看到存在重大的重复,并且诸如"电子邮件"和"密码"之类的词语的上下文是明确的并且在它们各自的视图中具有相同的含义.如果我决定将"电子邮件"更改为"电子邮件",那么必须去更改它们会有点烦人,所以我想重构字符串以引用某种字典.那么,如何使用&这样的锚点将字典哈希添加到文件的顶部:
dictionary:
email: &email Email
name: &name Name
password: &password Password
confirmation: &confirmation Confirmation
activerecord:
attributes:
user:
email: *email
name: *name
password: *password
password_confirmation: *confirmation
models:
user: User
users:
fields:
email: *email
name: *name …Run Code Online (Sandbox Code Playgroud) refactoring yaml structure ruby-on-rails internationalization
我知道i18n语言环境文件中有一些预设结构,以便Rails自动提取值.例如,如果要为新记录设置默认提交按钮文本:
# /config/locales/en.yml
en:
helpers:
submit:
create: "Create %{model}"
user:
create: "Sign Up"
Run Code Online (Sandbox Code Playgroud)
使用此设置,在视图中将产生以下结果:
# /app/views/things/new.html.erb
<%= f.submit %> #=> Renders a submit button reading "Create Thing"
# /app/views/users/new.html.erb
<%= f.submit %> #=> Renders a submit button reading "Sign Up"
Run Code Online (Sandbox Code Playgroud)
因此,Rails使用预设层次结构来获取不同模型的提交按钮文本.(也就是说,你不必告诉它在使用时可以获得哪些i18n文本f.submit.)我一直试图找到一种方法来使用flash通知和警报.是否有类似的预设结构来指定默认的Flash消息?
我知道您可以指定自己的任意结构,如下所示:
# /config/locales/en.yml
en:
controllers:
user_accounts:
create:
flash:
notice: "User account was successfully created."
# /app/controllers/users_controller.rb
def create
...
redirect_to root_url, notice: t('controllers.user_accounts.create.flash.notice')
...
end
Run Code Online (Sandbox Code Playgroud)
但notice: t('controllers.user_accounts.create.flash.notice')每次指定都很繁琐.有没有办法做到这一点,以便控制器"只知道"何时抓取并显示区域设置文件中指定的相应闪存消息?如果是这样,这些的默认YAML结构是什么?
编写一个完全翻译的应用程序可能会变得乏味。有没有办法为当前上下文设置默认翻译范围?
示例:我在我的show.html.erb操作中的部分_deadlines.html.erb中写入ProjectsController
现在,因为我正在努力成为一名优秀的程序员,所以我正在确定我所有的翻译。我想生成以下树
projects:
deadlines:
now: "Hurry the deadline is today !"
....
Run Code Online (Sandbox Code Playgroud)
我怎样才能让它比每次写全范围更不乏味?
项目/show.html.erb
...
<%= render 'projects/deadlines', project: @project %>
...
Run Code Online (Sandbox Code Playgroud)
从show.html.erb调用的projects/ _deadlines.html.erb
<p>Deadline : <%= t(:now, scope: [:projects, :deadlines]) %></p>
Run Code Online (Sandbox Code Playgroud)
有没有办法为当前上下文设置默认范围(这里是整个_deadlines.html.erb文件)?
编辑
有些人建议使用 Rails Lazy lookup,但这不会产生我正在寻找的范围。就我而言,我想跳过action默认范围(显示、索引等...)并为我正在渲染的当前部分添加一个范围(在我的情况下为 _deadlines.html.erb)
Rails 懒惰查找:
t('.now')
<=> t(:now, scope: [:projects, :show]
Run Code Online (Sandbox Code Playgroud)
但我想要:
t('.now')
<=> t(:now, scope: [:projects, :deadlines]
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用嵌套文件结构组织我的本地化文件,以便更容易查找.
我跟着
但我得到的翻译缺失:en.view.fruits.apple.我认为Rails试图只查找locales/en.yml文件中的翻译而不是子目录,尽管我已经将它们包括在内.
配置/ application.rb中:
config.i18n.load_path += Dir["#{Rails.root.to_s}/config/locales/**/*.{rb,yml}"]
Run Code Online (Sandbox Code Playgroud)
我的语言环境目录:
|locales
|-en.yml
|-views
|--en.yml
Run Code Online (Sandbox Code Playgroud)
区域设置/查看/ en.yml:
en:
fruits:
apple: "apple"
Run Code Online (Sandbox Code Playgroud)
意见/ fruit.html.haml:
= I18n.t('views.fruits.apple')
Run Code Online (Sandbox Code Playgroud) haml ruby-on-rails internationalization i18n-gem ruby-on-rails-4
erb ×1
haml ×1
i18n-gem ×1
locale ×1
rails-i18n ×1
refactoring ×1
ruby ×1
scope ×1
structure ×1
yaml ×1