Last active
August 10, 2018 10:34
-
-
Save nesterchung/bff41ad90bcf130c022fe9ad7792c711 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# File : gist:bff41ad90bcf130c022fe9ad7792c711/strings_translate.py | |
# Author : Nester Chung <twntwn3838@gmail> | |
# Date : 09.08.2018 | |
# Last Modified Date: 10.08.2018 | |
# Please install module before use this script: | |
# localizable | |
# googletrans | |
import sys | |
import argparse | |
import localizable | |
import logging as log | |
from googletrans import Translator | |
TRANSLATOR = Translator() | |
def translate(filein, lang): | |
""" Translate the .strings """ | |
strings = localizable.parse_strings(filename=filein) | |
for s in strings: | |
(comment, key, value) = (s['comment'], s['key'], s['value']) | |
tobj = TRANSLATOR.translate(value, src='en', dest=lang) | |
if not comment: | |
line = '"%s" = "%s";\n' % (key, tobj.text) | |
else: | |
line = '/* %s */\n"%s" = "%s";\n' % (comment, key, tobj.text) | |
line = line.encode('utf-8') | |
sys.stdout.write(line) | |
sys.stdout.write('\n') | |
if __name__ == "__main__": | |
PARSER = argparse.ArgumentParser(description='Translate .strings(en) file to specific language, and output to STDOUT') | |
PARSER.add_argument('filein', metavar='INPUT', help='input filename') | |
PARSER.add_argument('-l', '--lang', help='language to translate') | |
PARSER.add_argument('-v', '--verbose', action='store_true') | |
ARGS = PARSER.parse_args() | |
if ARGS.verbose: | |
log.basicConfig(format="%(levelname)s: %(message)s", level=log.DEBUG) | |
else: | |
log.basicConfig(format="%(levelname)s: %(message)s") | |
translate(ARGS.filein, ARGS.lang) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment