1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 MediaGoblin contributors. See AUTHORS.
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.
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.
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/>.
19 from babel
.localedata
import exists
20 from babel
.support
import LazyProxy
22 from mediagoblin
import mg_globals
29 TRANSLATIONS_PATH
= pkg_resources
.resource_filename(
30 'mediagoblin', 'i18n')
33 def locale_to_lower_upper(locale
):
35 Take a locale, regardless of style, and format it like "en-us"
38 lang
, country
= locale
.split('-', 1)
39 return '%s_%s' % (lang
.lower(), country
.upper())
41 lang
, country
= locale
.split('_', 1)
42 return '%s_%s' % (lang
.lower(), country
.upper())
47 def locale_to_lower_lower(locale
):
49 Take a locale, regardless of style, and format it like "en_US"
52 lang
, country
= locale
.split('_', 1)
53 return '%s-%s' % (lang
.lower(), country
.lower())
58 def get_locale_from_request(request
):
60 Figure out what target language is most appropriate based on the
63 request_form
= request
.GET
or request
.POST
65 if request_form
.has_key('lang'):
66 return locale_to_lower_upper(request_form
['lang'])
68 accept_lang_matches
= request
.accept_language
.best_matches()
70 # Your routing can explicitly specify a target language
71 matchdict
= request
.matchdict
or {}
73 if matchdict
.has_key('locale'):
74 target_lang
= matchdict
['locale']
75 elif request
.session
.has_key('target_lang'):
76 target_lang
= request
.session
['target_lang']
77 # Pull the first acceptable language
78 elif accept_lang_matches
:
79 target_lang
= accept_lang_matches
[0]
80 # Fall back to English
84 return locale_to_lower_upper(target_lang
)
88 def setup_gettext(locale
):
90 Setup the gettext instance based on this locale
92 # Later on when we have plugins we may want to enable the
93 # multi-translations system they have so we can handle plugin
96 # TODO: fallback nicely on translations from pt_PT to pt if not
98 if SETUP_GETTEXTS
.has_key(locale
):
99 this_gettext
= SETUP_GETTEXTS
[locale
]
101 this_gettext
= gettext
.translation(
102 'mediagoblin', TRANSLATIONS_PATH
, [locale
], fallback
=True)
104 SETUP_GETTEXTS
[locale
] = this_gettext
106 mg_globals
.setup_globals(
107 translations
=this_gettext
)
110 # Force en to be setup before anything else so that
111 # mg_globals.translations is never None
115 def pass_to_ugettext(*args
, **kwargs
):
117 Pass a translation on to the appropriate ugettext method.
119 The reason we can't have a global ugettext method is because
120 mg_globals gets swapped out by the application per-request.
122 return mg_globals
.translations
.ugettext(
126 def lazy_pass_to_ugettext(*args
, **kwargs
):
128 Lazily pass to ugettext.
130 This is useful if you have to define a translation on a module
131 level but you need it to not translate until the time that it's
134 return LazyProxy(pass_to_ugettext
, *args
, **kwargs
)
137 def pass_to_ngettext(*args
, **kwargs
):
139 Pass a translation on to the appropriate ngettext method.
141 The reason we can't have a global ngettext method is because
142 mg_globals gets swapped out by the application per-request.
144 return mg_globals
.translations
.ngettext(
148 def lazy_pass_to_ngettext(*args
, **kwargs
):
150 Lazily pass to ngettext.
152 This is useful if you have to define a translation on a module
153 level but you need it to not translate until the time that it's
156 return LazyProxy(pass_to_ngettext
, *args
, **kwargs
)
159 def fake_ugettext_passthrough(string
):
161 Fake a ugettext call for extraction's sake ;)
163 In wtforms there's a separate way to define a method to translate
164 things... so we just need to mark up the text so that it can be
165 extracted, not so that it's actually run through gettext.