-
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 and its translation.
The HAML template parser tries to be safe and escape text to HTML entities when needed. Sometimes this is not desired, however. If the strings from our locales files have no variables that may contain user input, they are safe anyway and we can print them raw by using !=
instead of =
. An example can be found in passing a link to a translation in sessions/new.html.haml (translation), where
Achtung, Cookies und Javascript müssen aktiviert sein!
= link_to "NoScript", "http://noscript.net/"
bitte abschalten.
was changed to
!= t '.nojs', link: link_to(t('.noscript'), "http://noscript.net/")
Here two translations texts were used:
nojs: 'Achtung, Cookies und Javascript müssen aktiviert sein! %{link} bitte abschalten.'
noscript: 'NoScript'
Since link_to()
returns html code, it should not be escaped. Still we want to include it in the .nojs
translation, since different languages may have a different word order. As all the inputs here (%{link}
) are completely fixed, it is safe to print it raw using !=
.
I hope this is enough to get you going! And please feel free to expand this.