Details

Applies to
Moin 1.9, 1.8.
Purpose
Allow plugin translation with po files (no additional source code required in the plugin).
Description

This patch makes Moin load po files for domains other than MoinMoin. This makes plugin translation as easy as placing a po file in MoinMoin/i18n, with no additional source code required in the plugin side. This also means that plugins can change current Moin translation to what they think to be better.

This patch also fixes a problem where you cannot instantiate a Translation object for your own domain without overriding the cache of MoinMoin domain, thus breaking translation. The solution adopted was attaching the domain name to the cache entry, so that you don't need to workaround it with dummy language names (e.g. Translation('es_custom', 'SomeDomain')).

Patch

   1 --- MoinMoin/i18n/__init__.py	2009-12-03 22:26:52 +0000
   2 +++ MoinMoin/i18n/__init__.py	2009-12-03 22:21:50 +0000
   3 @@ -222,7 +222,7 @@
   4      def loadLanguage(self, request, trans_dir="i18n"):
   5          request.clock.start('loadLanguage')
   6          # see comment about per-wiki scope above
   7 -        cache = caching.CacheEntry(request, arena='i18n', key=self.language, scope='wiki', use_pickle=True)
   8 +        cache = caching.CacheEntry(request, arena='i18n', key=('%s_%s' % (self.language, self.domain)), scope='wiki', use_pickle=True)
   9          langfilename = po_filename(request, self.language, self.domain, i18n_dir=trans_dir)
  10          needsupdate = cache.needsUpdate(langfilename)
  11          if not needsupdate:
  12 @@ -256,6 +256,18 @@
  13      """ Return the text direction for a language, either 'ltr' or 'rtl'. """
  14      return languages[lang]['x-direction']
  15  
  16 +def loadAllDomains(request, lang):
  17 +    t = Translation(lang)
  18 +    t.loadLanguage(request)
  19 +    global translations
  20 +    translations[lang] = t
  21 +    for lang_file in glob.glob(po_filename(request, language=lang, domain='*')):
  22 +        domain = os.path.basename(lang_file).split('.')[1]
  23 +        if domain != 'MoinMoin':
  24 +            t = Translation(lang, domain)
  25 +            t.loadLanguage(request)
  26 +            translations[lang].raw.update(t.raw)
  27 +
  28  def getText(original, request, lang, **kw):
  29      """ Return a translation of some original text.
  30  
  31 @@ -279,9 +291,7 @@
  32  
  33      global translations
  34      if not lang in translations: # load translation if needed
  35 -        t = Translation(lang)
  36 -        t.loadLanguage(request)
  37 -        translations[lang] = t
  38 +        loadAllDomains(request, lang)
  39  
  40      # get the matching entry in the mapping table
  41      translated = original

MoinMoin.i18n.patch

Discussion

Plan


CategoryMoinMoinPatch

MoinMoin: MoinMoinPatch/LoadAllTtranslationDomains (last edited 2010-01-22 00:00:47 by RenatoSilva)