This document discusses internationalizing (i18n) Scala programs using the gettext approach. It describes marking strings for translation, extracting them to template files, loading translation files, and translating at runtime. Tools are introduced for extracting strings from Scala code and templates, editing translation files, and parsing loaded files. The approach works for various Scala template engines and frameworks like Play.
8. Given this program:
printf("My name is %s.", myName)
Let’s i18nize it in gettext style!
(I won’t go into details of .properties style vs gettext style)
9. (1) Mark the strings we want to translate
printf(t("My name is %s."), myName)
10. i18n.pot template file:
msgid "My name is %s."
msgstr ""
(2) Extract to a template file
(1) Mark the strings we want to translate
printf(t("My name is %s."), myName)
11. i18n.pot template file:
msgid "My name is %s."
msgstr ""
fr.po language file:
msgid "My name is %s."
msgstr "Je m'appelle %s."
(2) Extract to a template file
(3) Give it to translators
(1) Mark the strings we want to translate
printf(t("My name is %s."), myName)
12. When strings in the program change, how to
update (add, remove, modify) language files
efficiently?
13. When strings in the program change, how to
update (add, remove, modify) language files
efficiently?
● Automatically copy similar existing
translation to make new translation (marked
as “fuzzy” to be modified by a human later)
14. When strings in the program change, how to
update (add, remove, modify) language files
efficiently?
● Automatically copy similar existing
translation to make new translation (marked
as “fuzzy” to be modified by a human later)
● Don’t delete, just comment out dated
translations for future references
15. When strings in the program change, how to
update (add, remove, modify) language files
efficiently?
● Automatically copy similar existing
translation to make new translation (marked
as “fuzzy” to be modified by a human later)
● Don’t delete, just comment out dated
translations for future references
● In translation files, sort by msgid so that it’s
easier to diff versions
16. fr.po language file:
msgid "My name is %s."
msgstr "Je m'appelle %s."
ja.po language file:
msgid "My name is %s."
msgstr "%sと申します。"
(4) Load language files to program,
basically parse the files to
key → value
data structure
vi.po language file:
msgid "My name is %s."
msgstr "Tôi tên là %s."
17. printf(t("My name is %s."), myName)
printf("Je m’appelle %s.", myName)
(5) At run time, the marker acts as a
function to replace the given key
with its value
18. gettext is quite advanced
● Allow specifying context
(one string may be translated to
different strings, depending on
context)
20. gettext is quite advanced
● Allow specifying singular/plural rules
(different languages may have
different singular/plural rules)
21. print(tn(
"I have one apple",
"I have %d apples",
numApples
))
Singular
Plural
Depending on this value
22. fr.po language file:
msgid ""
msgstr "Plural-Forms: nplurals=2; plural=n>1;"
msgid "I have one apple"
msgid_plural "I have %d apples"
msgstr[0] "J'ai une pomme"
msgstr[1] "J'ai %d pommes"
Singular/plural rule
Special key
26. Mark and extract i18n strings
Tool:
https://github.com/xitrum-framework/scala-xgettext
It’s a Scala compiler plugin, to extract i18n
strings at compile time.
27. Mark and extract i18n strings
Tool:
https://github.com/xitrum-framework/scala-xgettext
It’s a Scala compiler plugin, to extract i18n
strings at compile time.
↑↑↑
Neat! Just compile Scala source code and get the strings.
Scala is powerful
28. ● scala-xgettext can also extracts i18n strings
from view templates
● as long as the templates are converted to
Scala source code and compiled
Mark and extract i18n strings
29. ● scala-xgettext can also extracts i18n strings
from view templates
● as long as the templates are converted to
Scala source code and compiled
↑↑↑
It works for all popular Scala template engines like Scalate,
Scalatags, Twirl (Play framework template engine)
Mark and extract i18n strings
34. Poedit demo: Update i18n strings
● Add, modify, remove i18n strings in program
● Regenerate i18n.pot file
● Use Poedit to update existing .po files with
the new i18n.pot file
● Poedit can give translation hints for similar
strings