Merge remote-tracking branch 'refs/remotes/gandaro/369-thread-gettext'
[mediagoblin.git] / mediagoblin / tools / translate.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Affero General Public License for more details.
13 #
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17 import gettext
18 import pkg_resources
19 from babel.localedata import exists
20 from babel.support import LazyProxy
21
22 from mediagoblin import mg_globals
23
24 ###################
25 # Translation tools
26 ###################
27
28
29 TRANSLATIONS_PATH = pkg_resources.resource_filename(
30 'mediagoblin', 'i18n')
31
32
33 def locale_to_lower_upper(locale):
34 """
35 Take a locale, regardless of style, and format it like "en-us"
36 """
37 if '-' in locale:
38 lang, country = locale.split('-', 1)
39 return '%s_%s' % (lang.lower(), country.upper())
40 elif '_' in locale:
41 lang, country = locale.split('_', 1)
42 return '%s_%s' % (lang.lower(), country.upper())
43 else:
44 return locale.lower()
45
46
47 def locale_to_lower_lower(locale):
48 """
49 Take a locale, regardless of style, and format it like "en_US"
50 """
51 if '_' in locale:
52 lang, country = locale.split('_', 1)
53 return '%s-%s' % (lang.lower(), country.lower())
54 else:
55 return locale.lower()
56
57
58 def get_locale_from_request(request):
59 """
60 Figure out what target language is most appropriate based on the
61 request
62 """
63 request_form = request.GET or request.POST
64
65 if request_form.has_key('lang'):
66 return locale_to_lower_upper(request_form['lang'])
67
68 # Your routing can explicitly specify a target language
69 matchdict = request.matchdict or {}
70
71 if matchdict.has_key('locale'):
72 target_lang = matchdict['locale']
73 elif request.session.has_key('target_lang'):
74 target_lang = request.session['target_lang']
75 # Pull the first acceptable language or English
76 else:
77 # WebOb recently changed how it handles determining best language.
78 # Here's a compromise commit that handles either/or...
79 if hasattr(request.accept_language, "best_matches"):
80 accept_lang_matches = request.accept_language.best_matches()
81 if accept_lang_matches:
82 target_lang = accept_lang_matches[0]
83 else:
84 target_lang = 'en'
85 else:
86 target_lang = request.accept.best_match(
87 request.accept_language, 'en')
88
89 return locale_to_lower_upper(target_lang)
90
91 SETUP_GETTEXTS = {}
92
93 def setup_gettext(locale):
94 """
95 Setup the gettext instance based on this locale
96 """
97 # Later on when we have plugins we may want to enable the
98 # multi-translations system they have so we can handle plugin
99 # translations too
100
101 # TODO: fallback nicely on translations from pt_PT to pt if not
102 # available, etc.
103 if locale in SETUP_GETTEXTS:
104 this_gettext = SETUP_GETTEXTS[locale]
105 else:
106 this_gettext = gettext.translation(
107 'mediagoblin', TRANSLATIONS_PATH, [locale], fallback=True)
108 if exists(locale):
109 SETUP_GETTEXTS[locale] = this_gettext
110
111 mg_globals.thread_scope.translations = this_gettext
112
113
114 # Force en to be setup before anything else so that
115 # mg_globals.translations is never None
116 setup_gettext('en')
117
118
119 def pass_to_ugettext(*args, **kwargs):
120 """
121 Pass a translation on to the appropriate ugettext method.
122
123 The reason we can't have a global ugettext method is because
124 mg_globals gets swapped out by the application per-request.
125 """
126 return mg_globals.thread_scope.translations.ugettext(
127 *args, **kwargs)
128
129
130 def lazy_pass_to_ugettext(*args, **kwargs):
131 """
132 Lazily pass to ugettext.
133
134 This is useful if you have to define a translation on a module
135 level but you need it to not translate until the time that it's
136 used as a string.
137 """
138 return LazyProxy(pass_to_ugettext, *args, **kwargs)
139
140
141 def pass_to_ngettext(*args, **kwargs):
142 """
143 Pass a translation on to the appropriate ngettext method.
144
145 The reason we can't have a global ngettext method is because
146 mg_globals gets swapped out by the application per-request.
147 """
148 return mg_globals.thread_scope.translations.ngettext(
149 *args, **kwargs)
150
151
152 def lazy_pass_to_ngettext(*args, **kwargs):
153 """
154 Lazily pass to ngettext.
155
156 This is useful if you have to define a translation on a module
157 level but you need it to not translate until the time that it's
158 used as a string.
159 """
160 return LazyProxy(pass_to_ngettext, *args, **kwargs)
161
162
163 def fake_ugettext_passthrough(string):
164 """
165 Fake a ugettext call for extraction's sake ;)
166
167 In wtforms there's a separate way to define a method to translate
168 things... so we just need to mark up the text so that it can be
169 extracted, not so that it's actually run through gettext.
170 """
171 return string