如何用另一本词典中的内容替换一个词典中缺少的内容?

时间:2019-01-03 14:17:31

标签: dictionary lua

因此,我正在为游戏翻译,并且我有不同的词典。如果没有一种语言的翻译,我想将其设置为英文翻译。我尝试组合字典的每种方法最终都效率极低。

这是一些简化的例子

local translation-sr = {
    Buttons = {
        Confirm = "Потврди";
        Submit = "Унеси";
    };
    Countries = {
        Bloxell = "Блоксел";
        USA = "Сједињене Америчке Државе";
    };
    Firearms = {
        Manufacturers = {
            GenMot = "Џенерални Мотори";
            Intratec = "Интратек";
            TF = "ТФ Оружје";
        };
    };
};

local translation-en = {
    Buttons = {
        Confirm = "Confirm";
        Purchase = "Purchase";
        Submit = "Submit";
    };
    Countries = {
        Bloxell = "Bloxell";
        USA = "United States";
    };
    Firearms = {
        Manufacturers = {
            GenMot = "General Motors";
            Intratec = "Intratec ";
            TF = "TF Armaments";
        };
    };
    Languages = {
        Belarusian = "Belarusian";
        English = "English";
        French = "French";
        German = "German";
        Italian = "Italian";
        Russian = "Russian";
        Serbian = "Serbian";
        Spanish = "Spanish";
    };
};

2 个答案:

答案 0 :(得分:1)

我想你想做这样的事情

setmetatable(translation_sr.Buttons,{__index=translation_en.Buttons})

用于所有叶子子表。如果只有几个子表,则可以手动执行此操作。

答案 1 :(得分:-1)

我相信您应该使用一个元表来完成所需的工作。

我假设您将始终使用英语默认单词进行索引。有了它,您可以执行以下操作。

select value

此函数只返回表中不存在的密钥。

local function default(t,k)
    return k
end

local translation_sr = {
    Button = setmetatable({
        Confirm = "Потврди",
        Submit = "Унеси",
    },
    { __index = default }),

    Countries = setmetatable({
        ["Bloxell"] = "Блоксел",
        ["United States"]= "Сједињене Америчке Државе",
    }, 
    { __index = default }),

    Firearms = {
        Manufacturers = setmetatable({
            ["General Motors"] = "Џенерални Мотори",
            ["Intratec"] = "Интратек",
            ["TF Armaments"] = "ТФ Оружје",
        }, 
        { __index = default }),
    },
}

假设此键为英文单词,您将使用默认值local function default(t,k) return k end 作为返回值,您将从"Purchase"返回"Purchase"。此方法不需要translation_sr

相关问题