-
Notifications
You must be signed in to change notification settings - Fork 146
Translating templates
Each subdirectory of app
gets its own de.xxx.yml
file in config/locales/de
(please see existing files for a starting point). All German strings need to be changed, e.g. in the file app/views/home/_start_nav.haml
%h3 Direkt zu ...
was changed to
%h3= t '.title'
The =
lets the remainder of the line be parsed as code. The t()
function takes care of translation, and it argument '.title'
is an identifier. Since it starts with a .
, Ruby on Rails prepends view directory and simplified filename in addition to the locale, so that the full identifier becomes de.home.start_nav
. This is put into config/locales/de/de.home.yml.
When there is a more complex command on the line, the function call needs to have brackets. E.g.
%li= link_to "Mitglieder", foodcoop_users_path
was changed to
%li= link_to t('.members'), foodcoop_users_path
The brackets are required so that the parser knows that foodcoop_users_path
belongs to link_to()
(and not to t()
).
Another issue is when code appears in the text. To separate code from text, it is better to put that in an argument than inside the translation. For example
%small (Letzte Aktualisierung ist #{distance_of_time_in_words(Time.now, current_user.ordergroup.account_updated)} her)
was changed to
%small= t '.my_ordergroup.last_update', when: distance_of_time_in_words(Time.now, current_user.ordergroup.account_updated)
The corresponding text in in the locale file was set to Letzte Aktualisiering ist %{when} her
. Note the use of the percent sign to reference the variable given on the t()
command.
Most of the time it is sensible to use quotes in the locales files. Any special characters in YAML files will then be treated properly. This may, however, put html code (like a span
) in the translated text. If this is not acceptable, for example as the value of an input
field or textarea
, do not use quotes around the text. One example can be found in (ordergroup.html.haml)[https://github.com/wvengen/foodsoft/blob/2b11549d33b1085b61497d8022274919fa8d058a/app/views/home/ordergroup.html.haml#L24] and its (translation)[https://github.com/wvengen/foodsoft/blob/2b11549d33b1085b61497d8022274919fa8d058a/config/locales/de/de.home.yml#L67].
I hope this is enough to get you going! And please feel free to expand this.