root/tags/dabo-0.3.2/dLocalize.py

Revision 724, 1.9 kB (checked in by paul, 4 years ago)

Committed Vladimir's dLocalize patch, with a few minor style mods.
Also removed the old loc name (I grepped for occurances just in case
and found none).

Vladimir, if you would provide some starting mo files so we can test
this out, that would be great.

  • Property svn:eol-style set to native
Line 
1 import gettext, os
2 import dabo
3 from dabo.__version__ import version
4
5 __domain = "dabo"
6 __localedir = "locale"
7
8 def __translation(domain, version=None, dirs=[]):
9     """ Try to find
10     """
11     dirs = [os.path.join(d, __localedir) for d in dirs]
12     dirs.append(None) # tell to search in system dirs also
13     domains = [domain]
14     version and domains.insert(0, "-".join([domain, version]))
15
16     for domain in domains:
17         for dir in dirs:
18             try:
19                 return gettext.translation(domain, localedir=dir)
20             except IOError:
21                 pass
22     return None
23
24 # set default Dabo translation. File is searched under cwd()/locale/$LANG/LC_MESSAGES
25 # and in system default places.
26 __trans = __translation(__domain, version["version"], ["."]) or gettext.NullTranslations()
27
28 __app_initialized = False
29
30 def __add_apptrans():
31     global __trans
32     try:
33         app = dabo.dAppRef
34     except AttributeError:
35         app = None
36     if app:
37         appname, appver = [app.getAppInfo(k) for k in ("appName", "appVersion")]
38         if appname:
39             appname = appname.lower()
40             apptrans = __translation(appname, appver, [app.HomeDirectory])
41             if apptrans:
42                 apptrans.add_fallback(__trans)
43                 __trans = apptrans
44                 __app_initialized = True
45
46 def _(s):
47     """ Default localization service. Translation is based on default
48     gettext interface. Translation is returned in Unicode.
49
50     Messages are searched first in application translations and in Dabo ones after.
51     
52     Translation files are searched in
53     application.HomeDirectory/locale and system default locale
54     directories accoding to gettext conventions(files must be
55     placed under ${LANG}/LC_MESSAGES subdirectory. File basename must
56     be appName or appName-appVersion in lower case.
57     
58     Default appName is "dabo".
59     """
60     # application initialized, try to install messages
61     if not __app_initialized:
62         __add_apptrans()   
63     return __trans.ugettext(str(s))
64
65    
66 def n_(s):
67     """ Use it if you want to tell translation service about string
68     but don't want to translate it inplace.
69     """
70     return s
Note: See TracBrowser for help on using the browser.