Ticket #1086: dLocalize.2.py

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

tried to deal with tabs/spaces, some comments, some kind of testing

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     """ Use it to get translated strings.   
14     Localization files of app should be in under its' locale directory
15     Localization files od dabo framework should be under dabo's locale directory
16     """
17     global defLang, __app_initialized
18    
19     if not __app_initialized:
20            
21         # is application initialized ? if yes, we could know its name
22         try:
23             app = dabo.dAppRef
24         except AttributeError:
25             app = None
26            
27         if app:
28             # TODO: domain should be appname, app.mo is hardcoded for now
29             setLanguage(domain = 'app', localedir = os.path.join(app.HomeDirectory,__localedir))
30             __app_initialized = True
31             __app_trans.add_fallback(__dabo_trans)
32             # application's localization is inplace now, dabo's localization is a fallback
33        
34     if __app_initialized:
35         return __app_trans.gettext(s)
36     else:
37         # application's localization is not ready for now, use only dabo's localization
38         return __dabo_trans.gettext(s)
39
40 def n_(s):
41     #TODO: wouldn't it be better, if we will use something like _("string",False) in _ function ???
42     # i.e. one more argument for _function, telling by default to translate strings ?
43     # def _(s, translate=True):
44    
45     """ Use it if you want to tell translation service about string
46     but don't want to translate it inplace.
47     """
48    
49     return s
50
51
52 def setLanguage(lang = None, charset = None, domain = "dabo", localedir = None):
53     """ Use it if you want to switch to another localizations than you default.
54     You should call it twice - once for dabo framework, and once for app
55     Yet to be checked
56     """
57    
58     global defLang, defCharset, __dabo_trans, __app_trans
59    
60     #TODO: we should search system localizations directory as well
61      
62     if localedir is None:
63         localedir = os.path.join(os.path.split(dabo.__file__)[0], __localedir)
64            
65     if charset is None:
66         charset = defCharset
67        
68     if lang is None:
69         lang = defLang
70     else:
71         lang = lang.lower()
72         # It might be the full name instead of the two-letter abbreviation
73         if lang not in getLanguages():
74             try:
75                 lang = {"english": "en",
76                         "spanish": "es", "espanol": "es", "espa?ol": "es",
77                         "french": "fr", "francais": "fr", "fran?ais": "fr",
78                         "german": "de", "deutsch": "de",
79                         "italian": "it", "italiano": "it",
80                         "portuguese": "pt", "portugu?se": "pt",
81                         "russian": "ru"}[lang]
82             except KeyError:
83                 pass
84        
85     dabolocalefile=gettext.find(domain, localedir, languages = [lang], all = True)
86    
87     if not dabolocalefile:
88         print "Gettext file for language %s was not found , falling back to english" %lang
89
90         dabolocalefile = gettext.find(domain, localedir, languages = ['en'], all = True)
91    
92         if not dabolocalefile:
93             raise IOError, "Neither english nor '%s' language was found " % lang
94    
95         #TODO: for now, user apllication is FORCED to have localizations, even if developer wants single language
96    
97         defLang = "en"
98         defCharset = "ISO8859-1"
99
100
101     #TODO: handling of situation, when either dabo or app is missing localization.
102      
103     if domain == "dabo":
104         __dabo_trans = gettext.translation(domain, localedir, languages = [lang], codeset = charset)
105         defLang = lang
106     else:
107         __app_trans = gettext.translation(domain, localedir, languages = [lang], codeset = charset)
108        
109 defLang, defCharset = locale.getdefaultlocale()
110
111 if defLang is None:
112     defLang = "en"
113 else:
114     defLang = defLang[:2]
115 if defCharset is None:
116     defCharset = "ISO8859-1"
117    
118 setLanguage()
119
120 if __name__ == "__main__":
121     print 'user locale is ', locale.getdefaultlocale()
122     print 'framework locale is ', defLang, defCharset
123     print _("framework localization test")