跳转到内容

模組討論:Vgname/sandbox

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

新設計

[编辑]

(我知道這是沙盒的討論頁)

現在的模組代碼太爛,一直都有想重新寫的想法。最近提到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)回复