Passing variables inside rails internationalization yml file

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


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'))

which would get you "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 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"

and then you would join the array in the appropriate view like this:

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

Which would also get you "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:

  • 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.