在Rails国际化yml文件中传递变量 [英] Passing variables inside rails internationalization yml file

查看:82
本文介绍了在Rails国际化yml文件中传递变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在那里使用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:

  • 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