Skip to content

Commit

Permalink
[#2774] I18n improved .po JavaScript extractions
Browse files Browse the repository at this point in the history
  • Loading branch information
tobes committed Aug 6, 2012
1 parent 3fbfe7d commit e5f5822
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 25 deletions.
1 change: 1 addition & 0 deletions ckan/i18n/zh_TW/LC_MESSAGES/ckan.po
Expand Up @@ -12,6 +12,7 @@ msgstr ""
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\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"
Expand Down
94 changes: 69 additions & 25 deletions ckan/lib/cli.py
Expand Up @@ -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 <organization> 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
Expand Down

0 comments on commit e5f5822

Please sign in to comment.