I am currently working on language localization of Khan Academy, I have downloaded the source 8051 from Google Code . After survey information and viewing code online, the project is made using jinja2 as the templating language. I can use babel to accomplish my work.
With the following work, I can finally enable {%trans%}
and {%endtrans%}
tag parse-able by the template engine with following modification:
in webapp2_extra/jinja2.py:
from django.utils import translation
env.install_gettext_translations(translation)
in config_jinja2.py
-- put following line
"extensions": ['jinja2.ext.i18n']
However, my translated template of *.mo
and *.po
(from pybabel) does not correctly translate tag within value in to destined language. I am thinking the babel integration should came from webapp2_extra.i18n.py, but I do not know how to enable it.
As few posts in Google mentioned that following code might work:
from webapp2_extras import i18n
env.install_gettext_translations(i18n)
However, it fails because it does not recognize {%trans%}
tag.
So does anyone have the experience working on the same problem or has any suggestion to jinja2 i18n problem?
Appreciate any suggestions.
Khan Academy teaches JavaScript and SQL.
Khan Academy has been translated into other languages. You can see the full list of languages and how much has been translated here. In the footer (at the bottom) of the Khan Academy site, you can select the language of your choice.
Supporting over 50 languages and counting Help us translate our content into the world's languages.
Here is a module that works for me (translates {% trans %}
markup inside a jinja2 template).
main.py
import webapp2
from webapp2_extras import i18n
from jinja2 import FileSystemLoader, Environment
env = Environment(loader=FileSystemLoader('/path/to/my/templates'),
extensions=['jinja2.ext.i18n'])
env.install_gettext_translations(i18n)
class HelloWorld(webapp2.RequestHandler):
def _find_locale(self):
#needs customization
lang = self.request.accept_language.best_match(('en-us', 'fr'))
if ('fr' in lang):
return 'fr_FR'
return 'en_US'
def get(self):
i18n.get_i18n().set_locale(self._find_locale())
template = env.get_template('hello.html')
self.response.write(template.render())
config = {'webapp2_extras.i18n': {'translations_path': './i18n'}}
app = webapp2.WSGIApplication([
('/', HelloWorld),
], config=config, debug=True)
def main():
from paste import httpserver
httpserver.serve(app, host='127.0.0.1', port='8080')
if __name__ == '__main__':
main()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With