views:

755

answers:

2

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  A: 

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

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

I hope that helps you :)

Joschua
Thanks for the link, I had already tried to use this in my previous attempt, but couldn't make it actually work. I've tried again last night, and while I managed to get rid of any exception, there is still nothing translated... I'll continue to try to make it work, because apparently it must work, as there is an example app! I'll keep you informed if I succeed.
Emilien
I've finally been able to internationalize my application! I tried following the explanations on the link you sent, but something was not working, so I read the code sample that's linked from the article, and copy/pasting from the actual code, it worked! Thanks again for the link! In case anyone's interested, you can have a look at the actual changes I made to my app: http://bazaar.launchpad.net/~itt-devs/issuetrackertracker/main/revision/130
Emilien
A: 

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.

jetxee
Thanks for your detailed explanation. I had thought about using this kind of approach, but I'd really like to be able to harness the power of all the translators that are active in Launchpad, so I'd need to use standard gettext. I'd use this kind of solution only as a last resort, but thanks though!
Emilien
I think it would be nice to write a ‘compiler’ from gettext files to such python code.
jetxee
Incorrect - App Engine can access 'normal' files, as long as they were uploaded with the app. How else would it load templates?
Nick Johnson
Dear Nick! OK, I removed that controversial statement that GAE cannot access 'normal' files, but I hope that you, as a Google engineer working for AppEngine could be somewhat more constructive and provide better explanations why GAE cannot access gettext files and what can be done? From my experience few months ago, I could get gettext working locally with SDK, but it didn't work on GAE. So I am waiting for details how to fix gettext on GAE
jetxee