Passing variables inside rails internationalizing a yml file

I want to use the variables declared in the 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 

How can I combine the variable *site_name with additional text?

+30
ruby-on-rails yaml ruby-on-rails-3 internationalization
Oct 24
source share
2 answers

The short answer, I believe, no, you cannot do line interpolation in YAML the way you want using an alias .

In your case, what would I do, there is 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 the corresponding view with the site name as a parameter:

 t('.description', site_name: t('site_name')) 

which will bring you the "Site Name is an online system" .

However, if you are desperate to use aliases in your YAML file to concatenate strings together, the following completely unsuitable code will also work if the string is two elements of an array:

 en: site_name: &site_name "Site Name" static_pages: company: description: - *site_name - "is an online system" 

and then you would join array in the appropriate form as follows:

 t('.description').join(" ") 

Which will also bring you the "Site Name is an online system" .

However, before you decide to go this route, besides the question related to @felipeclopes, look:

  • this StackOverflow answer regarding string concatenation i18n (tl; dr Please, not for your translation team).
  • StackOverflow questions here and here , similar to your question.
+53
Dec 19
source share

You can use the following syntax, for example, in the following 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

+6
Oct 24
source share



All Articles