Writing an applilcation that can easily adapt to any language

Mikael Olofsson mikael at isy.liu.se
Thu Mar 2 02:35:31 EST 2006


Chance Ginger wrote:
> I am rather new at Python so I want to get it right. What I am doing
> is writing a rather large application with plenty of places that 
> strings will be used. Most of the strings involve statements of
> one kind or another. 
> 
> I would like to make it easy for the support people to port the
> application from one language to another, German, Spanish, etc. 
> Rather than make them search all over for the strings to replace
> is there a library that I can use to make this all easier? I
> keep thinking of resources in Java, as an example. Is there 
> anything like it in Python?

Brono's suggestion is most certainly very good. If you are looking for 
something more light-weight, you might enjoy my module language that I 
include below. The idea is to have everything language-specific in one 
module, that is imported everywhere it is needed in my application. Consider

     import language
     fileText = language.texts['file']

After this, fileText is either 'File' or 'Arkiv' depending on if 
language.lang is 'en' or 'sv'. The docstrings should be enough 
documentation to use the module. perhaps you might want to split the 
module up into two modules, one containing the class, and one containing 
the texts.

/MiO



And here is the module:

# -*- coding: cp1252 -*-

"""Module language:
The most important object made available here is the following:

     handleLanguage
         A class that handles information in different languages. See the
         doc-string of this class for more information.

     texts
         A dictionary containing objects of the class handleLanguage.

     lang
         A string representing chosen language.

     availableLanguages
         A list of strings representing available languages. The first
         item is the default fallback language if lang fails.

"""

lang='en'
availableLanguages=[lang]


class handleLanguage(dict):
     """class handleLanguage:
     A class that handles information in different languages as strings.
     This class is instantiated as follows:

         foo=handleLanguage(sv='blah',en='blahblah')

     After that we have foo['sv']=='blah' and foo['en']=='blahblah'.
     Also, the languages 'sv' and 'en' will have been appended to the
     module level list availableLanguages if they were not in that list
     before.

     Now let foo be any instance of this class. The real funtionality of
     this class is that str(foo) depends on the module variables lang and
     availableLanguages. More precicely str(foo)==foo[x] where x chosen
     according to the following.

         if lang in foo: x=lang
         elif availableLanguages[0] in foo: x=availableLanguages[0]
         elif 'en' in foo: x='en'
         else: x=foo.keys()[0].

     If none of this works, then we have str(foo)=='??', which only
     happens if foo does not contain any language.
     """
     def __init__(self,**kwargs):
         dict.__init__(self,**kwargs)
         for key in kwargs:
             if key not in availableLanguages:
                 availableLanguages.append(key)
     def __str__(self):
         try:
             return self[lang]
         except KeyError:
             if availableLangages[0] in self:
                 return self[availableLangages[0]]
             elif 'en' in self:
                 return self['en']
             elif self:
                 return self[self.keys()[0]]
             else:
                 return '??'
     def __add__(self,other):
         if not isinstance(other,dict):
             foo,other=self.__coerce__(other)
         new=handleLanguage(**dict(other))
         new.update(self)
         for key in self:
             if key in other:
                 new[key]=self[key]+other[key]
         return new
     def __radd__(self,other):
         if not isinstance(other,dict):
             foo,other=self.__coerce__(other)
         new=handleLanguage(**dict(other))
         new.update(self)
         for key in self:
             if key in other:
                 new[key]=other[key]+self[key]
         return new
     def __coerce__(self,other):
         new=handleLanguage()
         for key in self:
             new[key]=str(other)
         return self,new

texts={
     # Common texts
     'appName':handleLanguage(
         sv='Uppgiftshanteraren',
         en='TaskManager',
         ),
     'foo':handleLanguage(
         sv='foo',
         en='foo',
         ),
     # File menu
     'file':handleLanguage(
         sv='Arkiv',
         en='File',
         ),
     'help':handleLanguage(
         sv='Hjälp',
         en='Help',
         ),
     'open':handleLanguage(
         sv='Öppna',
         en='Open',
         ),
     }

if __name__=="__main__":
     keys=texts.keys()
     keys.sort()
     for lang in availableLanguages:
         print lang
         for key in keys:
             print '   ',key+':',texts[key]
         print
     print 'Done'



More information about the Python-list mailing list