First of all, have look at i18n. This is the standard way of dealing with locales. If you want to pursue the aproach you are taking now, which could be ok, if you dont have alot of language properties you could use the structured property, something likethis
Class languageprop(model):
en_val = stringproperty
es_val = stringproperty
Class someclass(model):
title = StructuredProperty(languageprop)
Sorry for typos and syntax.. Writing from tablet
Sent from my iPhone
I make apps in English and then localize to Portuguese or Swedish or
whatever the end usage is. It's more work but a better solution for
the programmers to be able to read what i/o is without having to
understand Portuguese or Swedish. A huge advantage over other i18n
systems for app engine is that with webapp2 you can localize
currencies and timezones using babel so I could write a localized
timesince filter, everything works and it is bug-free. Maybe webapp2
or some framework could include a useful getter for a user's language
ie user.get_locale something like django has that parses the request
so that pt-br falls back to pt if not available.
I think gettext is a very good project and I'm getting user's language
from request by http get parameter as described in the documentation,
http headers, by session value, by cookie and one could also get it
from datastore value. When making a locale file I find using poedit is
more convenient and that babel.cfg is not needed if you use poedit - I
can just compile the .po file directly in poedit. I change the
language in the basehandler dispatch function depending on the values
found. This is a not buggy but I don't understand why I cannot set the
session variable in the dispatch method:
from webapp2_extras import i18n
from webapp2_extras.i18n import lazy_gettext as _
def dispatch(self):
"""
............Save the sessions for preservation across requests
........"""
# self.session_store = sessions.get_store(request=self.request)
# if self.request.host.find('localhost') > 0: # for a Swedish
domain that uses Swedish
# or lang = os.environ.get("HTTP_ACCEPT_LANGUAGE")
i18n.get_i18n().set_locale('sv')
lang_code_get = self.request.get('hl', None)
if lang_code_get:
#self.session['i18n_language'] = lang_code_get # why won't this work?
i18n.get_i18n().set_locale(lang_code_get)
try:
response = super(NewBaseHandler, self).dispatch()
self.response.write(response)
finally:
self.session_store.save_sessions(self.response)
So I'd like a way to solve this once for all my apps like the user
model does a good job at a user model we want localization library
ready to use with builtin session management when starting a new app.
My configuration is
config = {'webapp2_extras.jinja2': {'template_path': 'template_files',
'filters': {'makeid': filters.makeid,
'timesince': filters.timesince,
'datetimeformat': filters.datetimeformat},
'environment_args': {'extensions': ['jinja2.ext.i18n']}}
'webapp2_extras.sessions': {'secret_key':
'RR-234234432432-secret-key'}
}
Thank you
Nick Rosencrantz
Now, if it dynamic data, then I could see why you need it stored with
it language attributes in the datastore.
--
Michael Robellard
(216)288-2745
Play Railroad Empire: http://www.therailroadempire.com
--
TBH I don't know which would be faster; it might be worth measuring carefully.
It may also depend on whether you have C code to accelerate it, which
you will when using the Python 2.7 runtime for all three, but which
only protobuf has for Python 2.5.
Downsides of JSON and PB are that you must conform to the limitations
of those respective serialization methods; pickle has limitations too
but is generally more lenient. When using PB, in particular, you would
have to use LocalStructuredProperty, which would require that you
define the structure using a Model class. I *think* the design here
requires being able to serialize a dict, so that would rule out PB --
it has no direct dict support. (In fact, PickleProperty and
JSONProperty were added in part to overcome this. :-)
Apologies to hetsch / Gernot / the OP, I don't have more time to
review your code. Good luck!
<dt>{% trans %}Country{% endtrans %}:</dt>
<dd>{% trans %}{{user.country}}{% country %}</dd>
? I normally just write my translation in a .po file and compile it
with poedit to .mo and then Jinja2 can pick up the compiled
translations without use of the datastore. I think gettext is a good
project since it enables us to use the same compiled .mo translation
for many different projects like I can take the .po file from django
which has rather a lot of translations and use those translations in
my project under the BSD licence.
Thank you for the aweseome ndb library,
Nick Rosencrantz