flickerstreak@1: --[[ flickerstreak@1: Name: AceLocale-2.0 flickerstreak@1: Revision: $Rev: 18753 $ flickerstreak@1: Developed by: The Ace Development Team (http://www.wowace.com/index.php/The_Ace_Development_Team) flickerstreak@1: Inspired By: Ace 1.x by Turan (turan@gryphon.com) flickerstreak@1: Website: http://www.wowace.com/ flickerstreak@1: Documentation: http://www.wowace.com/index.php/AceLocale-2.0 flickerstreak@1: SVN: http://svn.wowace.com/root/trunk/Ace2/AceLocale-2.0 flickerstreak@1: Description: Localization library for addons to use to handle proper flickerstreak@1: localization and internationalization. flickerstreak@1: Dependencies: AceLibrary flickerstreak@1: ]] flickerstreak@1: flickerstreak@1: local MAJOR_VERSION = "AceLocale-2.0" flickerstreak@1: local MINOR_VERSION = "$Revision: 18753 $" flickerstreak@1: flickerstreak@1: if not AceLibrary then error(MAJOR_VERSION .. " requires AceLibrary.") end flickerstreak@1: if not AceLibrary:IsNewVersion(MAJOR_VERSION, MINOR_VERSION) then return end flickerstreak@1: flickerstreak@1: local AceLocale = {} flickerstreak@1: flickerstreak@1: local DEFAULT_LOCALE = "enUS" flickerstreak@1: local _G = getfenv(0) flickerstreak@1: flickerstreak@1: local __baseTranslations__, __debugging__, __translations__, __baseLocale__, __translationTables__, __reverseTranslations__, __strictness__ flickerstreak@1: flickerstreak@1: local __call = function(self, key1, key2) flickerstreak@1: if key2 then flickerstreak@1: return self[key1][key2] flickerstreak@1: else flickerstreak@1: return self[key1] flickerstreak@1: end flickerstreak@1: end flickerstreak@1: flickerstreak@1: local rawget = rawget flickerstreak@1: local rawset = rawset flickerstreak@1: local type = type flickerstreak@1: flickerstreak@1: local lastSelf flickerstreak@1: flickerstreak@1: local __index = function(self, key) flickerstreak@1: lastSelf = self flickerstreak@1: local value = (rawget(self, __translations__) or AceLocale.prototype)[key] flickerstreak@1: rawset(self, key, value) flickerstreak@1: return value flickerstreak@1: end flickerstreak@1: flickerstreak@1: local __newindex = function(self, k, v) flickerstreak@1: if type(v) ~= "function" and type(k) ~= "table" then flickerstreak@1: AceLocale.error(self, "Cannot change the values of an AceLocale instance.") flickerstreak@1: end flickerstreak@1: rawset(self, k, v) flickerstreak@1: end flickerstreak@1: flickerstreak@1: local __tostring = function(self) flickerstreak@1: if type(rawget(self, 'GetLibraryVersion')) == "function" then flickerstreak@1: return self:GetLibraryVersion() flickerstreak@1: else flickerstreak@1: return "AceLocale(" .. self[__name__] .. ")" flickerstreak@1: end flickerstreak@1: end flickerstreak@1: flickerstreak@1: local refixInstance = function(instance) flickerstreak@1: if getmetatable(instance) then flickerstreak@1: setmetatable(instance, nil) flickerstreak@1: end flickerstreak@1: local translations = instance[__translations__] flickerstreak@1: if translations then flickerstreak@1: if getmetatable(translations) then flickerstreak@1: setmetatable(translations, nil) flickerstreak@1: end flickerstreak@1: local baseTranslations = instance[__baseTranslations__] flickerstreak@1: if getmetatable(baseTranslations) then flickerstreak@1: setmetatable(baseTranslations, nil) flickerstreak@1: end flickerstreak@1: if translations == baseTranslations or instance[__strictness__] then flickerstreak@1: setmetatable(instance, { flickerstreak@1: __index = __index, flickerstreak@1: __newindex = __newindex, flickerstreak@1: __call = __call, flickerstreak@1: __tostring = __tostring flickerstreak@1: }) flickerstreak@1: flickerstreak@1: setmetatable(translations, { flickerstreak@1: __index = AceLocale.prototype flickerstreak@1: }) flickerstreak@1: else flickerstreak@1: setmetatable(instance, { flickerstreak@1: __index = __index, flickerstreak@1: __newindex = __newindex, flickerstreak@1: __call = __call, flickerstreak@1: __tostring = __tostring flickerstreak@1: }) flickerstreak@1: flickerstreak@1: setmetatable(translations, { flickerstreak@1: __index = baseTranslations, flickerstreak@1: }) flickerstreak@1: flickerstreak@1: setmetatable(baseTranslations, { flickerstreak@1: __index = AceLocale.prototype, flickerstreak@1: }) flickerstreak@1: end flickerstreak@1: else flickerstreak@1: setmetatable(instance, { flickerstreak@1: __index = __index, flickerstreak@1: __newindex = __newindex, flickerstreak@1: __call = __call, flickerstreak@1: __tostring = __tostring, flickerstreak@1: }) flickerstreak@1: end flickerstreak@1: end flickerstreak@1: flickerstreak@1: function AceLocale:new(name) flickerstreak@1: error(MAJOR_VERSION .. " is not supported in WoW 2.0", 2) flickerstreak@1: self:argCheck(name, 2, "string") flickerstreak@1: flickerstreak@1: if self.registry[name] and type(rawget(self.registry[name], 'GetLibraryVersion')) ~= "function" then flickerstreak@1: return self.registry[name] flickerstreak@1: end flickerstreak@1: flickerstreak@1: local self = { flickerstreak@1: [__strictness__] = false, flickerstreak@1: [__name__] = name, flickerstreak@1: } flickerstreak@1: refixInstance(self) flickerstreak@1: flickerstreak@1: AceLocale.registry[name] = self flickerstreak@1: return self flickerstreak@1: end flickerstreak@1: flickerstreak@1: setmetatable(AceLocale, { __call = AceLocale.new }) flickerstreak@1: flickerstreak@1: AceLocale.prototype = {} flickerstreak@1: AceLocale.prototype.class = AceLocale flickerstreak@1: flickerstreak@1: function AceLocale.prototype:EnableDebugging() flickerstreak@1: if rawget(self, __baseTranslations__) then flickerstreak@1: AceLocale:error("Cannot enable debugging after a translation has been registered.") flickerstreak@1: end flickerstreak@1: rawset(self, __debugging__, true) flickerstreak@1: end flickerstreak@1: flickerstreak@1: function AceLocale.prototype:RegisterTranslations(locale, func) flickerstreak@1: AceLocale.argCheck(self, locale, 2, "string") flickerstreak@1: AceLocale.argCheck(self, func, 3, "function") flickerstreak@1: flickerstreak@1: if locale == rawget(self, __baseLocale__) then flickerstreak@1: AceLocale.error(self, "Cannot provide the same locale more than once. %q provided twice.", locale) flickerstreak@1: end flickerstreak@1: flickerstreak@1: if rawget(self, __baseTranslations__) and GetLocale() ~= locale then flickerstreak@1: if rawget(self, __debugging__) then flickerstreak@1: local t = func() flickerstreak@1: func = nil flickerstreak@1: if type(t) ~= "table" then flickerstreak@1: AceLocale.error(self, "Bad argument #3 to `RegisterTranslations'. function did not return a table. (expected table, got %s)", type(t)) flickerstreak@1: end flickerstreak@1: self[__translationTables__][locale] = t flickerstreak@1: t = nil flickerstreak@1: end flickerstreak@1: func = nil flickerstreak@1: return flickerstreak@1: end flickerstreak@1: local t = func() flickerstreak@1: func = nil flickerstreak@1: if type(t) ~= "table" then flickerstreak@1: AceLocale.error(self, "Bad argument #3 to `RegisterTranslations'. function did not return a table. (expected table, got %s)", type(t)) flickerstreak@1: end flickerstreak@1: flickerstreak@1: rawset(self, __translations__, t) flickerstreak@1: if not rawget(self, __baseTranslations__) then flickerstreak@1: rawset(self, __baseTranslations__, t) flickerstreak@1: rawset(self, __baseLocale__, locale) flickerstreak@1: for key,value in pairs(t) do flickerstreak@1: if value == true then flickerstreak@1: t[key] = key flickerstreak@1: end flickerstreak@1: end flickerstreak@1: else flickerstreak@1: for key, value in pairs(self[__translations__]) do flickerstreak@1: if not rawget(self[__baseTranslations__], key) then flickerstreak@1: AceLocale.error(self, "Improper translation exists. %q is likely misspelled for locale %s.", key, locale) flickerstreak@1: end flickerstreak@1: if value == true then flickerstreak@1: AceLocale.error(self, "Can only accept true as a value on the base locale. %q is the base locale, %q is not.", rawget(self, __baseLocale__), locale) flickerstreak@1: end flickerstreak@1: end flickerstreak@1: end flickerstreak@1: refixInstance(self) flickerstreak@1: if rawget(self, __debugging__) then flickerstreak@1: if not rawget(self, __translationTables__) then flickerstreak@1: rawset(self, __translationTables__, {}) flickerstreak@1: end flickerstreak@1: self[__translationTables__][locale] = t flickerstreak@1: end flickerstreak@1: t = nil flickerstreak@1: end flickerstreak@1: flickerstreak@1: function AceLocale.prototype:SetStrictness(strict) flickerstreak@1: AceLocale.argCheck(self, strict, 2, "boolean") flickerstreak@1: local mt = getmetatable(self) flickerstreak@1: if not mt then flickerstreak@1: AceLocale.error(self, "Cannot call `SetStrictness' without a metatable.") flickerstreak@1: end flickerstreak@1: if not rawget(self, __translations__) then flickerstreak@1: AceLocale.error(self, "No translations registered.") flickerstreak@1: end flickerstreak@1: rawset(self, __strictness__, strict) flickerstreak@1: refixInstance(self) flickerstreak@1: end flickerstreak@1: flickerstreak@1: function AceLocale.prototype:GetTranslationStrict(text, sublevel) flickerstreak@1: AceLocale.argCheck(self, text, 1, "string") flickerstreak@1: local translations = rawget(self, __translations__) flickerstreak@1: if not translations then flickerstreak@1: AceLocale.error(self, "No translations registered") flickerstreak@1: end flickerstreak@1: if sublevel then flickerstreak@1: local t = rawget(translations, text) flickerstreak@1: if type(t) ~= "table" then flickerstreak@1: AceLocale.error(self, "Strict translation %q::%q does not exist", text, sublevel) flickerstreak@1: end flickerstreak@1: local value = t[sublevel] flickerstreak@1: if not value then flickerstreak@1: AceLocale.error(self, "Strict translation %q::%q does not exist", text, sublevel) flickerstreak@1: end flickerstreak@1: return value flickerstreak@1: else flickerstreak@1: local value = rawget(translations, text) flickerstreak@1: if not value then flickerstreak@1: AceLocale.error(self, "Strict translation %q does not exist", text) flickerstreak@1: end flickerstreak@1: return value flickerstreak@1: end flickerstreak@1: end flickerstreak@1: flickerstreak@1: function AceLocale.prototype:GetTranslation(text, sublevel) flickerstreak@1: AceLocale.argCheck(self, text, 1, "string") flickerstreak@1: local translations = rawget(self, __translations__) flickerstreak@1: if not translations then flickerstreak@1: AceLocale.error(self, "No translations registered") flickerstreak@1: end flickerstreak@1: if sublevel then flickerstreak@1: local base = self[__baseTranslations__] flickerstreak@1: local standard = rawget(translations, text) flickerstreak@1: local current flickerstreak@1: local baseStandard flickerstreak@1: if not standard then flickerstreak@1: baseStandard = rawget(base, text) flickerstreak@1: current = baseStandard flickerstreak@1: end flickerstreak@1: if not type(current) ~= "table" then flickerstreak@1: AceLocale.error(self, "Loose translation %q::%q does not exist", text, sublevel) flickerstreak@1: end flickerstreak@1: local value = current[sublevel] flickerstreak@1: if not value then flickerstreak@1: if current == baseStandard or type(baseStandard) ~= "table" then flickerstreak@1: AceLocale.error(self, "Loose translation %q::%q does not exist", text, sublevel) flickerstreak@1: end flickerstreak@1: value = baseStandard[sublevel] flickerstreak@1: if not value then flickerstreak@1: AceLocale.error(self, "Loose translation %q::%q does not exist", text, sublevel) flickerstreak@1: end flickerstreak@1: end flickerstreak@1: return value flickerstreak@1: else flickerstreak@1: local value = rawget(translations, text) flickerstreak@1: if not value then flickerstreak@1: AceLocale.error(self, "Loose translation %q does not exist", text) flickerstreak@1: end flickerstreak@1: return value flickerstreak@1: end flickerstreak@1: end flickerstreak@1: flickerstreak@1: local function initReverse(self) flickerstreak@1: rawset(self, __reverseTranslations__, {}) flickerstreak@1: local alpha = self[__translations__] flickerstreak@1: local bravo = self[__reverseTranslations__] flickerstreak@1: for base, localized in pairs(alpha) do flickerstreak@1: bravo[localized] = base flickerstreak@1: end flickerstreak@1: end flickerstreak@1: flickerstreak@1: function AceLocale.prototype:GetReverseTranslation(text) flickerstreak@1: local x = rawget(self, __reverseTranslations__) flickerstreak@1: if not x then flickerstreak@1: if not rawget(self, __translations__) then flickerstreak@1: AceLocale.error(self, "No translations registered") flickerstreak@1: end flickerstreak@1: initReverse(self) flickerstreak@1: x = self[__reverseTranslations__] flickerstreak@1: end flickerstreak@1: local translation = x[text] flickerstreak@1: if not translation then flickerstreak@1: AceLocale.error(self, "Reverse translation for %q does not exist", text) flickerstreak@1: end flickerstreak@1: return translation flickerstreak@1: end flickerstreak@1: flickerstreak@1: function AceLocale.prototype:GetIterator() flickerstreak@1: local x = rawget(self, __translations__) flickerstreak@1: if not x then flickerstreak@1: AceLocale.error(self, "No translations registered") flickerstreak@1: end flickerstreak@1: return next, x, nil flickerstreak@1: end flickerstreak@1: flickerstreak@1: function AceLocale.prototype:GetReverseIterator() flickerstreak@1: local x = rawget(self, __reverseTranslations__) flickerstreak@1: if not x then flickerstreak@1: if not rawget(self, __translations__) then flickerstreak@1: AceLocale.error(self, "No translations registered") flickerstreak@1: end flickerstreak@1: initReverse(self) flickerstreak@1: x = self[__reverseTranslations__] flickerstreak@1: end flickerstreak@1: return next, x, nil flickerstreak@1: end flickerstreak@1: flickerstreak@1: function AceLocale.prototype:HasTranslation(text, sublevel) flickerstreak@1: AceLocale.argCheck(self, text, 1, "string") flickerstreak@1: local x = rawget(self, __translations__) flickerstreak@1: if not x then flickerstreak@1: AceLocale.error(self, "No translations registered") flickerstreak@1: end flickerstreak@1: if sublevel then flickerstreak@1: AceLocale.argCheck(self, sublevel, 2, "string", "nil") flickerstreak@1: return type(rawget(x, text)) == "table" and x[text][sublevel] and true flickerstreak@1: end flickerstreak@1: return rawget(x, text) and true flickerstreak@1: end flickerstreak@1: flickerstreak@1: function AceLocale.prototype:HasReverseTranslation(text) flickerstreak@1: local x = rawget(self, __reverseTranslations__) flickerstreak@1: if not x then flickerstreak@1: if not rawget(self, __translations__) then flickerstreak@1: AceLocale.error(self, "No translations registered") flickerstreak@1: end flickerstreak@1: initReverse(self) flickerstreak@1: x = self[__reverseTranslations__] flickerstreak@1: end flickerstreak@1: return x[text] and true flickerstreak@1: end flickerstreak@1: flickerstreak@1: AceLocale.prototype.GetTableStrict = AceLocale.prototype.GetTranslationStrict flickerstreak@1: AceLocale.prototype.GetTable = AceLocale.prototype.GetTranslation flickerstreak@1: flickerstreak@1: function AceLocale.prototype:Debug() flickerstreak@1: if not rawget(self, __debugging__) then flickerstreak@1: return flickerstreak@1: end flickerstreak@1: local words = {} flickerstreak@1: local locales = {"enUS", "deDE", "frFR", "koKR", "zhCN", "zhTW"} flickerstreak@1: local localizations = {} flickerstreak@1: DEFAULT_CHAT_FRAME:AddMessage("--- AceLocale Debug ---") flickerstreak@1: for _,locale in ipairs(locales) do flickerstreak@1: if not self[__translationTables__][locale] then flickerstreak@1: DEFAULT_CHAT_FRAME:AddMessage(string.format("Locale %q not found", locale)) flickerstreak@1: else flickerstreak@1: localizations[locale] = self[__translationTables__][locale] flickerstreak@1: end flickerstreak@1: end flickerstreak@1: local localeDebug = {} flickerstreak@1: for locale, localization in pairs(localizations) do flickerstreak@1: localeDebug[locale] = {} flickerstreak@1: for word in pairs(localization) do flickerstreak@1: if type(localization[word]) == "table" then flickerstreak@1: if type(words[word]) ~= "table" then flickerstreak@1: words[word] = {} flickerstreak@1: end flickerstreak@1: for bit in pairs(localization[word]) do flickerstreak@1: if type(localization[word][bit]) == "string" then flickerstreak@1: words[word][bit] = true flickerstreak@1: end flickerstreak@1: end flickerstreak@1: elseif type(localization[word]) == "string" then flickerstreak@1: words[word] = true flickerstreak@1: end flickerstreak@1: end flickerstreak@1: end flickerstreak@1: for word in pairs(words) do flickerstreak@1: if type(words[word]) == "table" then flickerstreak@1: for bit in pairs(words[word]) do flickerstreak@1: for locale, localization in pairs(localizations) do flickerstreak@1: if not rawget(localization, word) or not localization[word][bit] then flickerstreak@1: localeDebug[locale][word .. "::" .. bit] = true flickerstreak@1: end flickerstreak@1: end flickerstreak@1: end flickerstreak@1: else flickerstreak@1: for locale, localization in pairs(localizations) do flickerstreak@1: if not rawget(localization, word) then flickerstreak@1: localeDebug[locale][word] = true flickerstreak@1: end flickerstreak@1: end flickerstreak@1: end flickerstreak@1: end flickerstreak@1: for locale, t in pairs(localeDebug) do flickerstreak@1: if not next(t) then flickerstreak@1: DEFAULT_CHAT_FRAME:AddMessage(string.format("Locale %q complete", locale)) flickerstreak@1: else flickerstreak@1: DEFAULT_CHAT_FRAME:AddMessage(string.format("Locale %q missing:", locale)) flickerstreak@1: for word in pairs(t) do flickerstreak@1: DEFAULT_CHAT_FRAME:AddMessage(string.format(" %q", word)) flickerstreak@1: end flickerstreak@1: end flickerstreak@1: end flickerstreak@1: DEFAULT_CHAT_FRAME:AddMessage("--- End AceLocale Debug ---") flickerstreak@1: end flickerstreak@1: flickerstreak@1: setmetatable(AceLocale.prototype, { flickerstreak@1: __index = function(self, k) flickerstreak@1: if type(k) ~= "table" and k ~= 0 and k ~= "GetLibraryVersion" and k ~= "error" and k ~= "assert" and k ~= "argCheck" and k ~= "pcall" then -- HACK: remove "GetLibraryVersion" and such later. flickerstreak@1: AceLocale.error(lastSelf or self, "Translation %q does not exist.", k) flickerstreak@1: end flickerstreak@1: return nil flickerstreak@1: end flickerstreak@1: }) flickerstreak@1: flickerstreak@1: local function activate(self, oldLib, oldDeactivate) flickerstreak@1: AceLocale = self flickerstreak@1: flickerstreak@1: if oldLib then flickerstreak@1: self.registry = oldLib.registry flickerstreak@1: self.__baseTranslations__ = oldLib.__baseTranslations__ flickerstreak@1: self.__debugging__ = oldLib.__debugging__ flickerstreak@1: self.__translations__ = oldLib.__translations__ flickerstreak@1: self.__baseLocale__ = oldLib.__baseLocale__ flickerstreak@1: self.__translationTables__ = oldLib.__translationTables__ flickerstreak@1: self.__reverseTranslations__ = oldLib.__reverseTranslations__ flickerstreak@1: self.__strictness__ = oldLib.__strictness__ flickerstreak@1: self.__name__ = oldLib.__name__ flickerstreak@1: end flickerstreak@1: if not self.__baseTranslations__ then flickerstreak@1: self.__baseTranslations__ = {} flickerstreak@1: end flickerstreak@1: if not self.__debugging__ then flickerstreak@1: self.__debugging__ = {} flickerstreak@1: end flickerstreak@1: if not self.__translations__ then flickerstreak@1: self.__translations__ = {} flickerstreak@1: end flickerstreak@1: if not self.__baseLocale__ then flickerstreak@1: self.__baseLocale__ = {} flickerstreak@1: end flickerstreak@1: if not self.__translationTables__ then flickerstreak@1: self.__translationTables__ = {} flickerstreak@1: end flickerstreak@1: if not self.__reverseTranslations__ then flickerstreak@1: self.__reverseTranslations__ = {} flickerstreak@1: end flickerstreak@1: if not self.__strictness__ then flickerstreak@1: self.__strictness__ = {} flickerstreak@1: end flickerstreak@1: if not self.__name__ then flickerstreak@1: self.__name__ = {} flickerstreak@1: end flickerstreak@1: flickerstreak@1: __baseTranslations__ = self.__baseTranslations__ flickerstreak@1: __debugging__ = self.__debugging__ flickerstreak@1: __translations__ = self.__translations__ flickerstreak@1: __baseLocale__ = self.__baseLocale__ flickerstreak@1: __translationTables__ = self.__translationTables__ flickerstreak@1: __reverseTranslations__ = self.__reverseTranslations__ flickerstreak@1: __strictness__ = self.__strictness__ flickerstreak@1: __name__ = self.__name__ flickerstreak@1: flickerstreak@1: if not self.registry then flickerstreak@1: self.registry = {} flickerstreak@1: else flickerstreak@1: for name, instance in pairs(self.registry) do flickerstreak@1: local name = name flickerstreak@1: local mt = getmetatable(instance) flickerstreak@1: setmetatable(instance, nil) flickerstreak@1: instance[__name__] = name flickerstreak@1: local strict flickerstreak@1: if instance.translations then flickerstreak@1: instance[__translations__], instance.translations = instance.translations flickerstreak@1: instance[__baseLocale__], instance.baseLocale = instance.baseLocale flickerstreak@1: instance[__baseTranslations__], instance.baseTranslations = instance.baseTranslations flickerstreak@1: instance[__debugging__], instance.debugging = instance.debugging flickerstreak@1: instance.reverseTranslations = nil flickerstreak@1: instance[__translationTables__], instance.translationTables = instance.translationTables flickerstreak@1: if mt and mt.__call == oldLib.prototype.GetTranslationStrict then flickerstreak@1: strict = true flickerstreak@1: end flickerstreak@1: else flickerstreak@1: if instance[__strictness__] ~= nil then flickerstreak@1: strict = instance[__strictness__] flickerstreak@1: elseif instance[__translations__] ~= instance[__baseTranslations__] then flickerstreak@1: if getmetatable(instance[__translations__]).__index == oldLib.prototype then flickerstreak@1: strict = true flickerstreak@1: end flickerstreak@1: end flickerstreak@1: end flickerstreak@1: instance[__strictness__] = strict and true or false flickerstreak@1: refixInstance(instance) flickerstreak@1: end flickerstreak@1: end flickerstreak@1: flickerstreak@1: if oldDeactivate then flickerstreak@1: oldDeactivate(oldLib) flickerstreak@1: end flickerstreak@1: end flickerstreak@1: flickerstreak@1: AceLibrary:Register(AceLocale, MAJOR_VERSION, MINOR_VERSION, activate)