How to translate an application running on GAE?

178 views
Skip to first unread message

Emilien Klein

unread,
Jul 31, 2009, 6:46:34 PM7/31/09
to Google App Engine
Hi all,

After roaming through various blog posts and forums/discussion groups,
I just can't seem to find the "best way" to have a translated
application on GAE...

I'd like to have an application that automatically adapts itself to
the language that is sent in the headers by the client's web browser.
(You could also manually change the language, of course)
Retrieving the values should not be complicated (just get the value
from the headers, and do some processing on it to remove the "_FR"
part of "fr_FR", for example), however my question is more about the
technical implications of translation. And (REALLY important), I'd
like to be able to do that just with the standard App Engine API,
without having to install some kind of "Django on GAE" project that I
would need to test/support for an unknown duration...

Let me make some statements/questions:
- I suppose that the .po / .mo files that open source projects
traditionally use can not be used in the App Engine world?
- Would you have various template files for each languages (like a
contact.en.html, contact.fr.html, contact.nl.html, etc.) and use the
appropriate file depending on the language that has been detected from
the browser header?
- Would you use a single template file, but pass it the strings one by
one that you would retrieve from the datastore (ouch, just writing
this solution hurts!)

I'd like to hear if someone has successfully deployed a translated
application (links please), and if possible what solution you have
used.

(I'll just type in those 2 tags, if someone searches for it: i18n or
l10n )


Note: if you execute the following command from inside the GAE dev
server folder:
find | xargs -i grep -iH i18n \{\}
you will see that a lot of files contain the string i18n, and that all
are related to django. Does that mean that we could use standard
Django internationalization techniques, or is it just that Google <
forgot > to remove those files?

Rodrigo Moraes

unread,
Jul 31, 2009, 7:05:54 PM7/31/09
to google-a...@googlegroups.com
On Fri, Jul 31, 2009 at 7:46 PM, Emilien Klein wrote:
> I'd like to hear if someone has successfully deployed a translated
> application (links please), and if possible what solution you have
> used.

I use babel. It's a library from the trac guys:

http://babel.edgewall.org/

It is a gettext wrapper and i18n tools. You can extract messages from
.py and .html files, and then you translate/compile the .po/.mo
catalogs. For a full implementation you can see how it is done in Kay:

http://bitbucket.org/tmatsuo/kay/src/tip/kay/i18n/

-- rodrigo

Rodrigo Moraes

unread,
Jul 31, 2009, 7:17:35 PM7/31/09
to google-a...@googlegroups.com
Now, anwering some other questions.

> - I suppose that the .po / .mo files that open source projects
> traditionally use can not be used in the App Engine world?

