Translations / i18n
Translate your widget
ki18n
Ki18n (KDE internationalization) is the translation library for KDE. It has a programmer's guide which you can read, but we'll cover the basics here.
i18n()
Translated strings need to be wrapped in the i18n(...)
function. Note that single quotes i18n('Test')
will be ignored by the tool that parses your code for all the translation strings. Always use double quotes i18n("Test")
.
contents/ui/configGeneral.qml
Variables in i18n()
The i18n(...)
is an overloaded function which allows you to pass values into the translation i18n(format, variable1, variable2)
. Just place %1
where you want the first variable to be substituted, and %2
where the second should go.
contents/ui/main.qml
Plural in i18n()
In English, a translated sentence is different when there's just 1 item from when there is 2 or more items. i18np(...)
can be used in such a situation.
An example from the Ki18n docs is:
Using i18np(...)
can improve our previous example. When unreadEmailCount
was 1
, the tooltip would have read "1 unread emails"
.
contents/ui/main.qml
Translation Folder Structure
After we've wrapped all the messages in our code with i18n(...)
calls, we then need to extract all the messages for our translators into a template.pot
file which they can then create a fr.po
for their French translations.
We'll place the template.pot
file under a translate
folder inside the bundled package so that our users can easily translate our widget when they go poking into our code.
We'll also create a merge.sh
script which will extract the messages from our code into a template.pot
, then update the translated fr.po
file with any changes.
Lastly, we'll make a build.sh
script to convert the fr.po
text files into the binary .mo
files which are needed for KDE to recognize the translations.
The latest copy of my `merge.sh` and `build.sh` can be found here:
A working example can be seen in the Tiled Menu widget:
After running build.sh
we should end up with:
Install GetText
After we've wrapped all the messages in our code with i18n(...)
calls, we then need to extract all the messages for our translators into a template.pot
file.
To do this, we need to install the gettext
package.
Generating template.pot
First thing we need to do in our merge.sh
script, is list all files we wish to get translated in our widgets code.
The latest copy of my complete `merge.sh` script [can be found here](https://github.com/Zren/plasma-applet-lib/blob/master/package/translate/merge).
DIR
is the directory (absolute path to package/translate/
) since we may run the merge script from another directory.
We use kreadconfig5
to grab the widget's namespace (com.github.zren.helloworld
) and store it in plasmoidName
. We then remove the beginning of the namespace so we are left with helloworld
and store that in widgetName
. We also grab the website which a link to the GitHub repo for use as the bugAddress
.
After validating that plasmoidName
is not an empty string with bash's [ -z "$plasmoidName" ]
operator, we then list all .qml
and .js
files using find
and store the results of the command in a temporary infiles.list
file.
Then we generate a template.pot.new
using the xgettext
command. After generating it, we use sed
to replace a few placeholder strings.
In the complete merge.sh
, we also parse the widget name in metadata.desktop
first with xgettext --language=Desktop
and --join-existing
in the 2nd xgettext
call.
translate/merge.sh
Updating template.pot
Continuing our merge.sh
script, we then check to see if an older template.pot
file exists.
If it does, we'll replace the POT-Creation-Date
in the new file with the older creation date, then run the diff
command to detect if there's been any changes. If there has been changes, we fix the POT-Creation-Date
and overwrite the old template.pot
file. To make the changes more noticeable, we also list the added/removed translation messages.
If there hasn't been any changes, we simply delete the template.pot.new
file.
Lastly, we delete the infiles.list
to clean things up.
translate/merge.sh
Examining template.pot
Now that we've got a template.pot
, let's take a look at it.
The messages we want to translate appear as msgid "Show Thing"
, with the file it came from appearing in a comment in the line above. Underneath is an empty msgstr ""
which is where the translator will place the translated messages.
translate/template.pot
fr.po
Now that we've got a template.pot
, our translators can copy it and rename it to fr.po
.
We use fr
since it is the locale code for French, which we'll be using later.
A full list of locale codes can be found on StackOverflow. Make sure you use underscores (fr_CA
) instead of dashes (fr-CA
) if the language you are translating is not reusable for the generic fr
language.
Translators can then start filling out the empty msgstr ""
with translations.
translate/fr.po
Merging updates into fr.po
Our merge.sh
currently only extracts messages into template.pot
. We should next merge any new messages extracted for translation into our fr.po
file.
We'll first filter the translate directory for .po
files.
Then for each .po
file, we'll extract the locale code (fr
) from the filename using the basename
command then striping out the file extension.
We then use another GetText command msgmerge
to generate a new fr.po.new
file based on the old fr.po
and the current template.pot
.
Afterwards, we use sed
to replace the LANGUAGE
placeholder with our current locale code in case our translator left them as is.
When we're done, we overwrite the old fr.po
with fr.po.new
.
translate/merge.sh
Building .mo
Once our fr.po
has been filled out, we can then convert it to a binary .mo
file. So lets get started on our build.sh
script.
The latest copy of my complete `build.sh` script [can be found here](https://github.com/Zren/plasma-applet-lib/blob/master/package/translate/build).
We start with the same code that we used in our merge.sh
script to parse our metadata.desktop
file and get the widget's namespace. We also reuse the same code to iterate the .po
files.
Then we use another GetText command msgfmt
to convert the fr.po
file into a fr.mo
file.
We then make sure a contents/locale/fr/LC_MESSAGES/
folder exists, creating it if it does not.
Then we copy the fr.mo
to the LC_MESSAGES
folder, renaming it to plasma_applet_com.github.zren.helloworld.mo
. Notice that we put plasma_applet_
in front of the widget's namespace.
translate/build.sh
Testing our translations
First make sure you run our build.sh
translation script.
Next we need to install the language pack we want to test. We'll be testing with fr_CA
(Canadian French).
Kubuntu: You'll need to install the
language-pack-fr
package.Manjaro: You'll need to go to System Settings > Locale > Add > Français > Canada. Click Apply, enter your password, then wait for it to generate the language pack.
OpenSUSE: You'll need to install a secondary language using YaST.
Then we need to override the locale environment variables just for our plasmoidviewer
instance. If you run the locale
command, it should list all the environment variables available to override.
In practice, we only need to override LANG="fr_CA.UTF-8"
and another variable it didn't list LANGUAGE="fr_CA:fr"
. If your widget is a clock, then you might also need to override LC_TIME="fr_CA.UTF-8"
.
Reusing other translations
While it is bad practice to link to private code, if you know another widget has translated a string, you can use i18nd(domain, string, ...)
to use translations from that domain. Note that a widget's domain starts with plasma_applet_
, and ends with the widget's X-KDE-PluginInfo-Name
.
Eg: plasma_applet_com.github.zren.helloworld
An example can be found in org.kde.image
's main.qml which reuses the same code for the org.kde.slideshow
.
Last updated