I18nize Scala programs à la gettext
Transcript of I18nize Scala programs à la gettext
![Page 1: I18nize Scala programs à la gettext](https://reader035.fdocuments.us/reader035/viewer/2022062515/55d6dc5ebb61eb4e538b45db/html5/thumbnails/1.jpg)
i18nize Scala programsà la gettext
Episode 39, Wed, Aug 12, 2015Ngoc Dao
![Page 2: I18nize Scala programs à la gettext](https://reader035.fdocuments.us/reader035/viewer/2022062515/55d6dc5ebb61eb4e538b45db/html5/thumbnails/2.jpg)
About the speaker
● Ngoc Dao● Joined Atlassian since May
![Page 3: I18nize Scala programs à la gettext](https://reader035.fdocuments.us/reader035/viewer/2022062515/55d6dc5ebb61eb4e538b45db/html5/thumbnails/3.jpg)
● Favorite languages:Ruby, Erlang, Scala
![Page 4: I18nize Scala programs à la gettext](https://reader035.fdocuments.us/reader035/viewer/2022062515/55d6dc5ebb61eb4e538b45db/html5/thumbnails/4.jpg)
● Favorite languages:Ruby, Erlang, Scala
● My Scala style:Using Scala as ifScala = Java (performance, libs eco) + Ruby (human oriented) + Erlang (functional)
![Page 5: I18nize Scala programs à la gettext](https://reader035.fdocuments.us/reader035/viewer/2022062515/55d6dc5ebb61eb4e538b45db/html5/thumbnails/5.jpg)
● Don’t know Scalaz/Haskell (yet)
↑↑↑
Struggling withmonadic talks at ScalaSyd
![Page 6: I18nize Scala programs à la gettext](https://reader035.fdocuments.us/reader035/viewer/2022062515/55d6dc5ebb61eb4e538b45db/html5/thumbnails/6.jpg)
English is not my native language● From Viet Nam● 15 years in Japan
↑↑↑
i18n is important to me
![Page 7: I18nize Scala programs à la gettext](https://reader035.fdocuments.us/reader035/viewer/2022062515/55d6dc5ebb61eb4e538b45db/html5/thumbnails/7.jpg)
Given this program:printf("My name is %s.", myName)
![Page 8: I18nize Scala programs à la gettext](https://reader035.fdocuments.us/reader035/viewer/2022062515/55d6dc5ebb61eb4e538b45db/html5/thumbnails/8.jpg)
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)
![Page 9: I18nize Scala programs à la gettext](https://reader035.fdocuments.us/reader035/viewer/2022062515/55d6dc5ebb61eb4e538b45db/html5/thumbnails/9.jpg)
(1) Mark the strings we want to translate
printf(t("My name is %s."), myName)
![Page 10: I18nize Scala programs à la gettext](https://reader035.fdocuments.us/reader035/viewer/2022062515/55d6dc5ebb61eb4e538b45db/html5/thumbnails/10.jpg)
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)
![Page 11: I18nize Scala programs à la gettext](https://reader035.fdocuments.us/reader035/viewer/2022062515/55d6dc5ebb61eb4e538b45db/html5/thumbnails/11.jpg)
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)
![Page 12: I18nize Scala programs à la gettext](https://reader035.fdocuments.us/reader035/viewer/2022062515/55d6dc5ebb61eb4e538b45db/html5/thumbnails/12.jpg)
When strings in the program change, how to update (add, remove, modify) language files efficiently?
![Page 13: I18nize Scala programs à la gettext](https://reader035.fdocuments.us/reader035/viewer/2022062515/55d6dc5ebb61eb4e538b45db/html5/thumbnails/13.jpg)
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)
![Page 14: I18nize Scala programs à la gettext](https://reader035.fdocuments.us/reader035/viewer/2022062515/55d6dc5ebb61eb4e538b45db/html5/thumbnails/14.jpg)
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
![Page 15: I18nize Scala programs à la gettext](https://reader035.fdocuments.us/reader035/viewer/2022062515/55d6dc5ebb61eb4e538b45db/html5/thumbnails/15.jpg)
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
![Page 16: I18nize Scala programs à la gettext](https://reader035.fdocuments.us/reader035/viewer/2022062515/55d6dc5ebb61eb4e538b45db/html5/thumbnails/16.jpg)
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 tokey → valuedata structure
vi.po language file:
msgid "My name is %s."
msgstr "Tôi tên là %s."
![Page 17: I18nize Scala programs à la gettext](https://reader035.fdocuments.us/reader035/viewer/2022062515/55d6dc5ebb61eb4e538b45db/html5/thumbnails/17.jpg)
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
![Page 18: I18nize Scala programs à la gettext](https://reader035.fdocuments.us/reader035/viewer/2022062515/55d6dc5ebb61eb4e538b45db/html5/thumbnails/18.jpg)
gettext is quite advanced
● Allow specifying context(one string may be translated to different strings, depending on context)
![Page 19: I18nize Scala programs à la gettext](https://reader035.fdocuments.us/reader035/viewer/2022062515/55d6dc5ebb61eb4e538b45db/html5/thumbnails/19.jpg)
print(t("Hello"))print(tc("Casual", "Hello"))
fr.po language file:
msgid "Hello"msgstr "Bonjour"
msgctxt "Casual"msgid "Hello"
msgstr "Salut"
![Page 20: I18nize Scala programs à la gettext](https://reader035.fdocuments.us/reader035/viewer/2022062515/55d6dc5ebb61eb4e538b45db/html5/thumbnails/20.jpg)
gettext is quite advanced
● Allow specifying singular/plural rules(different languages may have different singular/plural rules)
![Page 21: I18nize Scala programs à la gettext](https://reader035.fdocuments.us/reader035/viewer/2022062515/55d6dc5ebb61eb4e538b45db/html5/thumbnails/21.jpg)
print(tn( "I have one apple", "I have %d apples", numApples))
Singular
Plural
Depending on this value
![Page 22: I18nize Scala programs à la gettext](https://reader035.fdocuments.us/reader035/viewer/2022062515/55d6dc5ebb61eb4e538b45db/html5/thumbnails/22.jpg)
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 ruleSpecial key
![Page 23: I18nize Scala programs à la gettext](https://reader035.fdocuments.us/reader035/viewer/2022062515/55d6dc5ebb61eb4e538b45db/html5/thumbnails/23.jpg)
DemoI’ll introduce some tools
![Page 24: I18nize Scala programs à la gettext](https://reader035.fdocuments.us/reader035/viewer/2022062515/55d6dc5ebb61eb4e538b45db/html5/thumbnails/24.jpg)
Hello.scala
println("Hello world")
![Page 25: I18nize Scala programs à la gettext](https://reader035.fdocuments.us/reader035/viewer/2022062515/55d6dc5ebb61eb4e538b45db/html5/thumbnails/25.jpg)
Mark and extract i18n strings
![Page 26: I18nize Scala programs à la gettext](https://reader035.fdocuments.us/reader035/viewer/2022062515/55d6dc5ebb61eb4e538b45db/html5/thumbnails/26.jpg)
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.
![Page 27: I18nize Scala programs à la gettext](https://reader035.fdocuments.us/reader035/viewer/2022062515/55d6dc5ebb61eb4e538b45db/html5/thumbnails/27.jpg)
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
![Page 28: I18nize Scala programs à la gettext](https://reader035.fdocuments.us/reader035/viewer/2022062515/55d6dc5ebb61eb4e538b45db/html5/thumbnails/28.jpg)
● 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
![Page 29: I18nize Scala programs à la gettext](https://reader035.fdocuments.us/reader035/viewer/2022062515/55d6dc5ebb61eb4e538b45db/html5/thumbnails/29.jpg)
● 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
![Page 30: I18nize Scala programs à la gettext](https://reader035.fdocuments.us/reader035/viewer/2022062515/55d6dc5ebb61eb4e538b45db/html5/thumbnails/30.jpg)
Translate language template file
![Page 31: I18nize Scala programs à la gettext](https://reader035.fdocuments.us/reader035/viewer/2022062515/55d6dc5ebb61eb4e538b45db/html5/thumbnails/31.jpg)
Translate language template file
Tool:Any text editor
![Page 32: I18nize Scala programs à la gettext](https://reader035.fdocuments.us/reader035/viewer/2022062515/55d6dc5ebb61eb4e538b45db/html5/thumbnails/32.jpg)
Translate language template file
Tool:Any text editor
Very convenient GUI editor:
https://poedit.net/
![Page 33: I18nize Scala programs à la gettext](https://reader035.fdocuments.us/reader035/viewer/2022062515/55d6dc5ebb61eb4e538b45db/html5/thumbnails/33.jpg)
Load language file
Tool:https://github.com/xitrum-framework/scaposer
It’s a Scala parser to transform .po text file intokey → valuedata structure
![Page 34: I18nize Scala programs à la gettext](https://reader035.fdocuments.us/reader035/viewer/2022062515/55d6dc5ebb61eb4e538b45db/html5/thumbnails/34.jpg)
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
![Page 35: I18nize Scala programs à la gettext](https://reader035.fdocuments.us/reader035/viewer/2022062515/55d6dc5ebb61eb4e538b45db/html5/thumbnails/35.jpg)
Demo source code:https://github.com/xitrum-framework/scala-xgettext-presentation
If you use Play framework:https://github.com/georgeOsdDev/play-xgettext