diff --git a/bin/gen/timeago-to-scala.py b/bin/gen/timeago-to-scala.py old mode 100644 new mode 100755 index 00352bfd19..26250f470b --- a/bin/gen/timeago-to-scala.py +++ b/bin/gen/timeago-to-scala.py @@ -18,42 +18,73 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -import argparse -import glob +import os.path import subprocess -from collections import defaultdict +import sys -template = '''package lila.i18n -// This file is generated by bin/gen/timeago-to-scala.py -// Do not edit it manually! -object TimeagoLocales {{ - val js: Map[String, String] = Map( - {cases} - ) -}} -''' +def main(args): + if not args: + print("// Usage:", file=sys.stderr) + print("// $ git clone https://github.com/hustcc/timeago.js", file=sys.stderr) + print("// $ ./bin/gen/timeago-to-scala.py timeago.js/src/lang/*.js > modules/i18n/src/main/TimeagoLocales.scala", file=sys.stderr) + return 1 -case_template = ''' "{key}" -> """{contents}"""''' + print("// This file is generated by bin/gen/timeago-to-scala.py.") + print("// Do not edit it manually!") + print() + print("package lila.i18n") + print() + print("object TimeagoLocales {") + print(" val js: Map[String, String] = Map(") -def main(): - parser = argparse.ArgumentParser() - parser.add_argument("locale_dir", help="The path to the timeago locales") - args = parser.parse_args() + first = True - cases = {} - - locale_dir = args.locale_dir - for file in glob.glob("{}/*.js".format(locale_dir)): - locale_key = file.split("/")[-1].replace(".js", "") - if locale_key == "locales" or locale_key == "en_short": + for arg in sorted(args, key=os.path.basename): + if not arg.endswith(".js") or arg.endswith("index.js"): + print(" // Skipping file: {}".format(arg), file=sys.stderr) continue - contents = subprocess.run(['uglifyjs', '-c', '-m', '--', file], stdout=subprocess.PIPE).stdout.decode("utf-8") - contents = contents.replace("module.exports=", "lichess.timeagoLocale=") - contents = "(function(){" + contents + "})()" - cases[locale_key] = case_template.format(key=locale_key, contents=contents) - cases = [v for k,v in sorted(cases.items())] - print(template.format(cases=",\n".join(cases))) + + locale = os.path.basename(arg).replace(".js", "") + + if first: + first = False + else: + print(",") + + if locale == "en_short": + print(" // Hardcoded locale: en", file=sys.stderr) + print(''' "en" -> """lichess.timeagoLocale=function(s,n){return[["just now","right now"],["%s seconds ago","in %s seconds"],["1 minute ago","in 1 minute"],["%s minutes ago","in %s minutes"],["1 hour ago","in 1 hour"],["%s hours ago","in %s hours"],["1 day ago","in 1 day"],["%s days ago","in %s days"],["1 week ago","in 1 week"],["%s weeks ago","in %s weeks"],["1 month ago","in 1 month"],["%s months ago","in %s months"],["1 year ago","in 1 year"],["%s years ago","in %s years"]][n]};"""''', end="") + continue + + print(" // {} -> {}".format(arg, locale), file=sys.stderr) + + with open(arg) as f: + js = postprocess(uglifyjs(preprocess(f.read()))) + print(''' "{}" -> """{}"""'''.format(locale, js), end="") + + print() + print(" )") + print("}") + + return 0 + + +def preprocess(js): + return js.replace("export default ", "module.exports=") + + +def uglifyjs(js): + p = subprocess.Popen(["uglifyjs", "--compress", "--mangle"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=sys.stderr) + stdout, stderr = p.communicate(js.encode("utf-8")) + if p.returncode != 0: + sys.exit(p.returncode) + return stdout.decode("utf-8") + + +def postprocess(js): + return "(function(){" + js.replace("module.exports=", "lichess.timeagoLocale=") + "})()" + if __name__ == '__main__': - main() + sys.exit(main(sys.argv[1:]))