|
|
# Prepare source code
|
|
|
|
|
|
## For PHP
|
|
|
|
|
|
Create nonexistent `pgettext` function for context translations.
|
|
|
|
|
|
```php
|
|
|
if (!function_exists('pgettext')) {
|
|
|
function pgettext($context, $msgId)
|
|
|
{
|
|
|
$contextString = "{$context}\004{$msgId}";
|
|
|
$translation = dcgettext('messages', $contextString, LC_MESSAGES);
|
|
|
if ($translation == $contextString)
|
|
|
return $msgId;
|
|
|
else
|
|
|
return $translation;
|
|
|
}
|
|
|
}
|
|
|
```
|
|
|
|
|
|
## For everything
|
|
|
|
|
|
Use `_($text)` and `pgettext($context, $text)` to mark string to be translated.
|
|
|
|
|
|
# Create initial .pot
|
|
|
|
|
|
Use the following to create the initial .pot (template file)
|
|
|
|
|
|
```bash
|
|
|
$ find -name '*.php' -exec xgettext --keyword="pgettext:1c,2" -c --from-code=UTF-8 -o locale/templates/messages.pot {} \+
|
|
|
```
|
|
|
|
|
|
# Create initial .po file from .pot
|
|
|
|
|
|
```bash
|
|
|
$ msginit -l de -i locale/templates/messages.pot -o locale/de/LC_MESSAGES/messages.po
|
|
|
```
|
|
|
|
|
|
# Update .po file from new .pot
|
|
|
|
|
|
```bash
|
|
|
$ msgmerge -U -N locale/de/LC_MESSAGES/messages.po locale/templates/messages.pot
|
|
|
```
|
|
|
|
|
|
# Translate .po files
|
|
|
|
|
|
Translate the .po files via `poedit`
|
|
|
|
|
|
# Convert .po files to .mo files
|
|
|
|
|
|
When using `poedit` this is done when saving the translation. Else use the following:
|
|
|
|
|
|
```bash
|
|
|
$ msgfmt locale/de/LC_MESSAGES/messages.po -o locale/de/LC_MESSAGES/messages.mo
|
|
|
```
|
|
|
|
|
|
# Script for creating .po files
|
|
|
|
|
|
This script creates .po-files for php and javascript:
|
|
|
|
|
|
```bash
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
cd "$(dirname $0)/htdocs"
|
|
|
|
|
|
find -name '*.php' -exec xgettext -L PHP --keyword="pgettext:1c,2" -c --from-code=UTF-8 -o locale/templates/php.pot {} \+
|
|
|
find -name '*.js' -exec xgettext -L JavaScript -c --from-code=UTF-8 -o locale/templates/js.pot {} \+
|
|
|
|
|
|
for i in locale/*; do
|
|
|
if [ -d "$i" ] && [ "locale/templates" != "$i" ]; then
|
|
|
PO_LANGUAGE=${i##*/}
|
|
|
echo "Language: $PO_LANGUAGE"
|
|
|
PO_PHP="$i/LC_MESSAGES/php.po"
|
|
|
if [ -f "$PO_PHP" ]; then
|
|
|
msgmerge -U -N "$PO_PHP" "locale/templates/php.pot"
|
|
|
else
|
|
|
msginit -l $PO_LANGUAGE -i "locale/templates/php.pot" -o "$PO_PHP"
|
|
|
fi
|
|
|
|
|
|
PO_JS="$i/LC_MESSAGES/js.po"
|
|
|
if [ -f "$PO_JS" ]; then
|
|
|
msgmerge -U -N "$PO_JS" "locale/templates/js.pot"
|
|
|
else
|
|
|
msginit -l $PO_LANGUAGE -i "locale/templates/js.pot" -o "$PO_JS"
|
|
|
fi
|
|
|
fi
|
|
|
done
|
|
|
``` |
|
|
\ No newline at end of file |