anki/pylib/anki/_backend/genfluent.py

83 lines
2.1 KiB
Python
Raw Normal View History

# 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
import stringcase
strings_json, outfile = sys.argv[1:]
modules = json.load(open(strings_json))
def legacy_enum() -> str:
out = ["class LegacyTranslationEnum(enum.Enum):"]
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}")
return "\n".join(out) + "\n"
def methods() -> str:
out = [
"class GeneratedTranslations:",
" def _translate(self, module: int, translation: int, args: Dict) -> str:",
" raise Exception('not implemented')",
]
for module in modules:
for translation in module["translations"]:
key = translation["key"].replace("-", "_")
arg_types = get_arg_types(translation["variables"])
args = get_args(translation["variables"])
doc = translation["text"]
out.append(
f"""
def {key}(self, {arg_types}) -> str:
r''' {doc} '''
return self._translate({module["index"]}, {translation["index"]}, {{{args}}})
"""
)
return "\n".join(out) + "\n"
def get_arg_types(args: List[str]) -> str:
return ", ".join([f"{stringcase.snakecase(arg)}: FluentVariable" for arg in args])
def get_args(args: List[str]) -> str:
return ", ".join([f'"{arg}": {stringcase.snakecase(arg)}' for arg in args])
out = ""
out += legacy_enum()
out += methods()
open(outfile, "wb").write(
(
'''# Copyright: Ankitects Pty Ltd and contributors
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
# pylint: skip-file
from __future__ import annotations
"""
This file is automatically generated from the *.ftl files.
"""
import enum
from typing import Dict, Union
FluentVariable = Union[str, int, float]
'''
+ out
).encode("utf8")
)