Ticket #1086: dLocalize.py

File dLocalize.py, 2.7 kB (added by shawkat@samdu.uz, 1 year ago)

dLocalize.py

Line 
1 # -*- coding: utf-8 -*-
2 import gettext
3 import locale
4 import os
5 import dabo
6
7 __app_initialized = False
8 __localedir = "locale"
9 __dabo_trans=None
10 __app_trans=None
11
12 def _(s):
13     global defLang, __app_initialized
14    
15     if not __app_initialized:
16         try:
17             app = dabo.dAppRef
18         except AttributeError:
19             app = None
20            
21         if app:
22             setLanguage(domain='app',localedir=os.path.join(app.HomeDirectory,__localedir))
23             __app_initialized = True
24             __app_trans.add_fallback(__dabo_trans)
25        
26     if __app_initialized:
27         return __app_trans.gettext(s)
28     else:
29         return __dabo_trans.gettext(s)
30
31 def n_(s):
32     """ Use it if you want to tell translation service about string
33     but don't want to translate it inplace.
34     """
35     global defLang
36     return s
37
38
39 def setLanguage(lang=None, charset=None,domain="dabo",localedir=None):
40     global defLang, defCharset, __dabo_trans, __app_trans
41    
42     if localedir is None:
43         localedir = os.path.join(os.path.split(dabo.__file__)[0], __localedir)
44            
45     if charset is None:
46         charset = defCharset
47        
48     if lang is None:
49         lang = defLang
50     else:
51         lang = lang.lower()
52         # It might be the full name instead of the two-letter abbreviation
53         if lang not in getLanguages():
54             try:
55                 lang = {"english": "en",
56                         "spanish": "es", "espanol": "es", "espa?ol": "es",
57                         "french": "fr", "francais": "fr", "fran?ais": "fr",
58                         "german": "de", "deutsch": "de",
59                         "italian": "it", "italiano": "it",
60                         "portuguese": "pt", "portugu?se": "pt",
61                         "russian": "ru"}[lang]
62             except KeyError:
63                 pass
64        
65     dabolocalefile=gettext.find(domain, localedir, languages=[lang], all=True)
66    
67     if not dabolocalefile:
68         print "Gettext file for language %s was not found , falling back to english" %lang
69
70         dabolocalefile=gettext.find(domain, localedir, languages=['en'], all=True)
71    
72         if not dabolocalefile:
73             raise IOError, "Neither english nor '%s' language was found " % lang
74    
75         defLang = "en"
76         defCharset = "ISO8859-1"
77
78     if domain=="dabo":
79         __dabo_trans=gettext.translation(domain, localedir,languages=[lang], codeset=charset)
80         defLang = lang
81     else:
82         __app_trans=gettext.translation(domain, localedir,languages=[lang], codeset=charset)
83        
84 defLang, defCharset = locale.getdefaultlocale()
85
86 if defLang is None:
87     defLang = "en"
88 else:
89     defLang = defLang[:2]
90 if defCharset is None:
91     defCharset = "ISO8859-1"
92    
93 setLanguage()
94
95 print _("framework localization test")