2006-05-02 09:31:56 +08:00
|
|
|
from django import http
|
2005-12-04 20:06:16 +08:00
|
|
|
from django.utils.translation import check_for_language, activate, to_locale, get_language
|
|
|
|
from django.utils.text import javascript_quote
|
|
|
|
from django.conf import settings
|
2006-01-19 09:06:12 +08:00
|
|
|
import os
|
|
|
|
import gettext as gettext_module
|
2005-11-04 12:59:46 +08:00
|
|
|
|
|
|
|
def set_language(request):
|
|
|
|
"""
|
|
|
|
Redirect to a given url while setting the chosen language in the
|
|
|
|
session or cookie. The url and the language code need to be
|
2007-09-14 15:33:45 +08:00
|
|
|
specified in the request parameters.
|
|
|
|
|
|
|
|
Since this view changes how the user will see the rest of the site, it must
|
|
|
|
only be accessed as a POST request. If called as a GET request, it will
|
|
|
|
redirect to the page in the request (the 'next' parameter) without changing
|
|
|
|
any state.
|
2005-11-04 12:59:46 +08:00
|
|
|
"""
|
2007-09-16 03:55:48 +08:00
|
|
|
next = request.POST.get('next', None)
|
|
|
|
if not next:
|
|
|
|
next = request.GET.get('next', None)
|
2005-11-04 12:59:46 +08:00
|
|
|
if not next:
|
|
|
|
next = request.META.get('HTTP_REFERER', None)
|
|
|
|
if not next:
|
|
|
|
next = '/'
|
2006-05-02 09:31:56 +08:00
|
|
|
response = http.HttpResponseRedirect(next)
|
2007-09-14 15:33:45 +08:00
|
|
|
if request.method == 'POST':
|
|
|
|
lang_code = request.POST.get('language', None)
|
|
|
|
if lang_code and check_for_language(lang_code):
|
|
|
|
if hasattr(request, 'session'):
|
|
|
|
request.session['django_language'] = lang_code
|
|
|
|
else:
|
|
|
|
response.set_cookie('django_language', lang_code)
|
2005-11-04 12:59:46 +08:00
|
|
|
return response
|
2005-12-04 20:06:16 +08:00
|
|
|
|
|
|
|
NullSource = """
|
|
|
|
/* gettext identity library */
|
|
|
|
|
2006-07-01 11:17:18 +08:00
|
|
|
function gettext(msgid) { return msgid; }
|
|
|
|
function ngettext(singular, plural, count) { return (count == 1) ? singular : plural; }
|
|
|
|
function gettext_noop(msgid) { return msgid; }
|
2005-12-04 20:06:16 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
LibHead = """
|
|
|
|
/* gettext library */
|
|
|
|
|
|
|
|
var catalog = new Array();
|
|
|
|
"""
|
|
|
|
|
|
|
|
LibFoot = """
|
|
|
|
|
|
|
|
function gettext(msgid) {
|
2006-07-01 11:17:18 +08:00
|
|
|
var value = catalog[msgid];
|
|
|
|
if (typeof(value) == 'undefined') {
|
|
|
|
return msgid;
|
|
|
|
} else {
|
|
|
|
return (typeof(value) == 'string') ? value : value[0];
|
|
|
|
}
|
2005-12-04 20:06:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function ngettext(singular, plural, count) {
|
2006-07-01 11:17:18 +08:00
|
|
|
value = catalog[singular];
|
|
|
|
if (typeof(value) == 'undefined') {
|
|
|
|
return (count == 1) ? singular : plural;
|
|
|
|
} else {
|
|
|
|
return value[pluralidx(count)];
|
|
|
|
}
|
2005-12-04 20:06:16 +08:00
|
|
|
}
|
|
|
|
|
2006-07-01 11:17:18 +08:00
|
|
|
function gettext_noop(msgid) { return msgid; }
|
2005-12-04 20:06:16 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
SimplePlural = """
|
2006-07-01 11:17:18 +08:00
|
|
|
function pluralidx(count) { return (count == 1) ? 0 : 1; }
|
2005-12-04 20:06:16 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
InterPolate = r"""
|
|
|
|
function interpolate(fmt, obj, named) {
|
2006-07-01 11:17:18 +08:00
|
|
|
if (named) {
|
2007-08-11 18:51:02 +08:00
|
|
|
return fmt.replace(/%\(\w+\)s/g, function(match){return String(obj[match.slice(2,-2)])});
|
2006-07-01 11:17:18 +08:00
|
|
|
} else {
|
2007-08-11 18:51:02 +08:00
|
|
|
return fmt.replace(/%s/g, function(match){return String(obj.shift())});
|
2006-07-01 11:17:18 +08:00
|
|
|
}
|
2005-12-04 20:06:16 +08:00
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
2006-07-01 11:09:14 +08:00
|
|
|
def null_javascript_catalog(request, domain=None, packages=None):
|
|
|
|
"""
|
|
|
|
Returns "identity" versions of the JavaScript i18n functions -- i.e.,
|
|
|
|
versions that don't actually do anything.
|
|
|
|
"""
|
|
|
|
return http.HttpResponse(NullSource + InterPolate, 'text/javascript')
|
|
|
|
|
2005-12-04 20:06:16 +08:00
|
|
|
def javascript_catalog(request, domain='djangojs', packages=None):
|
|
|
|
"""
|
|
|
|
Returns the selected language catalog as a javascript library.
|
|
|
|
|
|
|
|
Receives the list of packages to check for translations in the
|
|
|
|
packages parameter either from an infodict or as a +-delimited
|
|
|
|
string from the request. Default is 'django.conf'.
|
|
|
|
|
|
|
|
Additionally you can override the gettext domain for this view,
|
|
|
|
but usually you don't want to do that, as JavaScript messages
|
|
|
|
go to the djangojs domain. But this might be needed if you
|
|
|
|
deliver your JavaScript source from Django templates.
|
|
|
|
"""
|
|
|
|
if request.GET:
|
2007-04-26 21:30:48 +08:00
|
|
|
if 'language' in request.GET:
|
2005-12-04 20:06:16 +08:00
|
|
|
if check_for_language(request.GET['language']):
|
|
|
|
activate(request.GET['language'])
|
|
|
|
if packages is None:
|
|
|
|
packages = ['django.conf']
|
|
|
|
if type(packages) in (str, unicode):
|
|
|
|
packages = packages.split('+')
|
2005-12-04 20:57:46 +08:00
|
|
|
packages = [p for p in packages if p == 'django.conf' or p in settings.INSTALLED_APPS]
|
2005-12-04 20:06:16 +08:00
|
|
|
default_locale = to_locale(settings.LANGUAGE_CODE)
|
|
|
|
locale = to_locale(get_language())
|
|
|
|
t = {}
|
|
|
|
paths = []
|
2005-12-07 04:30:56 +08:00
|
|
|
# first load all english languages files for defaults
|
2005-12-04 20:06:16 +08:00
|
|
|
for package in packages:
|
|
|
|
p = __import__(package, {}, {}, [''])
|
|
|
|
path = os.path.join(os.path.dirname(p.__file__), 'locale')
|
|
|
|
paths.append(path)
|
2005-12-07 04:30:56 +08:00
|
|
|
catalog = gettext_module.translation(domain, path, ['en'])
|
|
|
|
t.update(catalog._catalog)
|
|
|
|
# next load the settings.LANGUAGE_CODE translations if it isn't english
|
|
|
|
if default_locale != 'en':
|
|
|
|
for path in paths:
|
|
|
|
try:
|
|
|
|
catalog = gettext_module.translation(domain, path, [default_locale])
|
2006-01-19 09:06:12 +08:00
|
|
|
except IOError:
|
2005-12-07 04:30:56 +08:00
|
|
|
catalog = None
|
|
|
|
if catalog is not None:
|
|
|
|
t.update(catalog._catalog)
|
|
|
|
# last load the currently selected language, if it isn't identical to the default.
|
2005-12-04 20:06:16 +08:00
|
|
|
if locale != default_locale:
|
|
|
|
for path in paths:
|
|
|
|
try:
|
|
|
|
catalog = gettext_module.translation(domain, path, [locale])
|
2006-01-19 09:06:12 +08:00
|
|
|
except IOError:
|
2005-12-04 20:06:16 +08:00
|
|
|
catalog = None
|
|
|
|
if catalog is not None:
|
|
|
|
t.update(catalog._catalog)
|
|
|
|
src = [LibHead]
|
|
|
|
plural = None
|
2007-04-26 21:30:48 +08:00
|
|
|
if '' in t:
|
2005-12-07 04:30:56 +08:00
|
|
|
for l in t[''].split('\n'):
|
|
|
|
if l.startswith('Plural-Forms:'):
|
|
|
|
plural = l.split(':',1)[1].strip()
|
2005-12-04 20:06:16 +08:00
|
|
|
if plural is not None:
|
|
|
|
# this should actually be a compiled function of a typical plural-form:
|
|
|
|
# Plural-Forms: nplurals=3; plural=n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2;
|
|
|
|
plural = [el.strip() for el in plural.split(';') if el.strip().startswith('plural=')][0].split('=',1)[1]
|
|
|
|
src.append('function pluralidx(n) {\n return %s;\n}\n' % plural)
|
|
|
|
else:
|
|
|
|
src.append(SimplePlural)
|
|
|
|
csrc = []
|
|
|
|
pdict = {}
|
|
|
|
for k, v in t.items():
|
|
|
|
if k == '':
|
|
|
|
continue
|
|
|
|
if type(k) in (str, unicode):
|
|
|
|
csrc.append("catalog['%s'] = '%s';\n" % (javascript_quote(k), javascript_quote(v)))
|
|
|
|
elif type(k) == tuple:
|
2007-04-26 21:30:48 +08:00
|
|
|
if k[0] not in pdict:
|
2005-12-04 20:06:16 +08:00
|
|
|
pdict[k[0]] = k[1]
|
|
|
|
else:
|
|
|
|
pdict[k[0]] = max(k[1], pdict[k[0]])
|
|
|
|
csrc.append("catalog['%s'][%d] = '%s';\n" % (javascript_quote(k[0]), k[1], javascript_quote(v)))
|
|
|
|
else:
|
|
|
|
raise TypeError, k
|
|
|
|
csrc.sort()
|
|
|
|
for k,v in pdict.items():
|
|
|
|
src.append("catalog['%s'] = [%s];\n" % (javascript_quote(k), ','.join(["''"]*(v+1))))
|
|
|
|
src.extend(csrc)
|
|
|
|
src.append(LibFoot)
|
|
|
|
src.append(InterPolate)
|
|
|
|
src = ''.join(src)
|
2006-05-02 09:31:56 +08:00
|
|
|
return http.HttpResponse(src, 'text/javascript')
|