PySM: creating language files
Creating language files in a project has been proven _very_ simple in our pySM project.
How the files are stored are not really the issue. The issue is how to most easily create the link between the display (strings) and the language.
For sake of demonstration:
lang_en = {'no_such_server': "No such server",
'unavailable': "unavailable",
'extra_blabla': "some more shizzle", }
language = lang_en
print("%(no_such_server)s" % language)
Now, this is for a so called “english” language array. But if said array were to be translated to another language (e.g. dutch):
lang_nl = {'no_such_server': "Server bestaat niet",
'unavailable': "Niet beschikbaar",
'extra_blabla': "Wat extra zever", }
language = lang_nl
print("%(no_such_server)s" % language)
So. Python proves his power _again_ in using this simple feature in everyones advantage!
# update:
It has been noted by my loyal friend that language files might be empty or be non-existent. Which is indeed quite correct.
The solution for that would be to create a standard “english” (since most people understand that) dictionary, and have another dictionary “merge” into the default dict.
The advantage of such approach is that when there’s an update, not all language files / dictionaries should update immediatelly. They can remain outdated, in a way. Because the merge would only replace the already existing keys in the old dict with the new values from the new dict.
Example:
# using above lang_en and lang_nl
language = lang_en # establish default, lang_en would then be hardcoded
language.update(lang_nl) # merge both dictionaries
print("%(no_such_server)s" % language) # Server bestaat niet
If at any chance an update is established and a key doesn’t exist in lang_nl, the default value in lang_en would be used.
Thanks @Tim for helping me here :)
You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

Comments are closed.