4
\$\begingroup\$

On a game server, programmed in python, I want to be able to select language specific expression, according to the user's preference. Provided a 'name tag' and a 'language identifier' I want a function to return the appropriate text for the specified tag in the given language, for example

> print(foo("turn left","de"))
Bitte nach links abbiegen
> print(foo("turn left","en"))
Turn left please

I hope you get the point.

Is there something available for python which can provide a solution? I am not looking for gettext or something which uses the computer's locale setting for the language! I am looking for a solution which might work similar, but works as given in the example.

Or is it better to code something like this by oneself? How then to store the language phrases in a best way? Just in a big text file? A data base? ...?

\$\endgroup\$
  • \$\begingroup\$ How many strings will you need? Do you need to have more than one available at a time, or only one per play session? \$\endgroup\$ – Vaillancourt Oct 2 '15 at 12:58
  • \$\begingroup\$ The server might need to respond to different players all in different languages. \$\endgroup\$ – Alex Oct 2 '15 at 13:00
  • 1
    \$\begingroup\$ Typically, the strings are in a file (or DB) stored on the client, and the server sends ids of text to display, not text. The client, upon starting, loads the text values associated with the user preferred language, then use a key-> value (text-id -> text in this case) function to retrieve the correct strings for ids supplied by the server or by the interface. You want to send text to your clients? \$\endgroup\$ – Vaillancourt Oct 2 '15 at 13:13
6
\$\begingroup\$

I would create a directory containing language text files. The directory could look like this:

en.txt

0 "Start game"
1 "Load game"
2 "Enable cannons"
3 "Join server"
4 "Kill the enemy"

fr.txt

 0 "Démarrer le jeu"
 1 "Load game"
 2 "Activer canons"
 3 "Rejoignez serveur"
 4 "Tuer l'ennemi"

de.txt

 0 "Spiel starten"
 1 "Spiel laden"
 2 "Enable Kanonen"
 3 "Join-Server"
 4 "Töte den Feind"

ch.txt

0 "開始遊戲"
1 "載入遊戲"
2 "啟用炮"
3 "加入服務器"
4 "殺死敵人"

Now make hashmap, with the language names as keys, and a String array as object. In each string array you load up the values and they are now ready to use.

A method to get the values. (Pseudo code)

public String getText(String languageCode, int messageId) {
    return languages.get(languageCode).get(messageId);
}

Initialization method (Pseudo code)

HashMap<String, HashMap<Integer, String>> languages;

public init(String directory) {
    for(file; each file in directory) {
        HashMap<Integer, String> languageArray = load file contents and add to array;
        languages.get(languageCode).add(languageArray);
}

This can of course both be implemented on the client side(for less bandwidth) or server side for more customize ability.

\$\endgroup\$
  • 2
    \$\begingroup\$ I'll just point out that unless you use tools to manage your language IDs, using integers can prove difficult to debug, in the resource files and in the code. \$\endgroup\$ – Vaillancourt Oct 2 '15 at 13:43
  • \$\begingroup\$ "Load game" can be translated as "Charger une partie" in french \$\endgroup\$ – DrakaSAN Oct 2 '15 at 16:08
  • \$\begingroup\$ @DrakaSAN I just used translate. Have no idea why it didn't translate that. \$\endgroup\$ – KaareZ Oct 2 '15 at 18:08
0
\$\begingroup\$

Here is a JSON-based solution, with the same concept exposed by Kaarez

languages.json

{
 "fr" : {
   "turn left" : "tournez à gauche s'il vous plaît" 
 },
 "en" : {
   "turn left" : "turn left please"
 }
}

And then, using the json module:

import json

with open('languages.json') as fd:
    lang = json.load(fd)

print(lang["fr"]["turn left"])

However, note that a JSON file could be bigger than a txt file.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.