473,387 Members | 1,515 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Writing an applilcation that can easily adapt to any language

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?

Peace,
Chance.
Mar 1 '06 #1
2 1159
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?


http://www.python.org/doc/2.4.2/lib/module-gettext.html

HTH
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Mar 1 '06 #2
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'
Mar 2 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: mark1822 | last post by:
Hi, I am trying to figure out if I should learn C and C++ to write CGI programs, or if I should just use PHP for my web site. I want to write a high traffic website that would call executibles...
40
by: post400 | last post by:
Hi, there is another famous book 'Writing solid code' but does it apply to Python ? Or it's usable only by Microsoft C programmers ? The author seems to be an ex-Microsoft guy ! Thanks ,...
385
by: Xah Lee | last post by:
Jargons of Info Tech industry (A Love of Jargons) Xah Lee, 2002 Feb People in the computing field like to spur the use of spurious jargons. The less educated they are, the more they like...
6
by: hpy_awad | last post by:
I am writing stings ((*cust).name),((*cust).address)to a file using fgets but rabish is being wrote to that file ? Look to my source please and help me finding the reason why this rabish is being...
9
by: 100 | last post by:
Has anybody read Steve Maguire's book "Writing solid code"? Do you think that the ideas in this book are not applicable in c# language? Does anybody find if(2 == i) istead of if(i == 2) as...
19
by: Marco | last post by:
FYI: Guidelines for writing efficient C/C++ code http://www.embedded.com/showArticle.jhtml?articleID=184417272 any comments?
2
by: Gustaf | last post by:
I read some data from an XML file. Some datas need to be converted to decimals. The program is to be used in any country, so sometimes the decimal sign is "." and sometimes ",". How can I make...
7
by: Matt Kowalczyk | last post by:
Hello, I am working on a compression project and I want to write ASCII characters using the minimum amount of bits. Since I will be writing ASCII characters from 0-127 I only need 7 bits to...
0
by: mcc99 | last post by:
Amazing, I surfed around to find a simple utility that could list files recursively from a given top folder down through it, writing out the path and filename in simple \path\file_name form, without...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.