Translate YAML files

WebTranslateIt supports YAML files, including support for pluralization. YAML files can come in 4 flavors.

The classic ruby .yml file

As used by the i18n gem included in Ruby on Rails.

    anonymous: "Business"
    :yes: "Yes"
    :no: "No"
      one: "1 bacon"
      other: "{{count}} bacons"

The more complex .yml file

As used by the r18n gem.

hi: !!markdown
  Hi, **people**!
greater: !!escape
  1 < 2 is true
  signup: !!gender
    male: Он зарегистрировался
    female: Она зарегистрировалась
  edit: Edit user
  name: User name is %1
  count: !!pl
    1: There is 1 user
    n: There are %1 users

WebTranslateIt can use a few filters: proc, pl, String, R18n::Untranslated, escape, html, markdown, textile and gender. Please contact Support if you need another filter.

The flat yaml file

As used by some PHP i18n libraries.

segment1: "translation1"
segment2: "translation2"
segment3: "translation3"

YAML files for Hugo framework

As used by the Hugo Framework.

- id: still_need_help
  translation: "Still need help?"

- id: get_in_touch
  translation: "We’re here to help. Get in touch and we’ll get back to you as soon as we can."

- id: contact_support
  translation: "Contact Support"

- id: copyright
  translation: "Copyright"

- id: all_rights_reserved
  translation: "All rights reserved."

- id: help_center
  translation: "Help Center"

- id: search_manual
  translation: "Search Manual"

- id: getting_started
  translation: "Need help getting started or having trouble configuring a device?"


Plural rules

You will notice that plural keys such as “one” aren’t editable in the Chinese interface. This is no mistake, this is due to the fact that the “one” rule doesn’t exist in the Chinese language, so it doesn’t make sense to have one.

For an in-depth explanation about plural rules, visit Understanding Plural Strings

In English, we have our well-known plural rules “one” and “other”:

0 dogs # Uses “other” rule
1 dog  # Uses “one” rule
7 dogs # Uses “other” rule

The word “dog” changes depending on the quantifier.

In Chinese words don’t change depending on the quantifier like most languages do. They would say something like:

0 dog # Uses “other” rule
1 dog # Uses “other” rule
2 dog # Uses “other” rule
7 dog # Uses “other” rule

The plural rules are defined by CLDR, which has documented all the known rules.

Now here’s the thing: The Ruby on Rails framework only knows how to handle English plural rules (one/other) which works for most occidental languages. It works for German for instance, but works badly for French and it’s really not adapted for Chinese, Arabic, Russian or Polish. You need to use the pluralization back-end.

Using the pluralizer back-end in your Rails application:

  1. Create a config/initializers/pluralization.rb file in your app, and copy/paste the following code to add the pluralization backend:

    require "i18n/backend/pluralization"
    I18n::Backend::Simple.send(:include, I18n::Backend::Pluralization)
  2. Download this plurals.rb file, which is part of the i18n gem and place the file under the config/locales/ directory.

  3. If necessary, rename the locales key names to suit your application’s needs. For instance, if you use the zh-CN locale code instead of zh, rename the zh key to zh-CN in the plurals.rb file.

  4. Restart your application. Rails will be able to create plural phrases using the correct plural rules for Chinese and other languages.

“no”, “yes” imported as false in WebTranslateIt?

YAML strings are typed, which means some keys are cast into a specific type. For instance, the number 1 is cast into an Integer and the word false is cast into a boolean.

Mind, though, as there are also shorthands for booleans, like yes, no, on, off, which are cast into booleans. The words no and off are equal to false You can read more about it in the YAML specification regarding booleans.

You can easily reproduce this behaviour. Given this YAML file:

    es: Spain
    no: "Norway (doesn’t work)"
    "no": Norway

Let’s try to load this YAML file using irb:

$ irb
ruby-1.9.3-p0 :001 > require 'yaml'
 => true 
ruby-1.9.3-p0 :002 > YAML.load_file('test.yaml')
 => {"en"=>{"country"=>{"es"=>"Spain", false=>"Norway (doesn’t work)", "no"=>"Norway"}}} 

The no key is interpreted as false by Ruby. In order for you to use the no key for Norway in your app, you’ll have to rename your key in your file to "no", so it can be cast as a string instead of as a boolean.

Once this is done, upload your language file to WebTranslateIt again, and the no key should appear, instead of false.

Syck, Psych and ya2yaml

Syck is an old YAML parser and dumper used by old versions of ruby (< 1.9). Newer version of ruby are compiled with Psych if the library libyaml is installed on your system. Psych is also rails’s preferred YAMLer when available.

It turns out, some files dumped using Syck cannot be parsed using Psych.

The good news is that WebTranslateIt recognizes both formats: YAML files dumped with Psych and those dumped with Syck. Internally, the system will first try to load a YAML file using Psych. If the parsing fails, it will try to load the YAML file using Syck.

However, to retain compatiblity with both parsers, the YAML files generated by WebTranslateIt are dumped using a third library called ya2yaml, which is compatible with both Psych and Syck parsers. This is the reason why you will notice a few syntactic changes when you download your YAML files.

Type Casting, nil and blank values

WebTranslateIt supports Type Casting. That is, if you upload a segment as an String, Array, Integer, Float or Symbol, it will be exported as such.

WebTranslateIt can’t determine the type of blank or untranslated values, though. A segment left untranslated can be considered as a nil entry, using the ~ symbol or as a blank entry using "".

  • By default, untranslated and unproofread translations are exported as nil (~), so Rails’s fallback mechanism know that there is no translation intentionally left blank here.

  • If you want the translation to be blank (and not nil), leave the translation box blank and flag the translation as proofread. WTI will know that this segment is meant to be left blank ("").

  • If you want the translation to be nil, place ~ in the translation box. This segment will be exported as a nil entry.

More Information