From e5f5822c338c2c268fb367227c0c7d191a97d5d1 Mon Sep 17 00:00:00 2001 From: tobes Date: Mon, 6 Aug 2012 12:51:19 +0100 Subject: [PATCH] [#2774] I18n improved .po JavaScript extractions --- ckan/i18n/zh_TW/LC_MESSAGES/ckan.po | 1 + ckan/lib/cli.py | 94 +++++++++++++++++++++-------- 2 files changed, 70 insertions(+), 25 deletions(-) diff --git a/ckan/i18n/zh_TW/LC_MESSAGES/ckan.po b/ckan/i18n/zh_TW/LC_MESSAGES/ckan.po index 98c516e3ed4..9d73279e780 100644 --- a/ckan/i18n/zh_TW/LC_MESSAGES/ckan.po +++ b/ckan/i18n/zh_TW/LC_MESSAGES/ckan.po @@ -12,6 +12,7 @@ msgstr "" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" +"Plural-Forms: nplurals=1; plural=0\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=utf-8\n" "Content-Transfer-Encoding: 8bit\n" diff --git a/ckan/lib/cli.py b/ckan/lib/cli.py index 3eb2d247ea5..df17d790cd8 100644 --- a/ckan/lib/cli.py +++ b/ckan/lib/cli.py @@ -1502,42 +1502,86 @@ def command(self): else: print 'command not recognised' - def build_js_translations(self): - import polib - import gettext - import simplejson as json - pot_path = os.path.join(self.i18n_path, 'ckan.pot') - po = polib.pofile(pot_path) - js_strings = [] + def po2dict(self, po, lang): + '''Convert po object to dictionary data structure (ready for JSON). + + This function is from pojson + https://bitbucket.org/obviel/pojson + +Copyright (c) 2010, Fanstatic Developers +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + * Neither the name of the nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL FANSTATIC DEVELOPERS BE LIABLE FOR ANY +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +''' + result = {} + + result[''] = {} + result['']['plural-forms'] = po.metadata['Plural-Forms'] + result['']['lang'] = lang + result['']['domain'] = 'ckan' + for entry in po: + if entry.obsolete: + continue + # check if used in js file we only include these occurrences = entry.occurrences js_use = False for occurrence in occurrences: if occurrence[0].endswith('.js'): js_use = True break - if js_use: - msg = entry.msgid.encode('utf-8') - js_strings.append(msg) + if not js_use: + break + if entry.msgstr: + result[entry.msgid] = [None, entry.msgstr] + elif entry.msgstr_plural: + plural = [entry.msgid_plural] + result[entry.msgid] = plural + ordered_plural = sorted(entry.msgstr_plural.items()) + for order, msgstr in ordered_plural: + plural.append(msgstr) + return result + + def build_js_translations(self): + import polib + import simplejson as json out_dir = os.path.abspath(os.path.join(self.ckan_path, 'public', 'base', 'i18n')) - languages = [] - for f in os.listdir(self.i18n_path): - if os.path.isdir(os.path.join(self.i18n_path, f)): - languages.append(f) - - for language in languages: - lang = gettext.translation('ckan', self.i18n_path, [language]) - lang.install() - trans_dict = {} - for string in js_strings: - trans_dict[string] = lang.gettext(string) - out_file = open(os.path.join(out_dir, '%s.js' % language), 'w') - out_file.write(json.dumps(trans_dict)) - out_file.close() - print 'Generated JavaScript translations' + for l in os.listdir(self.i18n_path): + if os.path.isdir(os.path.join(self.i18n_path, l)): + print 'Generating', l + f = os.path.join(self.i18n_path, l, 'LC_MESSAGES', 'ckan.po') + po = polib.pofile(f) + data = self.po2dict(po, l) + data = json.dumps(data, sort_keys=True, + ensure_ascii=False, indent=2 * ' ') + out_file = open(os.path.join(out_dir, '%s.js' % f), 'w') + out_file.write(data) + out_file.close() + + print 'Completed generating JavaScript translations' def mangle_po(self): ''' This will mangle the zh_TW translations for translation coverage