/:customize-your-mezzanine-with-Django-middleware-and-JavaScript-AngularJS-for-i18n-multilingual.html
Last active
August 29, 2015 14:00
-
-
Save andretw/11340242 to your computer and use it in GitHub Desktop.
This Gist is for my post: http://www.andretw.com/2014/04/customize-your-mezzanine-with-Django-middleware-and-JavaScript-AngularJS-for-i18n-multilingual.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This Gist is for my post: http://www.andretw.com/2014/04/customize-your-mezzanine-with-Django-middleware-and-JavaScript-AngularJS-for-i18n-multilingual.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# List of middleware classes to use. Order is important; in the request phase, | |
# these middleware classes will be applied in the order given, and in the | |
# response phase the middleware will be applied in reverse order. | |
MIDDLEWARE_CLASSES = ( | |
"mezzanine.core.middleware.UpdateCacheMiddleware", | |
"django.contrib.sessions.middleware.SessionMiddleware", | |
"django.middleware.locale.LocaleMiddleware", | |
# Add the following line into MIDDLEWARE_CLASSES | |
"plusdiff.middleware.SelectLangMiddleware", | |
"django.contrib.auth.middleware.AuthenticationMiddleware", | |
"django.middleware.common.CommonMiddleware", | |
"django.middleware.csrf.CsrfViewMiddleware", | |
"django.contrib.messages.middleware.MessageMiddleware", | |
"mezzanine.core.request.CurrentRequestMiddleware", | |
"mezzanine.core.middleware.RedirectFallbackMiddleware", | |
"mezzanine.core.middleware.TemplateForDeviceMiddleware", | |
"mezzanine.core.middleware.TemplateForHostMiddleware", | |
"mezzanine.core.middleware.AdminLoginInterfaceSelectorMiddleware", | |
"mezzanine.core.middleware.SitePermissionMiddleware", | |
# Uncomment the following if using any of the SSL settings: | |
# "mezzanine.core.middleware.SSLRedirectMiddleware", | |
"mezzanine.pages.middleware.PageMiddleware", | |
"mezzanine.core.middleware.FetchFromCacheMiddleware", | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<!-- Don't forget to inclue AngularJS, jQuery, etc. --> | |
<script src="{% static "langpack/" %}{{ LANG }}.js"></script> | |
</head> | |
<body> | |
${ _("This Sentence needs to be translated.") }$ | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.config ($interpolateProvider)-> | |
$interpolateProvider.startSymbol('${') | |
$interpolateProvider.endSymbol('}$') | |
# JavaScript version by js2coffee | |
# | |
# module.config(function($interpolateProvider) { | |
# $interpolateProvider.startSymbol('${'); | |
# return $interpolateProvider.endSymbol('}$'); | |
# }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# i18n | |
$rootScope._ = (key) -> | |
if LANGPACK? and LANGPACK[key]? | |
return LANGPACK[key] | |
else | |
return key | |
# JavaScript version by js2coffee | |
# | |
# $rootScope._ = function(key) { | |
# if ((typeof LANGPACK !== "undefined" && LANGPACK !== null) && (LANGPACK[key] != null)) { | |
# return LANGPACK[key]; | |
# } else { | |
# return key; | |
# } | |
# }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
######################################################################################### | |
# A middleware which sets user's locale by the query param "?lang=zh_TW" (locale name) # | |
######################################################################################### | |
from django.utils import translation | |
from django.utils.translation.trans_real import get_supported_language_variant | |
from django.shortcuts import redirect | |
# for add_query_parameters | |
from urlparse import urlparse, parse_qsl, urlunparse | |
from urllib import urlencode | |
def add_query_parameters(url, query_parameters): | |
returned_url = list(urlparse(url)) | |
returned_query_parms = dict(parse_qsl(returned_url[4])) | |
if query_parameters: | |
for qs in query_parameters: | |
if query_parameters[qs] is not None: | |
returned_query_parms[qs] = query_parameters[qs] | |
returned_url[4] = urlencode(returned_query_parms) | |
return urlunparse(returned_url) | |
class SelectLangMiddleware: | |
''' | |
Simple version for making sure all the pages have the query parameter: lang. | |
You can modify it to fit your requirements. | |
''' | |
def process_request(self, request): | |
# admin console will not be supported for i18n | |
if request \ | |
and not str(request.path).startswith('/static') \ | |
and not str(request.path).startswith('/admin') \ | |
and not str(request.path).startswith('/api'): | |
try: | |
default_lang = translation.get_language() | |
lang = request.GET.get("lang", None) | |
if lang: | |
request.LANG = lang | |
else: | |
request.LANG = default_lang | |
redirect_to = add_query_parameters(request.build_absolute_uri(), { "lang" : default_lang }) | |
return redirect(redirect_to) | |
except Exception as e: | |
print "Error Occurred", e |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var LANGPACK = { | |
"This Sentence needs to be translated.": "這一行需要被翻譯" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment