Gettext

From MapbenderWiki

Jump to: navigation, search

Static data in PHP files may be translated via gettext. All strings that will be translated must be called with the PHP gettext method, example

echo _("Not an integer value"); //_() is short for gettext()

BEWARE: Mapbender uses a function _mb() to wrap _() (scenario: you might want to disable i18n even though gettext is installed)

echo _mb("Not an integer value"); //_mb() is a wrapper for _()

.po file

The .po file is a text file containing keys and values. The keys are the input values to the _() function, like "Not an integer value"

The .po file segemnt will look like this

msgid "Not an integer value."
msgstr "Kein Integer-Wert."

.po files are stored in /resources/locale/<locale_name>/LC_MESSAGES/, example

/resources/locale/de_DE/LC_MESSAGES/

to create a .po file for a new language (for example german), use xgettext

xgettext -p <folder>/resources/locale/de_DE/LC_MESSAGES/ -o Mapbender.po -L php -j --keyword=_mb -n --from-code utf-8 <folder>/http/php/*.php

-p locale path, target directory
-o output-file
-L language
-j merge with existing po file
-n add metadata
--from-code encoding of php file
--keyword name of the function in the source code 
  • translations can be done via POEdit

.mo file

The .mo file is a binary representation of the text file. It is required to compile the .mo file from the .po file on the system where Mapbender is installed. There is no sense in shipping it with Mapbender. The .po file is automatically compiled into an .mo file if you save the .po file in POEdit.

Compile the .mo file via command line like this

msgfmt resources/locale/de_DE/LC_MESSAGES/Mapbender.po -o resources/locale/de_DE/LC_MESSAGES/Mapbender.mo

msgfmt is part of the gettext package, install it via

apt-get install gettext

if necessary.

  • for an example, see module print_PDF.php
gted (Gettext editor) is a nice Eclipse Plug-In for translating PO files.
Personal tools