[Solved]-Language Localization of Khan Academy

1👍

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

Leave a comment