Ruby on Rails YAML file (.yml)

 

YAML file format is a popular choice for storing translations in Ruby on Rails applications. That is not surprising, knowing it has a human-readable syntax, that it supports a wide variety of data types, and that it is suitable for data exchange.

The YAML file has a lot of capabilities. However, we will only focus on those that are relevant in terms of localization for Ruby on Rails applications.

Rails YAML file example

---
en-US:
  common:
    count:
      one: '%{count} item'
      other: '%{count} items'
    devices:
    - Desktop
    - Tablet
    - Mobile
    - Smartwatch
    next: Next
    previous: Previous
    welcome: Welcome
    welcome_html: <b>Welcome</b>
  page:
    home:
      intro: |-
        Welcome to our website,
        where technology and innovation meet creativity and design.
      subtitle: Welcome, %{name}!
      title: Home
...

Placeholder

Placeholders enable the insertion of dynamic values into translated text. Represented with %{variable_name} syntax, placeholders are interpolated at runtime based on the context, providing a powerful and flexible way to create dynamic translations.

The placeholder example:

welcome: Welcome, %{name}!

Plural

Plural localization messages are handy in case of translations that change based on a numerical value, such as "1 item" vs. "2 items". Using special pluralization keys in YAML files, the correct pluralization form is selected at runtime based on the variable's numerical value.

The plural example:

total:
  one: '%{count} item'
  other: '%{count} items'

Array

Array localization messages are useful for translating text that includes multiple values, such as lists or menus. They are represented with YAML arrays, where each element contains a separate translation for a different item.

The array example:

devices:
  - Desktop
  - Tablet
  - Mobile
  - Smartwatch

How to use HTML in localization messages in Rails?

To include HTML in localization messages in Rails, you can use the "_html" suffix with the key name in the YAML file. This informs Rails to treat the value as "Safe HTML" and render it accordingly.

What are the most frequent mistakes in YAML localization files for Rails?

Some of the most frequent mistakes in YAML localization files for Rails include syntax errors, incorrect formatting of keys or values, mismatched placeholders, missing translations, and improper indentation. Another common mistake is not properly handling pluralization rules across different languages.

Tired of manually editing translation files?

Our platform streamlines software localization for you.

Copyrights 2024 © Localizely