How to create an internationalized Google App Engi

2019-01-22 04:28发布

I would like to provide my Python GAE website in the user's own language, using only the tools available directly in App Engine. For that, I would like to use GNU gettext files (.po and .mo files).

Has someone successfully combined Python Google App Engine and gettext files? If so, could you please provide the steps you used?

I had started a discussion in GAE's Google group, but haven't been able to extract from it how I'd like to do it: I don't want to add external dependencies, like Babel (suggested in the discussion). I want to use plain vanilla Google App Engine, so no manual update of Django or this kind of stuff.

At first, I will start using the language sent by the browser, so no need to manually force the language by using cookies etc. However, I might add a language changing feature later, once the basic internationalization works.

As a background note to give you more details about what I'm trying to do, I would like to internationalize Issue Tracker Tracker, an open source application I've hosted on Launchpad. I plan to use Launchpad's translation platform (explaining why I'd like to use .mo files). You can have a look at the source code in it's Bazaar branch (sorry no link due to stackoverflow spam prevention limit for new users...)

Thanks for helping me advance on this project!

2条回答
神经病院院长
2楼-- · 2019-01-22 04:42

You can use the Django internationalisation tool, like explained here.

They are also saying that there is no easy way to do this.

I hope that helps you :)

查看更多
在下西门庆
3楼-- · 2019-01-22 05:03

As my needs were simple, I used a simple hack instead of (unavailable) gettext. I created a file with string translations, translate.py. Approximately like this:

en={}
ru={}

en['default_site_title']=u"Site title in English"
ru['default_site_title']=u"Название сайта по-русски"

Then in the main code I defined a function which returns a dictionary with translations into the most suitable language from the list (the first one to have a translation is used or English):

import translate

def get_messages(languages=[]):
    msgs=translate.en
    for lang in languages:
        if hasattr(translate,lang):
            msgs=getattr(translate,lang)
            break
    return msgs

Usage:

msgs = get_messages(["it","ru","en"])
hi = msgs['hello_message'] % 'yourname'

I also defined a helper function which extracts a list of languages from Accept-Language header.

It's not the most flexible solution, but it doesn't have any external dependencies and works for me (in a toy project). I think translate.py may be generated automatically from gettext files.

In case you want to see more, my actual source is here.

查看更多
登录 后发表回答