Permalink
Browse files

Track url/domain/lang in JSONTranslations

  • Loading branch information...
1 parent 4eaf67e commit a22ccd0e77590cd196a9136a6ce4d6d5175b8edd @keesbos keesbos committed Jul 3, 2012
Showing with 83 additions and 10 deletions.
  1. +5 −7 examples/gettext/Gettext.py
  2. +53 −0 examples/gettext/__main__.py
  3. +13 −0 examples/gettext/i18n.py
  4. +12 −3 library/pyjamas/JSONTranslations.py
@@ -4,19 +4,17 @@
from pyjamas.ui.HTML import HTML
from pyjamas.ui.Label import Label
from pyjamas import Window
-from pyjamas.JSONTranslations import JSONTranslations
+from i18n import i18n, _, ngettext
+import pygwt
+
-t = JSONTranslations()
-_ = t.gettext
lang = [
'nl_NL',
'de_DE',
'en_US',
]
-import pygwt
-
class GettextExample(object):
def __init__(self):
@@ -36,14 +34,14 @@ def change_texts(self, text):
self.l.setText(_("Hello World (label)"))
text = [_("Hello from %s") % pygwt.getModuleBaseURL()]
for i in range(4):
- text.append(t.ngettext('%(num)d single', '%(num)d plural', i) % dict(num=i))
+ text.append(ngettext('%(num)d single', '%(num)d plural', i) % dict(num=i))
text = '<br />'.join(text)
self.base.setHTML(text)
def greet(self, fred):
fred.setText(_("No, really click me!"))
Window.alert(_("Hello, there!"))
- t.load("lang", "Gettext", lang[0], onCompletion=self.change_texts)
+ i18n.load(lang=lang[0], onCompletion=self.change_texts)
lang.append(lang.pop(0))
if __name__ == '__main__':
@@ -0,0 +1,53 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+
+TARGETS = [
+ 'Gettext.py',
+]
+
+
+PACKAGE = {
+ 'title': 'gettext example',
+ 'desc': 'Gettext using JSONTranslations',
+}
+
+
+def setup(targets):
+ '''Setup example for translation, MUST call util.setup(targets).'''
+ util.setup(targets)
+
+
+def translate():
+ '''Translate example, MUST call util.translate().'''
+ util.translate()
+
+
+def install(package):
+ '''Install and cleanup example module. MUST call util.install(package)'''
+ util.install(package)
+
+
+##---------------------------------------##
+# --------- (-: DO NOT EDIT :-) --------- #
+##---------------------------------------##
+
+
+import sys
+import os
+
+
+examples = head = os.path.abspath(os.path.dirname(__file__))
+while os.path.split(examples)[1].lower() != 'examples':
+ examples = os.path.split(examples)[0]
+ if not examples:
+ raise ValueError("Cannot determine examples directory")
+sys.path.insert(0, os.path.join(examples))
+from _examples import util
+sys.path.pop(0)
+
+util.init(head)
+
+setup(TARGETS)
+translate()
+install(PACKAGE)
View
@@ -0,0 +1,13 @@
+from pyjamas.JSONTranslations import JSONTranslations
+
+__all__ = ['i18n', 'gettext', 'ngettext', '_']
+
+
+class Translations(JSONTranslations):
+ base_url = "lang"
+ domain = "Gettext"
+
+i18n = Translations()
+gettext = i18n.gettext
+ngettext = i18n.ngettext
+_ = gettext
@@ -20,18 +20,27 @@ def c2py(plural):
class JSONTranslations(NullTranslations):
re_nplurals = re.compile('nplurals *= *(\d+)')
re_plural = re.compile('plural *= *([^;]+)')
+ base_url = None
+ domain = None
def __init__(self, *args, **kwargs):
NullTranslations.__init__(self, *args, **kwargs)
+ self.new_catalog()
+
+ def new_catalog(self, base_url=None, domain=None, lang=None):
self._catalog = {}
+ self.lang = lang
self.plural = lambda n: int(n != 1) # germanic plural by default
- def load(self, base_url, domain=None, lang=None, onCompletion=None, onError=None):
+ def load(self, base_url=None, domain=None, lang=None, onCompletion=None, onError=None):
+ if base_url is None:
+ base_url = self.base_url
+ if domain is None:
+ domain = self.domain
url = base_url
if domain is not None and lang is not None:
url = "%s/%s_%s.json" % (url, domain, lang)
- self._catalog = {}
- self.plural = lambda n: int(n != 1) # germanic plural by default
+ self.new_catalog(base_url, domain, lang)
self._onCompletion = onCompletion
self._onError = onError
HTTPRequest().asyncGet(url, self)

0 comments on commit a22ccd0

Please sign in to comment.