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
|
2021-03-26 08:54:07 +01:00
|
|
|
from typing import List, Literal, TypedDict
|
2021-03-26 00:40:41 +01:00
|
|
|
|
|
|
|
import stringcase
|
|
|
|
|
2021-10-07 15:31:49 +02:00
|
|
|
strings_json, translate_out, modules_out = sys.argv[1:]
|
2021-06-11 12:12:38 +02:00
|
|
|
modules = json.load(open(strings_json, encoding="utf8"))
|
2021-03-26 00:40:41 +01:00
|
|
|
|
|
|
|
|
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 methods() -> str:
|
2021-10-07 15:31:49 +02:00
|
|
|
out = [ """import type { FluentVariable } from "@fluent/bundle";
|
|
|
|
import { getMessage } from "./i18n";
|
|
|
|
|
|
|
|
function translate(key: string, args: Record<string, FluentVariable> = {}): string {
|
|
|
|
return getMessage(key, args) ?? `missing key: ${key}`;
|
|
|
|
}""" ]
|
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 11:23:43 +01:00
|
|
|
/** {doc} */
|
|
|
|
export function {key}({arg_types}): string {{
|
2021-10-07 15:31:49 +02:00
|
|
|
return translate("{translation["key"]}"{args})
|
2021-03-26 11:23:43 +01:00
|
|
|
}}
|
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"
|
|
|
|
|
|
|
|
|
2021-03-26 12:34:02 +01:00
|
|
|
def map_args_to_real_names(args: List[Variable]) -> str:
|
2021-04-14 10:22:02 +02:00
|
|
|
return (
|
|
|
|
"{"
|
|
|
|
+ ", ".join(
|
|
|
|
[f'"{arg["name"]}": args.{typescript_arg_name(arg)}' for arg in args]
|
|
|
|
)
|
|
|
|
+ "}"
|
2021-03-26 12:34:02 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-03-26 08:54:07 +01:00
|
|
|
def get_args(args: List[Variable]) -> str:
|
|
|
|
if not args:
|
|
|
|
return ""
|
|
|
|
else:
|
2021-03-26 12:34:02 +01:00
|
|
|
for arg in args:
|
|
|
|
if typescript_arg_name(arg) != arg["name"]:
|
|
|
|
# we'll need to map variables to their fluent equiv
|
|
|
|
return ", " + map_args_to_real_names(args)
|
|
|
|
|
|
|
|
# variable names match, reference object instead
|
2021-03-26 08:54:07 +01:00
|
|
|
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
|
|
|
|
2021-04-14 10:22:02 +02:00
|
|
|
|
2021-03-26 12:22:37 +01:00
|
|
|
def module_names() -> str:
|
|
|
|
buf = "export enum ModuleName {\n"
|
|
|
|
for module in modules:
|
|
|
|
name = module["name"]
|
|
|
|
upper = name.upper()
|
|
|
|
buf += f' {upper} = "{name}",\n'
|
|
|
|
buf += "}\n"
|
|
|
|
return buf
|
|
|
|
|
2021-03-26 00:40:41 +01:00
|
|
|
|
2021-10-07 15:31:49 +02:00
|
|
|
def write(outfile, out) -> None:
|
|
|
|
open(outfile, "wb").write(
|
|
|
|
(
|
|
|
|
f"""// Copyright: Ankitects Pty Ltd and contributors
|
2021-03-26 00:40:41 +01:00
|
|
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
|
|
|
"""
|
2021-10-07 15:31:49 +02:00
|
|
|
+ out
|
|
|
|
).encode("utf8")
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
write(translate_out, str(methods()))
|
|
|
|
write(modules_out, str(module_names()))
|