跳转到内容

模块讨论:Vgname/sandbox

页面内容不支持其他语言。
添加话题
维基百科,自由的百科全书
For Each element In group ... Next在话题“新设计”中的最新留言:2个月前

新设计

[编辑]

(我知道这是沙盒的讨论页)

现在的模组代码太烂,一直都有想重新写的想法。最近提到vgname要不要用改用注脚标注的问题,正好有了重写的动机。虽然讨论没有结论,但这次可以留个开关参数,要启用直接改参数就ok。

另外四年前有提到标注马新译名的问题。这个也是最头痛的问题,以前三个地区写条件硬拗就很麻烦了,六个区域还真没想到怎么处理。不过刚才灵光一现,发现只要先跑一遍填汉字的区域,再跑一遍填代码的区域,原来多用一张表就能搞定😂

做了一个示例,各地用字预览了一下,看起来应该是符合期望:

中国大陆译作“寂静岭”,香港译作“鬼魅山房”,台湾译作“沉默之丘”

代码先放到这里,稍后再移植到lua上。最难的工作已经解决,后面应该一路顺风了😂

"""各地译名处理(用Python寫的伪Lua程式碼)"""

from typing import Literal, Any

type RegionCode = Literal["cn", "hk", "mo", "my", "sg", "tw"]


region_table: dict[RegionCode, str] = {
    "cn": "中国大陆",
    "hk": "香港",
    "mo": "澳門",
    "my": "马来西亚",
    "sg": "新加坡",
    "tw": "台灣",
}


def with_conversion_tag(text: str, flag: bool | str = True) -> str:
    """Applies an inline content conversion tag to the text according to the flag.

    * If the flag is falsy, the text is returned as is (i.e., allowed to be automatically converted).
    * If the flag is 'strict', the text is wrapped in no conversion tag (-{...}-).
    * Otherwise, the text is wrapped in character-based convention tag (-{zh; zh-hans; zh-hant;|...}-).

    :param text: The text to be processed.
    :param flag: The flag that determines the type of conversion label to apply.
    :return: The processed text with or without the conversion tag.
    """

    if flag is False:
        return text
    if flag == "strict":
        return "-{" f"{text}" "}-"
    return "-{zh; zh-hans; zh-hant;|" f"{text}" "}-"


def get_translations(args: dict[Any, str]) -> dict[RegionCode, str]:
    """..."""

    result = {}
    for k, _ in args.items():
        if k in region_table:
            result[k] = args[k]
    return result


def group_regions_by_title(
    data: dict[RegionCode, str],
) -> list[tuple[str, list[RegionCode]]]:
    """..."""

    grouped_title_data, pending_data = {}, {}
    for region, title in data.items():
        if title in region_table:
            pending_data[region] = title
        else:
            grouped_title_data.setdefault(title, []).append(region)

    for source_region, target_region in pending_data.items():
        for title, regions in grouped_title_data.items():
            if target_region in regions:
                regions.append(source_region)

    result = []
    for k, value in grouped_title_data.items():
        result.append((k, sorted(value)))
    result.sort(key=lambda item: item[1][0])

    return result


def get_convertion_rule(data: list[tuple[str, list[RegionCode]]], region: RegionCode):
    """..."""

    text_frags = []
    for title, regions in data:
        if region not in regions:
            text = "、".join([region_table[x] for x in regions])
            text += "常用英文" if title == "en" else f"译作“{title}”"
            text_frags.append(text)
    return ",".join(text_frags)


def core(args: dict[Any, str]) -> str:
    """The main function."""
    translations = get_translations(args)
    grouped_data = group_regions_by_title(translations)
    regions = []
    for group in grouped_data:
        regions.extend(group[1])
    result_frags = []
    for region in regions:
        msg = get_convertion_rule(grouped_data, region)
        msg = f"zh-{region}:{with_conversion_tag(msg)};"
        result_frags.append(msg)
    return "-{" f"{' '.join(result_frags)}" "}-"


if __name__ == "__main__":
    args0 = {
        1: "最终幻想系列",
        "tw": "en",
        "cn": "最终幻想",
        "hk": "cn",
        "sg": "en",
    }
    print(core(args0))
    # -{
    # zh-cn:-{zh; zh-hans; zh-hant;|新加坡、台灣常用英文}-;
    # zh-hk:-{zh; zh-hans; zh-hant;|新加坡、台灣常用英文}-;
    # zh-sg:-{zh; zh-hans; zh-hant;|中国大陆、香港译作“最终幻想”}-;
    # zh-tw:-{zh; zh-hans; zh-hant;|中国大陆、香港译作“最终幻想”}-;
    # }-

    args1 = {
        1: "沉默之丘系列",
        "ja": "サイレントヒル",
        "en": "Silent Hill",
        "tw": "沉默之丘",
        "cn": "寂静岭",
        "hk": "鬼魅山房",
        "sg": "en",
        "my": "en",
    }
    print(core(args1))
    # -{
    # zh-cn:-{zh; zh-hans; zh-hant;|香港译作“鬼魅山房”,马来西亚、新加坡常用英文,台灣译作“沉默之丘”}-;
    # zh-hk:-{zh; zh-hans; zh-hant;|中国大陆译作“寂静岭”,马来西亚、新加坡常用英文,台灣译作“沉默之丘”}-;
    # zh-my:-{zh; zh-hans; zh-hant;|中国大陆译作“寂静岭”,香港译作“鬼魅山房”,台灣译作“沉默之丘”}-;
    # zh-sg:-{zh; zh-hans; zh-hant;|中国大陆译作“寂静岭”,香港译作“鬼魅山房”,台灣译作“沉默之丘”}-;
    # zh-tw:-{zh; zh-hans; zh-hant;|中国大陆译作“寂静岭”,香港译作“鬼魅山房”,马来西亚、新加坡常用英文}-;
    # }-

--For Each element In group ... Next 2025年2月15日 (六) 15:52 (UTC)回复