简体   繁体   中英

Language Localization of Khan Academy

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.

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM