Ruby-on-rails 在 Rails 国际化 yml 文件中传递变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13055753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing variables inside rails internationalization yml file
提问by alex
I want to use variables declared in yml file right there.
For example, I declared site_nameand want to use it below in description.
我想在那里使用 yml 文件中声明的变量。例如,我site_name在下面声明并想在description.
en:
site_name: &site_name "Site Name"
static_pages:
company:
description: *site_name #this works fine
description: "#{*site_name} is an online system" #this doesn't work
How can I combine *site_namevariable with additional text?
如何将*site_name变量与附加文本结合起来?
回答by Paul Fioravanti
The short answer is, I believe, no you cannot do string interpolation in YAML the way you want using an alias.
简短的回答是,我相信,不,您不能使用alias以您想要的方式在 YAML 中进行字符串插值。
In your case, what I would do is have something like the following in my locale file:
在您的情况下,我会做的是在我的语言环境文件中包含以下内容:
en:
site_name: "Site Name"
static_pages:
company:
description: ! '%{site_name} is an online system'
and then call in the appropriate view with the site name as a parameter:
然后以站点名称作为参数调用适当的视图:
t('.description', site_name: t('site_name'))
which would get you "Site Name is an online system".
这会让你"Site Name is an online system"。
However, if you're desperate to use aliases in your YAML file to concatenate strings together, the following completely unrecommendedcode would also work by having the string be two elements of an array:
但是,如果您迫切希望在 YAML 文件中使用别名将字符串连接在一起,那么以下完全不推荐的代码也可以通过将字符串作为数组的两个元素来工作:
en:
site_name: &site_name "Site Name"
static_pages:
company:
description:
- *site_name
- "is an online system"
and then you would jointhe array in the appropriate view like this:
然后你会join在适当的视图中排列数组,如下所示:
t('.description').join(" ")
Which would also get you "Site Name is an online system".
这也会让你"Site Name is an online system"。
However, before you decide to go down this path, apart from the question that @felipeclopes linked to, have a look at:
但是,在您决定走这条路之前,除了@felipeclopes 链接的问题之外,请查看:
- this StackOverflow answerregarding concatenating i18n strings (tl;dr Please don't for your translation team's sake).
- StackOverflow questions hereand herethat are similar to your question.
- 这个关于连接 i18n 字符串的StackOverflow 答案(tl;dr 请不要为了你的翻译团队)。
- 此处和此处的StackOverflow 问题与您的问题类似。
回答by felipeclopes
You can use the following syntax, like in the example:
您可以使用以下语法,如示例中所示:
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
password: *password
confirmation: *confirmation
sessions:
new:
email: *email
password: *password
This example was taken from: Refactoring Ruby on Rails i18n YAML files using dictionaries

