在 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'))
这会让你站点名称是一个在线系统"
.
但是,如果您迫切希望在 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(" ")
这也会让你站点名称是一个在线系统"
.
然而,在你决定走这条路之前,除了@felipeclopes 链接的问题,看看:
However, before you decide to go down this path, apart from the question that @felipeclopes linked to, have a look at:
- 这个 StackOverflow 答案关于连接 i18n 字符串(tl;dr 请不要为了您的翻译团队的缘故).
- 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屋!