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