Internationalization - wb8tyw/D-Rats GitHub Wiki

Introduction

D-Rats is being used world-wide so we need to both make sure that internationalization works.

This is to give programmers and translators guidelines on how to do internationalization.

This is my first time internationalizing a python program, so I am learning as I write this.

It does not seem to me to be fair for English programmers to not have to deal with the translation efforts.

So what I am proposing for new messages, that the text in the Python module be an upper case, and the exact case message be entered be put in the English message file.

This would be similar to Android Java programming where the procedure seems to be to provide only string IDs in the code and a database that maps the string IDs to the local language, default being English.

Gettext setup

Found this guide: Translate Python GNU Gettext from phrase.com blog

There are some differences here.

One issue is that as a program evolves, the strings needing translation will change.

So we need a way to track the changes and notify translators.

Future D-rats changes

The config.py has a selection list for the preferred language with 4 selections.

It appears that these selections are hard coded into the program.

Changes needed:

  • The language list should be composed of the list of directories in the locales directory that have translations.
  • This default should be determined by the environment variables that gettext uses to determine the locale.
  • The list should be filtered by the locales that are installed on the system, or should have an indicator that tells the user if the needed locale is installed.
  • The list should have an indicator if the aspell language has been installed for the locale.

Currently I do not know the best way to accomplish this.

Demonstration program

The internationalization_test.py has been added as a test program for getting familiar with the process.

With no translations setup this is the output that the program generates.

$ ./internationalization_test.py
HELLO_WORLD
WARNING:language_test:DEFAULT_NOT_IMPL: DEFAULT ARGUMENTS NOT IMPLEMENTED!
INFO:language_test:UNIT_TEST1: EXECUTING UNIT TEST FUNCTION.

The script build_pot.sh is used to rebuild the base.pot file. This builds a translation file template for all of D-RATs

# Need to make sure a directory is present for your language.
# en for English
mkdir locale/en
mkdir locale/en/LC_MESSAGES

Then the the base.pot file becomes the new D-RATS.po file for the language.

cp locale/base.pot locale/en/LC_MESSAGES/D-RATS.po

The header section needed to be modified, particularly the "charset" needs to be set to UTF-8.

"Project-Id-Version: D-Rats-EV\n"
"Report-Msgid-Bugs-To: https://github.com/maurizioandreotti/D-Rats/issues\n"
"POT-Creation-Date: 2021-10-16 14:15-0500\n"
"PO-Revision-Date: 2021-10-16 04:51+CDT\n"
"Last-Translator: John E. Malmberg <[email protected]>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=n != 1;\n"
#"Language-Team: LANGUAGE <[email protected]>\n"
#"Language: \n"

You will need to add "msgstr" strings for every string that you want replaced.

#: internationalization_test.py:70
msgid "HELLO_WORLD"
msgstr "Hello World"

#: internationalization_test.py:79
msgid "COMMAND ARGS NOT IMPLEMENTED!"
msgstr "Command Arguments not implemented."

#: internationalization_test.py:82
msgid "DEFAULT ARGUMENTS NOT IMPLEMENTED!"
msgstr "Default arguments not implemented."

#: internationalization_test.py:84
msgid "EXECUTING UNIT TEST FUNCTION."
msgstr "Executing unit test function."

Then you need to make/update a message catalog file

pushd locale/en/LC_MESSAGES/D-RATS.po
msgfmt -o D-RATS.mo D-RATS
popd

Now when the program is run, you get the translated results.

./internationalization_test.py
Hello World
WARNING:language_test:DEFAULT_NOT_IMPL: Default arguments not implemented.
INFO:language_test:UNIT_TEST1: Executing unit test function.

Now testing with the "it" locale by manually adding the strings for the internationalize_test.py module run through google translate. The character that is a combination of a '`' and small 'a' was not working so for this example I replaced it with just a small a. The error that I got was:

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 43: ordinal not in range(128)

It turns out that this failure is a bug somewhere in the python2 installed on my test system. It is not present in my python3 packages.

While it can be worked around in the code, it does not seem to be worth it. So at least for the short term, we can put unicode characters in the po files.

pushd locale/it/LC_MESSAGES
msgfmt -o D-RATS.mo D-RATS
popd

And the test:

LANG="it" ./internationalization_test.py
Ciao mondo
WARNING:language_test:DEFAULT_NOT_IMPL: Default arguments not implemented.
INFO:language_test:UNIT_TEST1: Esecuzione della funzione di test dell'unita.

While this is in my fork of D-rats, this can easily be added to the official D-Rats repository.

The catalog mo files are binary so will need to be generated by people creating kits or manually installing D-Rats from source.

Currently D-Rats code does not look like it is properly setup to use these files. This example should give guidance on what changes are needed.

⚠️ **GitHub.com Fallback** ⚠️