Yes, you can use those files with gettext functions. You'll probably
need to wrap those functions and write some utilities to make it
easier to use - but you don't need to do that because there are
excellent libraries that have done it - Babel is the most known in the
python world (I actually don't know any other).

> - Would you have various template files for each languages (like a
> contact.en.html, contact.fr.html, contact.nl.html, etc.) and use the
> appropriate file depending on the language that has been detected from
> the browser header?

No! You would use one template, and have the strings wrapped by a
translation function. The convention is to use a function name "_".
Like:

{{ _("This is my string - I'll translate it later, and if not it'll be
in English forever.") }}

There are issues you'll find, like how to handle singular/plural and
string replacements, but existing solutions cover that.

> - Would you use a single template file, but pass it the strings one by
> one that you would retrieve from the datastore (ouch, just writing
> this solution hurts!)

Absolutelly no. The strings are stored in a .po file, which you
compile into a .mo file. This if you go for the widely used gettext.

> Note: if you execute the following command from inside the GAE dev
> server folder:
> find | xargs -i grep -iH i18n \{\}
> you will see that a lot of files contain the string i18n, and that all
> are related to django. Does that mean that we could use standard
> Django internationalization techniques, or is it just that Google <
> forgot > to remove those files?

I tried to use Django's i18n. Unfortunately Django is too tied to
itself and that library is not reusable outside of Django. Fortunately
later I found a standalone, reusable, well written one: Babel. It
works very well.

-- rodrigo

Emilien Klein

unread,
Aug 1, 2009, 10:51:13 AM8/1/09
to Google App Engine
OK, thanks Rodrigo for the link to Babel. I had already seen it, but
never used it.
The problem I see with using Babel is that it IS an external library,
which means that I need to integrate it with my project. What I'm
looking for is a way to have a translated application WITHOUT having
to install any external library...

I am looking at http://makeyjl.blogspot.com/2009/02/using-djangos-i18n-in-google-app-engine.html
right now, and (while I'm not done playing around) I have already been
able to generate .po files, and my application still works OK. I'm
going to continue playing around, and then I'll report here about how
it went. If everything works fine I might even write a Knol article to
explain my steps (the article is not 100% accurate, I had to find out
to add "{% load i18n %}" in the templates for instance...

If anyone has another way of translating App Engine applications using
only the available tools, fell free to share it!

Rodrigo Moraes

unread,
Aug 1, 2009, 11:16:28 AM8/1/09
to google-a...@googlegroups.com
On Sat, Aug 1, 2009 at 11:51 AM, Emilien Klein wrote:
> The problem I see with using Babel is that it IS an external library,
> which means that I need to integrate it with my project. What I'm
> looking for is a way to have a translated application WITHOUT having
> to install any external library...

Without any other external library, you have 2 options:

1. use gettext - http://docs.python.org/library/gettext.html
2. hack and patch django's i18n

The problem with the 2nd option is that your app stays tied to
django's ecosystem - later you need i18n in a small project and you'll
have to add django just for it. Ok, it is a valid solution, I just
personally prefer to use a external library that is not coupled with a
bunch of unrelated things, and avoid all the monkeypatching mess.

-- rodrigo

Devel63

unread,
Aug 1, 2009, 11:31:41 AM8/1/09
to Google App Engine
We use the Django 0.96 that is bundled with GAE.

from django.utils.translation import gettext as _

Then just use _('My String") in the code, combined with the .po/.mo
files.


On Aug 1, 8:16 am, Rodrigo Moraes <rodrigo.mor...@gmail.com> wrote:
> On Sat, Aug 1, 2009 at 11:51 AM, Emilien Klein wrote:
> > The problem I see with using Babel is that it IS an external library,
> > which means that I need to integrate it with my project. What I'm
> > looking for is a way to have a translated application WITHOUT having
> > to install any external library...
>
> Without any other external library, you have 2 options:
>
> 1. use gettext -http://docs.python.org/library/gettext.html

Emilien Klein

unread,
Aug 1, 2009, 2:21:08 PM8/1/09
to Google App Engine
I understand your point, but Django has the benefit of coming bundled
with GAE, whereas Babel is an external lib that you need to import in
your project, keep up to date, etc...
I'll try to use what comes with GAE, and if it doesn't perform well,
then maybe I'll start looking at other external libraries.

On 1 août, 10:16, Rodrigo Moraes <rodrigo.mor...@gmail.com> wrote:
> On Sat, Aug 1, 2009 at 11:51 AM, Emilien Klein wrote:
> > The problem I see with using Babel is that it IS an external library,
> > which means that I need to integrate it with my project. What I'm
> > looking for is a way to have a translated application WITHOUT having
> > to install any external library...
>
> Without any other external library, you have 2 options:
>
> 1. use gettext -http://docs.python.org/library/gettext.html

Emilien Klein

unread,
Aug 1, 2009, 2:22:49 PM8/1/09
to Google App Engine
Hi Devel63,
Thanks for your answer. But this is just for inside your Python files,
how do you manage text in the templates? Or don't you use templates? I
don't really see how you're managing this...
Is your app open source (i.e. Is there a place where I can see your
code)?

Jason Salas

unread,
Aug 1, 2009, 5:56:25 PM8/1/09
to google-a...@googlegroups.com
I've used Babelfish only as a web tool, never an API, so does that
option exist? You can leverage GAE's urlfetch lib and make a remote
call if Babelfish has a REST interface.
--
Sent from my mobile device

Roberto Saccon

unread,
Aug 1, 2009, 7:23:02 PM8/1/09
to Google App Engine
I haven't followed this discussion in detail, but babelfish is easy to
implement in django, however you need a bit of low-level hackery, I
easily got it working (and I am not a regular Python hacker), I had
all the babelfish stuff running db-based (templates and translations),
completely replacing gettext. That project was just a proof-of-
concept. Start with looking the babelfish django integration they are
offering, that shows how to do things (helpful even if you are not
using django)

--
Roberto

On Aug 1, 6:56 pm, Jason Salas <digitalpontificat...@gmail.com> wrote:
> I've used Babelfish only as a web tool, never an API, so does that
> option exist?  You can leverage GAE's urlfetch lib and make a remote
> call if Babelfish has a REST interface.
>
> On 8/2/09, Emilien Klein <emilien.kl...@gmail.com> wrote:
>
>
>
>
>
>
>
> > OK, thanks Rodrigo for the link to Babel. I had already seen it, but
> > never used it.
> > The problem I see with using Babel is that it IS an external library,
> > which means that I need to integrate it with my project. What I'm
> > looking for is a way to have a translated application WITHOUT having
> > to install any external library...
>
> > I am looking at
> >http://makeyjl.blogspot.com/2009/02/using-djangos-i18n-in-google-app-...

Devel63

unread,
Aug 2, 2009, 10:51:58 AM8/2/09
to Google App Engine
In the templates, use {% trans 'My string' %}.

Put {% load i18n %} at the top of the file (must be one of first
couple lines).
Message has been deleted

dflorey

unread,
Aug 3, 2009, 4:01:54 AM8/3/09
to Google App Engine
You can use my simple commons project, it worked fine for my GAE app:

http://commons.apache.org/sandbox/i18n/

On 3 Aug., 07:57, NiklasRTZ <nik...@montao.com.br> wrote:
> The sv .po seems to have a colon too much so it doubles with use with
> forms. All ready translations from the django library LC_MESSAGES can
> get reused for our projects which is very good, templates work with
> i18n and a custom request handler and some success currently
> integrating translations with djangoforms where I must translate eg.
> from django.utils import translation
> class i18NForm(djangoforms.ModelForm):
>   email = forms.CharField(max_length=127,label=_("E-mail
> address").capitalize())
> Then sv.LC_MESSAGES.django.po row 1584 read
> #: contrib/admin/views/doc.py:297
> msgid "E-mail address"
> msgstr "E-postadress:"
> Note the colon (:) then doubles when using it with djangoforms. Hence
> my suspicion the colon should be removed otherwise it doubles.
> It's only in the locale sv locale no didn't double the ":"
> #: contrib/admin/views/doc.py:283
> msgid "E-mail address"
> msgstr "E-post adresse"
> Should the colon get removed?

ego008

unread,
Aug 4, 2009, 8:41:52 AM8/4/09
to google-a...@googlegroups.com
you can read the django docs

2009/8/3 dflorey <daniel...@gmail.com>
--
gae-django-cms (GD-cms)
a multi-user CMS running on GAE 一个基于GAE多用户的CMS
sample http://cmsdome.appspot.com/
projects http://code.google.com/p/gae-django-cms/
Reply all
Reply to author
Forward
0 new messages