[chrome-gnome-shell/feature/gettext] gettext: added script that generates messages.json files from po



commit 71f365df9c3c0cf7ba8b805b00656e9e9a14fb25
Author: Yuri Konotopov <ykonotopov gmail com>
Date:   Thu May 5 12:42:42 2016 +0300

    gettext: added script that generates messages.json files from po

 contrib/po2chrome-messages.py |  113 +++++++++++++++++++++++++++++++++++++++++
 1 files changed, 113 insertions(+), 0 deletions(-)
---
diff --git a/contrib/po2chrome-messages.py b/contrib/po2chrome-messages.py
new file mode 100755
index 0000000..8d75363
--- /dev/null
+++ b/contrib/po2chrome-messages.py
@@ -0,0 +1,113 @@
+#!/usr/bin/env python
+
+'''
+    GNOME Shell integration for Chrome
+    Copyright (C) 2016  Yuri Konotopov <ykonotopov gmail com>
+
+    This program is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+'''
+
+from copy import copy
+import argparse
+import json
+import os
+import polib
+import sys
+
+METADATA_SUFFIX = 'chrome-gnome-shell-key-'
+METADATA_STORE_DESCRIPTION = 'chrome-gnome-shell-store-description'
+
+class Directory(argparse.Action):
+       def __call__(self, parser, namespace, values, option_string=None):
+               prospective_dir=values
+               if not os.path.isdir(prospective_dir):
+                       raise argparse.ArgumentTypeError("Directory: {0} is not a valid 
path".format(prospective_dir))
+               if os.access(prospective_dir, os.R_OK):
+                       setattr(namespace, self.dest, prospective_dir)
+               else:
+                       raise argparse.ArgumentTypeError("Directory: {0} is not a readable 
dir".format(prospective_dir))
+
+def parseArguments():
+       parser = argparse.ArgumentParser()
+
+       parser.add_argument('--reference-lang', '-l',
+                       dest='lang',
+                       default='en',
+                       help='Reference language')
+       parser.add_argument('chrome_store_description',
+                       action=Directory,
+                       metavar='<chrome store dir>',
+                       help='Directory contains Chrome store description files')
+       parser.add_argument('locales',
+                       action=Directory,
+                       metavar='<locales dir>',
+                       help='Path to extension _locales directory')
+       parser.add_argument('po',
+                       action=Directory,
+                       metavar='<po dir>',
+                       help='Directory for gettext files')
+
+       return parser.parse_args()
+
+
+def find_chrome_entry(po, key, exact=False):
+       for entry in po:
+               for occurrence, line in entry.occurrences:
+                       if exact and occurrence == key:
+                               return entry
+                       elif occurrence == METADATA_SUFFIX + key:
+                               return entry
+
+
+def run():
+       args = parseArguments();
+
+       with open(os.path.join(args.locales, args.lang, "messages.json"), 'r') as file:
+               chromeMessages = json.load(file)
+
+       with open(os.path.join(args.chrome_store_description, args.lang), 'r') as file:
+               chromeStoreDescription = file.read()
+
+       with open(os.path.join(args.po, 'LINGUAS'), 'r') as file:
+               for line in file:
+                       lang = line.strip()
+
+                       po_path = os.path.join(args.po, lang + '.po')
+
+                       if not lang or not os.path.isfile(po_path):
+                               continue
+
+                       print('Processing ' + lang)
+
+                       with open(po_path, 'r') as file:
+                               po = polib.pofile(po_path)
+
+                       messages = { '_DO_NOT_EDIT': { 'message': '', 'description': 'This file is auto 
generated with po2chrome-messages tool from ' + lang + '.po.' }}
+                       for messageKey in chromeMessages:
+                               messageEntry = copy(chromeMessages[messageKey])
+
+                               entry = find_chrome_entry(po, messageKey)
+                               if entry and entry.msgstr and not 'fuzzy' in entry.flags:
+                                       messageEntry['message'] = entry.msgstr
+
+                               messages[messageKey] = messageEntry
+
+                       os.makedirs(os.path.join(args.locales, lang), exist_ok=True)
+                       with open(os.path.join(args.locales, lang, "messages.json"), 'w') as file:
+                               json.dump(messages, file, indent='\t', sort_keys=True)
+
+                       entry = find_chrome_entry(po, METADATA_STORE_DESCRIPTION, exact=True)
+                       if entry and entry.msgstr and not 'fuzzy' in entry.flags:
+                               with open(os.path.join(args.chrome_store_description, lang), 'w') as file:
+                                       file.write(entry.msgstr)
+                       else:
+                               try:
+                                       os.remove(os.path.join(args.chrome_store_description, lang))
+                               except FileNotFoundError:
+                                       pass
+
+if __name__ == '__main__':
+       run()


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]