jenkins-bot has submitted this change and it was merged.
Change subject: [SYNC] Add JSON support to make_i18n_dict.py ......................................................................
[SYNC] Add JSON support to make_i18n_dict.py
It works simply like this
from scripts.maintenance.make_i18n_dict import i18nBot bot = i18nBot('disambredir', 'msg') bot.to_json()
Duplicate of Ifcf93e18c3393f908caf80f86ad231ee21faca49 Bug: T87231
Change-Id: Ib072780d0bee0de78f64696253db0d0822d8e0d0 --- M maintenance/make_i18n_dict.py 1 file changed, 33 insertions(+), 2 deletions(-)
Approvals: Ladsgroup: Looks good to me, approved jenkins-bot: Verified
diff --git a/maintenance/make_i18n_dict.py b/maintenance/make_i18n_dict.py index d3d2f66..e9a321a 100644 --- a/maintenance/make_i18n_dict.py +++ b/maintenance/make_i18n_dict.py @@ -14,6 +14,11 @@
If you have the messages as instance constants you may call the bot as follows:
bot = i18nBot('<scriptname>.<class instance>', '<msg dict1>', '<msg dict2>')
+ +It's also possible to make json files too by using to_json method: +>>> from make_i18n_dict import i18nBot +>>> bot = i18nBot('disambredir', 'msg') +>>> bot.to_json() """ # # (C) xqt 2013 @@ -23,15 +28,21 @@ # __version__ = '$Id$' # +import os +import json +import codecs import sys sys.path.insert(1, '..') + +from pywikibot import config +
class i18nBot(object):
def __init__(self, script, *args): modules = script.split('.') self.scriptname = modules[0] - + self.script = __import__(self.scriptname, modules[1:]) if len(modules) == 2 and hasattr(self.script, modules[1]): self.script = getattr(self.script, modules[1]) @@ -51,7 +62,7 @@ for msg in self.messages: label = "%s-%s" % (self.scriptname, msg) if label in self.dict[code]: - print " " * 16 +"'%s': u'%s'," \ + print " " * 16 + "'%s': u'%s'," \ % (label, self.dict[code][label]) print " " * 8 + "}," print "}" @@ -78,5 +89,25 @@ self.read(msg) self.print_all()
+ def to_json(self): + if not self.dict: + self.run() + json_dir = os.path.join( + config.base_dir, 'i18n', self.scriptname) + if not os.path.exists(json_dir): + os.makedirs(json_dir) + for lang in self.dict: + file_name = os.path.join(json_dir, '%s.json' % lang) + if os.path.isfile(file_name): + with codecs.open(file_name, 'r', 'utf-8') as json_file: + new_dict = json.loads(json_file.read()) + else: + new_dict = {} + new_dict['@metadata'] = new_dict.get('@metadata', {'authors': []}) + with codecs.open(file_name, 'w', 'utf-8') as json_file: + new_dict.update(self.dict[lang]) + json.dump(new_dict, json_file, ensure_ascii=False, + sort_keys=True, indent=4, separators=(',', ': ')) + if __name__ == "__main__": print __doc__
pywikibot-commits@lists.wikimedia.org