lotrointerface.com
Search Downloads

LoTROInterface SVN SequenceBars

[/] [trunk/] [Thurallor/] [Common/] [Utils/] [Locale.lua] - Rev 20

Go to most recent revision | Compare with Previous | Blame | View Log

Locale = class(Turbine.Object);

function Locale:Constructor(language)

    -- We don't need separate treatment for U.S. and British English.
    if (language == Turbine.Language.EnglishGB) then
        language = Turbine.Language.English;
    end
    self.language = language;

    -- Global table for containing localized text strings
    self.text = {};

    self.contextDelimiter = "/";
    self:SetContext("/");
end

function Locale:AddText(newText)
    DeepTableCopy(newText, self.text);
end

function Locale:SetLanguage(language)
    self.language = language;
end

function Locale:GetLanguage()
    return self.language;
end

function Locale:SetContext(newContext)
--Puts("Entering context " .. newContext .. "; " .. Turbine.Engine.GetCallStack());
    local previousContext = self.contextStr;
    if (string.sub(newContext, 1, 1) == self.contextDelimiter) then
        self.context = self.text;
        self.contextStr = self.contextDelimiter;
        newContext = string.sub(newContext, 2, -1);
    end
    for c in string.gmatch(newContext, "[^" .. self.contextDelimiter .. "]+") do
        if (not self.context[c]) then
            Puts("Can't find " .. c .. " in " .. self.contextStr);
            break;
        end
        self.context = self.context[c];
        self.contextStr = self.contextStr .. c .. self.contextDelimiter;
    end
    return previousContext;
end

function Locale:GetContext()
    return self.contextStr;
end

function Locale:GetNumber(itemName)
    return tonumber(self:GetText(itemName));
end

function Locale:GetText(itemName)
    if (type(itemName) ~= "string") then
        error("Argument (" .. Serialize(itemName) .. ") is not a string.", 2);
    end
    context, remainder = string.match(itemName, "^(.*" .. self.contextDelimiter .. ")([^" .. self.contextDelimiter .. "]+)$");
    if (context) then
        -- Temporarily change to the new context, get item, then return to previous context
        local prevContext = self:SetContext(context);
        local text = self:GetText(remainder);
        self:SetContext(prevContext);
        return text;
    end
    local item = self.context[itemName];
    if (not item) then
        return "(" .. tostring(itemName) .. " not found)";
    end
    local value = item[self.language];
    if (not value) then
        value = "(" .. itemName .. " not localized)";
    elseif (value == "?") then
        value = item[Turbine.Language.English];
        if (type(value) == "string") then
            value = value .. "†";
        end
    end
    return value;
end

function Locale:GetItem(text)
    for name, translations in pairs(self.context) do
        if (translations[self.language] == text) then
            return name;
        end
    end
    return nil;
end

function Locale:GetItems(context)
    local prevContext = self:GetContext();
    if (context) then
        self:SetContext(context);
    end
    local items = {};
    for name, translations in pairs(self.context) do
        table.insert(items, name);
    end
    if (context) then
        self:SetContext(prevContext);
    end
    return items;
end

-- Create a single instance of this object
if (not _G.L) then
    _G.L = Locale(Turbine.Engine:GetLanguage());
--_G.L = Locale(Turbine.Language.German);
end

Go to most recent revision | Compare with Previous | Blame


All times are GMT -5. The time now is 10:22 PM.


Our Network
EQInterface | EQ2Interface | Minion | WoWInterface | ESOUI | LoTROInterface | MMOUI | Swtorui