Maybe a dumb question

Chris Liechti cliechti at gmx.net
Tue Sep 10 19:10:00 EDT 2002


Fabien HENON <fabien.henon at caramail.com> wrote in 
news:3D7E6B0E.4060709 at caramail.com:

> I have written an editor for Povray using Python, Tkinter and Pmw
> 
> It is available at :
> 
> http://pyvon.sourceforge.net
> 
> The software includes one main file and 6 others which are for the 
> language customization. People can choose between French, English, 
> German,...
> All the translations for these languages are stored in files called 
> menu_francais.py, menu_english.py, menu_deutsch.py ....when the editor 
> is started.
> 
> I would like to clean the directory a bit and put all these 'languages 
> files' into a sub-directory called Lang.
> How do I tell python to look into a particular sub-directory when 
> importing a file.
> 
> I tried the os.path.join syntax, but to no avail.
> 
> Below is the bit of code that imports the language file from the current 
>   working directory :
> 
> 
> try :
>    homedir = os.path.expanduser('~')
>    fd = open(homedir+"/.pyvonrc", 'r')
>    for line in fd.readlines():
>       if line.startswith("Language :"):
>      line=line[11:]          
>      if   line.startswith("francais"):from menu_francais import *
>          elif line.startswith("english"):from menu_english import *
>          elif line.startswith("deutsch"):from menu_deutsch import *
>          ...............

do you have considered the ConfigParser module? it would make it easy to 
parse a config file...

and for your problem, if you avoid "from x import *" , which isn't 
recomended for broader usage anyway, you could use something like that:


config = ConfigParser.ConfigParser()
config.read([os.path.join(homedir, "/.pyvonrc")]) 
try:
    	lang = __import__(config.get("Environment", "Language"))
except ImportError:
    	print "Wanrning: could not load language file, using default"
    	lang = __import__("english.py")


which would require an entry like that in the config file:
--------
[Environment]
Language=deutsch
--------

you could also look at other ways to localize your program.
i think "locale" and "i18n" should help in a google search.

chris



-- 
Chris <cliechti at gmx.net>




More information about the Python-list mailing list