# Source code for matchtable.i18n

    import json
    from typing import Literal, Self, TypedDict

    class UidRef(TypedDict):
        uid_ref: str
        uid_type: Literal["i18n"]

    class I18nWord(TypedDict):
        """A word.

        Args:
            key:
                Resource key for the vocabulary.
            ja:
                Japanese for the vocabulary.
            en:
                English for the vocabulary.
            zh_cn:
                Chinese for the vocabulary.
        """

        key: str
        ja: str | int | None
        en: str | int | None
        zh_cn: str | int | None

    class I18nResourcesWriter:
        """A writer for an i18n resource."""

        def __init__(self):
            self.res = {}
            for lang in ["ja", "en", "zh_cn"]:
                self.res[lang] = None

        def add_word(self, word: I18nWord) -> Self:
            """Add a word to the resources."""
            langs: list[Literal["ja", "en", "zh_cn"]] = ["ja", "en", "zh_cn"]
            for lang in langs:
                if word[lang] is None:
                    continue
                if self.res[lang] is None:
                    self.res[lang] = {}
                self.res[lang][word["key"]] = word[lang]
            return self

        def write(self, path: str) -> bool:
            """Write i18n resources to the specified path.

            Args:
                path:
                    Output json path.

            Returns:
                bool:
                    Is writing success or fail.
            """
            res = {}
            for lang in ["ja", "en", "zh_cn"]:
                res_lang = "zh-CN" if lang == "zh_cn" else lang
                res[res_lang] = self.res[lang]
            try:
                with open(path, "w") as f:
                    json.dump(res, f, allow_nan=False)
                return True
            except Exception:
                return False