multi-language text

Hi,

I want to implement different languages in my game and now I am looking for some efficient ways to do that.

I have read a bit about html using scripts
or based of on text file where all lines are been saved

something like that I am looking for

example:

(just an part from an other script)


# command to creat a 'NEW Lobby'
        if data[0] == '§NEWL§':
            print(data[1])
            max_user = data[1][0]
            lobby_name = data[1][1]
            lobby_privacy = data[1][2]
            lobby_pw = data[1][3]
            lobby_location = 'XX' ## not ready to use

            try:
                global_lobbies = [v for k, v in self.lobbies_specs.items() if lobby_name in v]
                if global_lobbies == []:
                    if 2 <= max_user <= 10:
                        if lobby_pw == None:
                            if lobby_privacy[1] == 'open':
                                created = self.creat_lobby(req_addr, max_user, lobby_name, lobby_location, lobby_privacy, lobby_pw)
                                if created:
                                    print('new_lobby will be added')
                                    to_send = self.pack_data(('§ACK§', 'you creat succesfully your own lobby!'))
                                    self.sendSingle(to_send, req_addr)
                                else:
                                    raise Exception("some internal error has occoured to add you to your lobby!")
                            else:
                                 raise Exception('you can not set an password at an open lobby, use close lobbies instead!')
                        elif lobby_privacy[1] == 'close':
                            if len(lobby_pw) > 3 or lobby_pw != None:
                                created = self.creat_lobby(req_addr, max_user, lobby_name, lobby_location, lobby_privacy, lobby_pw)
                                if created:
                                    print('new_lobby will be added')
                                    to_send = self.pack_data(('§ACK§', 'you creat succesfully your own lobby!'))
                                    self.sendSingle(to_send, req_addr)
                                else:
                                    raise Exception("some internal error has occoured to add you to your lobby!")
                            else:
                                raise Exception('your current lobby password is to short!')
                        else:
                            raise Exception('you can not set an password at an open lobby, use close lobbies instead!')
                    else:
                        raise Exception('your max_users setting is not between 2-10 players!')
                else:
                    raise Exception('the name %s is given to another lobby please choose another name!' % lobby_name)
            except Exception as e:
                to_send = self.pack_data(('§ERROR§', str(e)))
                print(e)
                self.sendSingle(to_send, req_addr)

for each (in that case) Exception I want an language key and the line key and then the right language and line

I recommend splitting that code into some functions. I struggle to understand that much indentation. It would take dedicated effort which I do not feel like putting in.

Language support. I generally have a file, something like “language.py” it looks something like:


LANGUAGES = {
'english':{
    'ok':'OK',
    'cancel':'CANCEL',
    'connect_error':'A fatal connection error has occured'
},
'gibberish':{
    'ok':'%^',
    'cancel':'wervcajt',
    'connect_error': 'weguhb.h uhwegu'
}

current_language = 'english'

def get_message(slug):
    return LANGUAGES[current_language][slug]

def set_language(lang):
    global current_language
    current_language = lang

Then in other code you can do:


import language

language.get_message('ok')

And:


import language
language.set_language('gibberish')

Obviously the language dict could be constructed in some other way. It could be read from file etc. The key is to seperate what the message is about (eg I am raising a connect_error) from it’s contents (the message ‘A fatal connection error has occured’)

Yes I should do that. It messed up when it grows at complexity.

as you mean to get for each language an dictionary with the same predefined keys and than set only the value to the different language.

maybe I should do that too

but what is the benefit of doing it with an xml file (I have often see it, maybe at older games)?

not that I have to use it only for understanding. I think i would do it with an .cfg

you can even use an .ini setup defining the languages, user selects language and you grab the language selection out of an .ini.
using a file means players can adjust it, use it in-game no one can change it but the maker of the game. (or simply add more languages)

What style/type you going to use is up to you.
if you like .ini setup or xml, look at configParser, it can parse xml/ini files.

I suggest to use .properties files as it is used by Java (see Resource bundle). There are a few editors that makes it easy to create the according language files (just text files). You get one file per language.

The access is as with all localization libraries via text key and locale (current language).

The presentation is a bit more complex as you need to call the localisation every time you want to show text. Additionally you need to refresh existing texts when the current language changes.

Btw. I would not use localization when raising errors. Errors are very technical and not a proper output towards the end user.

Hello the_cannibal.

I have made an email account in an overlay scene, where you can select the language in the start menu. It’s made with a dictionary where you need to put the text in the different languages in, and the right one will be selected with a variable saved in a txt file.
Here is the link: Email3lang https://blenderartists.org/forum/showthread.php?408743-Email-account-with-3-languages-and-manual&p=3111625&viewfull=1#post3111625
Greetings
achisp