J'utilise cette solution avec Pylons et ExtJS, mais je pense que vous pouvez facilement changer la fonction JS pour vos besoins ..
Dans le contrôleur Pylônes i ont la méthode qui exporte toutes les traductions (dont vous avez besoin pour ce polib - http://code.google.com/p/polib/):
import polib
from pylons.i18n import get_lang
...
## method in conttroller class
@jsonify
def get_trans(self):
def get_trans():
items = polib.mofile(os.path.join(config['pylons.paths']['root'], 'i18n/%s/LC_MESSAGES/app.mo' % get_lang()[0]))
return [{'name': item.msgid, 'value': item.msgstr} for item in items]
return {'translations' : get_trans()}
partie JavaScript:
lang_store = new Ext.data.JsonStore({
url: '/lang/get',
root: 'translations',
id: 'name',
autoLoad: true,
fields: ['name', 'value'],
});
gettext = _ = function(_key) {
try {
return lang_store.getById(_key).data.value
} catch(e) {
return _key + "**"
}
}
maintenant, vous pouvez utiliser "gettext (STRING)" ou "_ (STRING)" fonction JS et vous obtiendrez chaîne traduite, s'il n'y aura pas de traduction, vous obtiendrez quelque chose comme UNTRANSLATED_STRING ** (avec des astérisques à la fin).