Created
October 25, 2011 09:23
-
-
Save memphys/1312005 to your computer and use it in GitHub Desktop.
GAE I18n
This file contains hidden or 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
# Creating dir for translation files | |
mkdir -p /path/to/myapp/conf/locale | |
cd /path/to/myapp | |
# Creating language file by scanning app directory for text marked for translation. | |
# It will appear in /path/to/conf/locale/en/LC_MESSAGES/django.po | |
# Run command for each needed language | |
PYTHONPATH=/path/to/googleappengine/lib/django/ | |
/path/to/googleappengine/lib/django/django/bin/make-messages.py -l en | |
# Compile messages to django.mo files after finishing translation | |
# Usually local app engine server need restart to changes take place | |
PYTHONPATH=/path/to/python/googleappengine/lib/django/ | |
/path/to/googleappengine/python/lib/django/django/bin/compile-messages.py | |
# Command to update message | |
# It will update django.po files. Translate them and compile again. | |
PYTHONPATH=/path/to/googleappengine/python/lib/django/ | |
/path/to/googleappengine/python/lib/django/django/bin/make-messages.py -a | |
This file contains hidden or 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
# -*- coding: utf-8 -*- | |
import Cookie | |
import re | |
import os | |
from google.appengine.ext import webapp | |
from google.appengine.ext.webapp import template | |
from google.appengine.api import users | |
os.environ['DJANGO_SETTINGS_MODULE'] = 'conf.settings' | |
from django.conf import settings | |
# Force Django to reload settings | |
settings._target = None | |
from django.utils import translation | |
class IndexPage(webapp.RequestHandler): | |
def reset_language(self): | |
supported = dict(settings.LANGUAGES) | |
language = language = self.request.cookies.get('django_language') | |
if language and language in supported: | |
pass | |
else: | |
language = settings.LANGUAGE_CODE | |
translation.activate(language) | |
self.request.LANGUAGE_CODE = translation.get_language() | |
self.response.headers['Content-Language'] = translation.get_language() | |
return language | |
def get(self, param=None): | |
if param in ['en', 'ru']: | |
cookie = Cookie.SimpleCookie() | |
cookie['django_language'] = param | |
cookie['django_language']['expires'] = 365*24*60*60 | |
h = re.compile('^Set-Cookie: ').sub('', cookie.output(), count=1) | |
self.response.headers.add_header('Set-Cookie', str(h)) | |
self.reset_language() | |
self.redirect('/') | |
curLang = self.reset_language() | |
path = os.path.join(os.path.dirname(__file__), '../../templates/index.html') | |
self.response.out.write(template.render(path, { | |
"curLang": curLang, | |
"langs": settings.LANGUAGES, | |
})) |
This file contains hidden or 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
# conf/settings.py | |
USE_I18N = True | |
# Valid languages | |
LANGUAGES = ( | |
('ru', 'RUS'), | |
('en', 'ENG'), | |
) | |
# This is a default language | |
LANGUAGE_CODE = 'ru' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment