Hi
I would like to support additional languages in my PyS60 program (menu, messages, etc).
Is there standard/recommended way to do this ?
Thanks !
Marcelo Barros
Hi
I would like to support additional languages in my PyS60 program (menu, messages, etc).
Is there standard/recommended way to do this ?
Thanks !
Marcelo Barros
Hi Marcelo,
Well, there is no specific way of doing that, but I think the easiest one is saving one file for each language containing the translated strings (one string at each line)
Then, user selects the language and you open the file and make a list with all the strings:
By the way, .lang is a cool extension to use for languagesCode:f = open('E:\\Folder\\english.lang', 'r') lang = f.read().split('\r\n') f.close()
Sorry if it's too confusing. When I have some free time I'll try to add an example on the Wiki for this
Hope you understood,
Rafael.
I recommend using Python for this too. No need to create your own parser and you can have the power of Python to process the localizations if required.
See Mobile Web Server's tutorial on how to do this. You can create a module for each language and simply import the correct module dynamically when the language changes.
Thanks GameDude\Rafael, you were cristal clear. I will try something joining both ideas !
Aah, such beautiful code !!! Now I can add multi-language support for my sw, tooI've been thinking about it, but it always slipped lower in priority list, because it was too much trouble. Now all problems are solved!
Seriously, anyone planning to support multiple languages, please take a real good look at that code! It's well worth the effort. Maybe it's not too obvious at first look, but it will save you a lot of time, debugging and trouble later.
Happy,
--jouni who can't even recall previous time he woke up to see such beautiful code![]()
I guess, you didn't get enough time last year, to check out the Featured Articles on the Forum Nokia Wiki![]()
Pankaj Nathani
www.croozeus.com
Wow, looks great! Ever since the Great FN UI Upgrade, I've had problems finding anything. When thinking back, I use use FN now a lot less that I used to. However that new Features Articles page looks like a real improvement! I'll have to bookmark it and check regularly
In the meanwhile, here's some sample code about how to do text localization:
http://jouni.miettunen.googlepages.com/localizationtest
Cheers,
--jouni
Nice demo Jouni, may I steal (with references, of course) some peaces ? ;-)
I would be extremely delighted about that! Feel free to use as much or little as you wish, with any modifications and completely without any references. It's ok, no problems! It was released for other people to use, not for me
...if you find any improvements or corrections, naturally I'd be interestedIf I get some energy peak, I might add support for localized graphics, pretty simple and straightforward to do. However more useful would be support for partial translations! It would require some thinking first, so I'm not going to do it any time soon. The short thinking times I can get will be used for thinking something else - unless my next app would support multiple languages, hmph
Cheers,
--jouni
Hi Folks,
Using some ideas collected here, I did an implementation for multiple languages. It supports missing strings (partial translation) and use the same strategy described here by Jouni (importing modules with language stuff). Partial translation feature was based on introspection (hasattr, __dict__, __getattribute__ and __setattr__).
Code: http://wordmobi.googlecode.com/files...ale_demo_1.zip
Unpack and copy all files into e:\python (phone memory card). Afterwards, run wm_locale_demo.py
For those following this thread and with Jouni code in their minds, I think only the class below is enough to understand how it works.
Code:# -*- coding: utf-8 -*- __all__ = [ "Locale" ] class Loc_Data(object): "Translation data holder" pass class Default(object): "Default language support" def __init__(self): self.loc = Loc_Data() self.loc.zero = u'Zero' self.loc.one = u'One' self.loc.two = u'Two' self.loc.three = u'Three' self.loc.four = u'Four' self.loc.five = u'Five' self.loc.six = u'Six' self.loc.seven = u'Seven' self.loc.eight = u'Eight' self.loc.nine = u'Nine' self.loc.change_language = u'Change Language' self.loc.english_us = u'English (USA)' self.loc.finnish = u'Finnish' self.loc.hungarian = u'Hungarian' self.loc.portuguese_br = u'Portuguese (Brazil)' self.loc.about = u'About' self.loc.exit = u'Exit' class Locale(Default): "Multiple language support class" LOC_MODULE = "wm_locale_%s" def __init__(self,lang = ""): "Load all locale strings for one specific language or default if empty" Default.__init__(self) try: lang_mod = __import__( self.LOC_MODULE % ( lang ) ) except ImportError: pass else: self.merge_locale(lang_mod) def merge_locale(self, lang_mod): "Merge new location string into default locale" # replace existing strings and keep old ones # if it is missing in the locale module for k,v in self.loc.__dict__.iteritems(): if hasattr(lang_mod,k): nv = lang_mod.__getattribute__(k) self.loc.__setattr__(k,nv)
Great,
You obviously know python, unlike I! Code looks fantastic, have to check and test it carefully. If I read correctly, you do reset variables before trying to load new language == exactly what has to be done.
Well, now I have no excuses left, my next app simply must support multiple languages
Delighted,
--jouni
Strategy documented here:
http://wiki.forum.nokia.com/index.php/LocalizationExampleForPyS60
Thanks !
Gargi Das- http://gargidas.blogsot.com
Forum Nokia Python Wiki
Learn Python at http://mobapps.org/PyS60