2021-03-26 00:40:41 +01:00
|
|
|
# Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
|
|
|
import json
|
|
|
|
import sys
|
|
|
|
from typing import List
|
2021-03-26 08:54:07 +01:00
|
|
|
from typing import List, Literal, TypedDict
|
2021-03-26 00:40:41 +01:00
|
|
|
|
|
|
|
import stringcase
|
|
|
|
|
|
|
|
strings_json, outfile = sys.argv[1:]
|
|
|
|
modules = json.load(open(strings_json))
|
|
|
|
|
|
|
|
|
2021-03-26 08:54:07 +01:00
|
|
|
class Variable(TypedDict):
|
|
|
|
name: str
|
|
|
|
kind: Literal["Any", "Int", "String", "Float"]
|
|
|
|
|
|
|
|
|
2021-03-26 00:40:41 +01:00
|
|
|
def legacy_enum() -> str:
|
|
|
|
out = ["export enum LegacyEnum {"]
|
|
|
|
for module in modules:
|
|
|
|
for translation in module["translations"]:
|
|
|
|
key = stringcase.constcase(translation["key"])
|
|
|
|
value = module["index"] * 1000 + translation["index"]
|
|
|
|
out.append(f" {key} = {value},")
|
|
|
|
|
|
|
|
out.append("}")
|
|
|
|
return "\n".join(out) + "\n"
|
|
|
|
|
|
|
|
|
|
|
|
def methods() -> str:
|
|
|
|
out = [
|
2021-03-26 08:54:07 +01:00
|
|
|
"export class GeneratedTranslations {",
|
|
|
|
" translate(key: string, args?: Record<string, any>): string { return 'nyi' } ",
|
2021-03-26 00:40:41 +01:00
|
|
|
]
|
|
|
|
for module in modules:
|
|
|
|
for translation in module["translations"]:
|
2021-03-26 08:54:07 +01:00
|
|
|
key = stringcase.camelcase(translation["key"].replace("-", "_"))
|
|
|
|
arg_types = get_arg_name_and_types(translation["variables"])
|
2021-03-26 00:40:41 +01:00
|
|
|
args = get_args(translation["variables"])
|
|
|
|
doc = translation["text"]
|
|
|
|
out.append(
|
|
|
|
f"""
|
2021-03-26 08:54:07 +01:00
|
|
|
/** {doc} */
|
|
|
|
{key}({arg_types}): string {{
|
|
|
|
return this.translate("{translation["key"]}"{args})
|
|
|
|
}}
|
2021-03-26 00:40:41 +01:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
2021-03-26 08:54:07 +01:00
|
|
|
out.append("}")
|
|
|
|
|
2021-03-26 00:40:41 +01:00
|
|
|
return "\n".join(out) + "\n"
|
|
|
|
|
|
|
|
|
2021-03-26 08:54:07 +01:00
|
|
|
def get_arg_name_and_types(args: List[Variable]) -> str:
|
|
|
|
if not args:
|
|
|
|
return ""
|
|
|
|
else:
|
|
|
|
return (
|
|
|
|
"args: {"
|
|
|
|
+ ", ".join(
|
|
|
|
[f"{typescript_arg_name(arg)}: {arg_kind(arg)}" for arg in args]
|
|
|
|
)
|
|
|
|
+ "}"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def arg_kind(arg: Variable) -> str:
|
|
|
|
if arg["kind"] in ("Int", "Float"):
|
|
|
|
return "number"
|
|
|
|
elif arg["kind"] == "Any":
|
|
|
|
return "number | string"
|
|
|
|
else:
|
|
|
|
return "string"
|
|
|
|
|
|
|
|
|
|
|
|
def get_args(args: List[Variable]) -> str:
|
|
|
|
if not args:
|
|
|
|
return ""
|
|
|
|
else:
|
|
|
|
return ", args"
|
2021-03-26 00:40:41 +01:00
|
|
|
|
|
|
|
|
2021-03-26 08:54:07 +01:00
|
|
|
def typescript_arg_name(arg: Variable) -> str:
|
2021-03-26 10:09:50 +01:00
|
|
|
name = stringcase.camelcase(arg["name"].replace("-", "_"))
|
2021-03-26 08:54:07 +01:00
|
|
|
if name == "new":
|
|
|
|
return "new_"
|
|
|
|
else:
|
|
|
|
return name
|
2021-03-26 00:40:41 +01:00
|
|
|
|
|
|
|
|
|
|
|
out = ""
|
|
|
|
|
|
|
|
out += legacy_enum()
|
2021-03-26 08:54:07 +01:00
|
|
|
out += methods()
|
2021-03-26 00:40:41 +01:00
|
|
|
|
|
|
|
|
|
|
|
open(outfile, "wb").write(
|
|
|
|
(
|
|
|
|
"""// Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
|
|
|
"""
|
|
|
|
+ out
|
|
|
|
).encode("utf8")
|
|
|
|
)
|