Skip to content

Project Level i18n Support

vicwin edited this page Mar 6, 2012 · 12 revisions

In order for the project to use i18n translation support (by using the i18n gem), the following steps are needed to setup the project:

  • create a “locale.yml” file under the ./projects/<your project>/etc/ directory.
    • the yml file has the following format:
      —-
      en: #different local sets
      risk_report: #this needs to be the cucumber feature file name in your project
      hello: Hello #the symbol or key and then map to the corresponding translation
      test_complete: test%{var}OK
      cn:
      risk_report:
      hello: 你好
      test_complete: 测试%{var}成功
  • The default locale is “en”, and you can change the locale setting in your project_init.rb file
    •  ENV[“WARDEN_TEST_TARGET_LOCALE”] = “cn” 
  • You can also set the locale in your step as well:
    •  @warden_session.set_locale(“ch”)
  • To get the translated string from the yml file, you will need to do these in your step file:
    _t(:hello) # => "Hello"
    _t("hello") # => "Hello"
    @warden_session.set_locale("ch") #set the locale to Chinese
    _t("hello") # => "你好"
    _t(:test_complete, :var => "111") # => 测试111成功
    
    • The _t(*args) method is very similar to the i18n.translate() method, you can look at these links for more advance usage Rails i18n example , i18n Gem wiki

The Global Reference

Sometimes you want the same translation to be referenced in many of the feature files. we can achieve this by using defining a Global section.

---
en:
  _GLOBAL_:
    hello: Hello
  risk_report:
    test_complete: test%{var}ok

To use translation defined in the GLOBAL section, you just need to add a ‘$’ sign in front of the translation string. e.g.

_t('$hello')

PKG’s v.s. project’s locale.yml

If there is a locale.yml in a project that uses pkg features and lib, it will be used as default; however the project level’s locale.yml will take precedent if the same key exists in both files, otherwise, i18n will uses both translations defined in these two files together.