#!/usr/bin/python3

# timeago-to-scala - convert 
# https://github.com/hustcc/timeago.js/tree/master/locales to scala
#
# Copyright (C) 2017 Lakin Wecker <lakin@wecker.ca>
#
# 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.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.  

import argparse
import glob
from collections import defaultdict

template = '''object TimeagoLocales {{
    val locales: Map[String, String] = Map(
        {cases}
    )
}}
'''

case_template = '''        "{key}" -> """{contents}"""'''

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("locale_dir", help="The path to the timeago locales")
    args = parser.parse_args()

    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":
            continue
        lines = [l.strip() for l in open(file, "r").readlines()]
        lines = [l for l in lines if l and not l.startswith("//")]
        contents = " ".join(lines)
        contents = contents.replace("module.exports = ", "")
        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)))

if __name__ == '__main__':
    main()

@@ -41,17 +41,16 @@
  convert
- 
 %0A# https
@@ -799,18 +799,16 @@
 enses/%3E.
-  
 %0A%0Aimport
@@ -880,16 +880,35 @@
 te = '''
+package lila.i18n%0A%0A
 object T
@@ -928,22 +928,15 @@
  %7B%7B%0A
-  
   val 
-locale
+j
 s: M
@@ -961,20 +961,16 @@
  = Map(%0A
-    
     %7Bcas
@@ -973,18 +973,16 @@
 %7Bcases%7D%0A
-  
   )%0A%7D%7D%0A'
@@ -1004,20 +1004,16 @@
 te = '''
-    
     %22%7Bke
@@ -1648,24 +1648,126 @@
 ts = %22, %22%22)%0A
+        contents = contents.replace(%22%5D, %5B%22, %22%5D,%5B%22)%0A        contents = contents.replace(%22', '%22, %22','%22)%0A
         case

tweak timeago locale script