| 1 |
--- |
| 2 |
short-description: Localization with GNU Gettext |
| 3 |
... |
| 4 |
|
| 5 |
# Localisation |
| 6 |
|
| 7 |
Localising your application with GNU gettext takes a little effort but |
| 8 |
is quite straightforward. We'll create a `po` subdirectory at your |
| 9 |
project root directory for all the localisation info. |
| 10 |
|
| 11 |
## Generating .pot and .po files |
| 12 |
|
| 13 |
In your main meson.build file include the `po` subdirectory in the build process. |
| 14 |
|
| 15 |
subdir('po') |
| 16 |
|
| 17 |
In this `po` subdirectory we need: |
| 18 |
- `LINGUAS`: Space separated list of languages |
| 19 |
- `POTFILES`: List of source files to scan for translatable strings. |
| 20 |
- `meson.build`: Localization specific Meson file |
| 21 |
|
| 22 |
### LINGUAS |
| 23 |
|
| 24 |
File with space separated list of languages. A sample LINGUAS might look like this. |
| 25 |
|
| 26 |
aa ab ae af |
| 27 |
|
| 28 |
### POTFILES |
| 29 |
|
| 30 |
File that lists all the source files that gettext should scan in order |
| 31 |
to find strings to translate. The syntax of the file is one line per |
| 32 |
source file and the line must contain the relative path from source |
| 33 |
root. A sample POTFILES might look like this. |
| 34 |
|
| 35 |
src/file1.c |
| 36 |
src/file2.c |
| 37 |
src/subdir/file3.c |
| 38 |
include/mything/somefile.h |
| 39 |
|
| 40 |
### meson.build |
| 41 |
|
| 42 |
Localization specific Meson file. It imports and uses the `i18n` |
| 43 |
module. If not defined before it needs to define the `GETTEXT_PACKAGE` |
| 44 |
global. |
| 45 |
|
| 46 |
```meson |
| 47 |
i18n = import('i18n') |
| 48 |
# define GETTEXT_PACKAGE |
| 49 |
add_project_arguments('-DGETTEXT_PACKAGE="intltest"', language:'c') |
| 50 |
i18n.gettext(meson.project_name()) |
| 51 |
``` |
| 52 |
|
| 53 |
The first command imports the `i18n` module that provides gettext |
| 54 |
features. The fourth line does the actual invocation. The first |
| 55 |
argument is the gettext package name. This causes two things to |
| 56 |
happen. The first is that Meson will generate binary mo files and put |
| 57 |
them to their proper locations when doing an install. The second is |
| 58 |
that it creates a build rule to regenerate the main pot file. If you |
| 59 |
are using the Ninja backend, this is how you would invoke the rebuild. |
| 60 |
|
| 61 |
### generate .pot file |
| 62 |
|
| 63 |
Then we need to generate the main pot file. The potfile can have any |
| 64 |
name but is usually the name of the gettext package. Let's say the |
| 65 |
project is called *intltest*. In this case the corresponding pot file |
| 66 |
would be called `intltest.pot`. |
| 67 |
|
| 68 |
Run the following command from your build folder to generate the pot |
| 69 |
file. It is recommended to inspect it manually afterwards and fill in |
| 70 |
e.g. proper copyright and contact information. |
| 71 |
|
| 72 |
```console |
| 73 |
$ meson compile intltest-pot |
| 74 |
``` |
| 75 |
|
| 76 |
### generate .po files |
| 77 |
|
| 78 |
For each language listed in the array above we need a corresponding |
| 79 |
`.po` file. Those can be generated by running the following command |
| 80 |
from your build folder. |
| 81 |
|
| 82 |
```console |
| 83 |
$ meson compile intltest-update-po |
| 84 |
``` |