在Rails国际化yml文件中传递变量 [英] Passing variables inside rails internationalization yml file
问题描述
我想在那里使用yml文件中声明的变量.
例如,我声明了site_name
,并想在下面的description
中使用它.
I want to use variables declared in yml file right there.
For example, I declared site_name
and want to use it below in 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
如何将*site_name
变量与其他文本结合在一起?
How can I combine *site_name
variable with additional text?
推荐答案
The short answer is, I believe, no you cannot do string interpolation in YAML the way you want using an alias.
在您的情况下,我要做的是在语言环境文件中包含以下内容:
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'))
这会让您"Site Name is an online system"
.
但是,如果您迫切希望在YAML文件中使用别名将字符串连接在一起,则以下完全不推荐使用的代码也可以通过使字符串成为数组的两个元素来工作:>
However, if you're desperate to use aliases in your YAML file to concatenate strings together, the following completely unrecommended code would also work by having the string be two elements of an array:
en:
site_name: &site_name "Site Name"
static_pages:
company:
description:
- *site_name
- "is an online system"
,然后您将在适当的视图中将数组join
像这样:
and then you would join
the array in the appropriate view like this:
t('.description').join(" ")
还能帮到你"Site Name is an online system"
.
但是,在决定走这条路之前,除了@felipeclopes链接到的问题之外,还请看看:
However, before you decide to go down this path, apart from the question that @felipeclopes linked to, have a look at:
-
关于连接i18n字符串的
- 此StackOverflow答案(tl;请不要为了翻译团队的缘故).
- StackOverflow问题此处和此处与您的问题类似.
- this StackOverflow answer regarding concatenating i18n strings (tl;dr Please don't for your translation team's sake).
- StackOverflow questions here and here that are similar to your question.
这篇关于在Rails国际化yml文件中传递变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!