Translations
Now that you've learned about Meson, the next step is to make your app able to be translated to different languages. The first thing you need to know is how to mark strings in your code as translatable. Here's an example:
See the difference? We marked the string as translatable by adding _()
around it. Go back to your project and make all your strings translatable by adding _()
.
Now we have to make some changes to our Meson build system and add a couple new files to describe which files we want to translate and which languages we want to translate into.
Open up your "meson.build" file and add these lines below your project declaration:
Remove the lines that install your .desktop and metainfo files and replace them with the following:
The
merge_file
method combines translating and installing files, similarly to how theexecutable
method combines building and installing your app. You might have noticed that this method has both aninput
argument and anoutput
argument. We can use this instead of therename
argument from theinstall_data
method.Still in this file, add the following as the last line:
You might have noticed in step 2 that the
merge_file
method has aninput
andoutput
. We're going to append the additional extension.in
to our .desktop and .metainfo.xml files so that this method can take the untranslated files and produce translated files with the correct names.We use the
git mv
command here instead of renaming in the file manager or withmv
so thatgit
can keep track of the file rename as part of our revision history.Now, Create a directory named "po" in the root folder of your project. Inside of your po directory you will need to create another "meson.build" file. This time, its contents will be:
Inside of "po" create another file called "POTFILES" that will contain paths to all of the files you want to translate. For us, this looks like:
We have one more file to create in the "po" directory. This file will be named "LINGUAS" and it should contain the two-letter language codes for all languages you want to provide translations for. As an example, let's add German and Spanish
Now it's time to go back to your build directory and generate some new files using Terminal! The first one is our translation template or
.pot
file:After running this command you should notice a new file in the po directory containing all of the translatable strings for your app.
Now we can use this template file to generate translation files for each of the languages we listed in the LINGUAS file with the following command:
You should notice two new files in your po directory called
de.po
andes.po
. These files are now ready for translators to localize your app!Last step. Don't forget to add all of the new files we created in the po directory to git:
That's it! Your app is now fully ready to be translated. Remember that each time you add new translatable strings or change old ones, you should regenerate your .pot and po files using the -pot
and -update-po
build targets from the previous two steps. If you want to support more languages, just list them in the LINGUAS file and generate the new po file with the -update-po
target. Don't forget to add any new po files to git!
If you're having trouble, you can view the full example code here on GitHub
Translators Comments
Sometimes detailed descriptions in the context of translatable strings are necessary for disambiguation or to help in the creation of accurate translations. For these situations use /// TRANSLATORS:
comments.
Last updated