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
Discussion
- Nice patch, but:
- domains are to keep stuff separate, but you're mixing it all together in the end
usually plugins should not place stuff into MoinMoin/i18n/ (plugins are per wiki, but MoinMoin/i18n is global, access rights, ...)
RenatoSilva - What exactly means keep stuff separate? For this patch, it means to attach additional translations without changing default po file, or in other words, allow multiple po files for the same language. You may want to change/expand the definition and tell that domains are not being fully implemented, but remember that currently they are not implemented at all.
Also, I don't think mixing all domains together in translations object breaks the domain idea, according to the above interpretation. If you want plugins using their own translation dictionary, then they can just continue doing what they currently do. That is, this change is an addition and doesn't break current code used by plugins to load their own pos. It just adds a new feature (easy plugin translation) which will be available for those who want to use it, and which can be safely ignored. The only issue would be if any current plugin is placing its po files in the root of i18n using the domain scheme for naming, then that file would be loaded globally. However I wonder if any existing plugin does this or if loading them would be a relevant issue.
Note: different directories, rather than domains, would be enough for implementing the idea (actually the pos would be better organized in that case), but I used domains for convenience and coherence with default (but unimplemented) scheme.
About your second point, how about the plugins dir? For example:plugins/macro/ChildPages.py plugins/macro/ChildPages.de.po plugins/macro/ChildPages.es.po plugins/macro/ChildPages.pt-br.po
Plan
- Priority:
- Assigned to:
- Status: