Multi-language or localisation, example project

Previous topic - Next topic

BdR

Here is an example of a program which supports multiple languages, in the same way as I used in my game Snake Slider. Here is how I added multi-language support, hopefully someone can also benefit from this. :good:

At first I programmed the game with just English texts. Then I decided to add multilanguage, so I went through all source files and collected the text-strings and put them in a column in an Excel sheet. Then I added a column with translated Dutch texts as a first test. Then I wrote a VBA macro which would output the texts to an ini-file with numbers as keys. For each language there is a separate ini-file, so for example the english.ini file would like this:
Code (glbasic) Select

[words]
0=Start game
1=Editor
2=Options
3=Yes
4=No
etc.


The vba macro also generates a GLBasic list of constants which contains all the numbers, so this is kind of like enumeration. I could paste this into my source file, it looks for example like this:
Code (glbasic) Select

CONSTANT ML_START_GAME = 0
CONSTANT ML_EDITOR = 1
CONSTANT ML_OPTIONS = 2
CONSTANT ML_YES = 3
CONSTANT ML_NO = 4
etc.

Then I wrote a GLBasic function that would read the items from the ini-file into an array of strings called MultiLang$[]. So at program start-up (or when the user changes the language) the program reads all texts from the corresponding language file into the array.

When all this was ready, I changed my source files and replaced all the fixed English strings to MultiLang$[] array items, using the constants to get a certain string from the array, for example like this:
Code (glbasic) Select
// original command..
PRINT "Start game", 100, 100, TRUE

// ..changed to multi-language
PRINT MultiLanguage$[ML_START_GAME], 100, 100, TRUE

The Excel file and macro help to manage and edit the translated texts. If I used "magic numbers" in GLBasic to locate the string items, like MultiLang$[2], I would have to update many lines of code everytime I inserted or deleted a translated text item. But now, I can just generate new language ini-files and an updated list of constants and everything still works the same. :)

See attachement for an example project plus an Excel file with the vba macro.

[attachment deleted by admin]