Monthly Archives: December 2017

Python and Pygame Internationalization

Introduction

Pygame is a game-developing module of Python. It can be regarded as a platform or engine as well. The developer can employ many useful methods from it when developing 2D or 3D games. In this article, I will illustrate details of the internationalization workflow in Python and Pygame.

The internationalization workflow can be roughly divided into the following steps:

  • string wrapping
  • generate .pot file
  • translation in Poedit
  • save .mo and .po files into the correct folder
  • use pygettext method in codes
  • connect pygettext method with button function

Without further ado, let’s dive into details of these steps.

String wrapping

Unsurprisingly, the first step we need to do is to wrap strings for translation. Wrapping strings is relatively simple in Python (Pygame). We can simply use “_(“ and “)” to complete this task. In python, we do not need to create property files to store keys and values for different languages.

Generate .pot file

After wrapping strings, the second step is to generate a pot file for translation. Python has a script called Pygettext for internationalization. Normally, you can find Pygettext file in a similar path with the following C:\Users\Sibin Lu\AppData\Local\Programs\Python\Python36-32\Tools\i18n. The only thing one should keep in mind is to put the file for translation and Pygettext under the same folder when generating a pot file.

When you’re ready, you need to type a command line in command prompt to generate a pot file (see pic below).

Translation in Poedit

We can then translate the pot file in Poedit. This is the simplest step in the internationalization process. However, it is by no means an easy task if you would like to render a premium translation. Moreover, we need to be careful when choosing the language code.

Save .mo and .po files into the correct folder

After completing the translation, we can generate a mo and a po file which will be using in our program. Creating a correct folder structure is the first thing to do. Under the same folder of the program, we need to create a “Locale” folder, then “language code (es, zh, bg, etc.)”, and finally a “LC_MESSAGES”. Our mo and po files will go under the “LC_MESSAGES” folder.

Use Pygettext method in codes

The next step is to employ Pygettext method in the program. The method is as follows:

ru = gettext.translation(‘alien_invasion’, localedir=’locale’, languages=[‘ru’])

ru.install()

we can see from the above method that we need to specify the file, the directory and the Language code folder to get translated texts by simply calling this method. Howeverm it only works for a single language. In order to achieve multiple languages internationalization, we must combine Pygettext method with the button function.

Connect pygettext method with button function

In this program, we have a button function that can help us obtain the internationalization goal.

From the above picture, we get to know what things we can do with this function. There are several parameters in this function. Apparently, the action parameter is what we need. We could pass through the Pygettext method to it as arguments (see the pic below).

Then, we could switch between languages by simply clicking language buttons.

This is one of the many ways to do internationalization, and there are many other methods that are worthwhile to explore as well.