flickerstreak@1: --[[ flickerstreak@1: Name: AceLocale-2.2 flickerstreak@22: Revision: $Rev: 40629 $ 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.2 flickerstreak@1: SVN: http://svn.wowace.com/root/trunk/Ace2/AceLocale-2.2 flickerstreak@1: Description: Localization library for addons to use to handle proper flickerstreak@1: localization and internationalization. flickerstreak@1: Dependencies: AceLibrary flickerstreak@7: License: LGPL v2.1 flickerstreak@1: ]] flickerstreak@1: flickerstreak@1: local MAJOR_VERSION = "AceLocale-2.2" flickerstreak@22: local MINOR_VERSION = "$Revision: 40629 $" 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@22: AceLocale.prototype = { class = AceLocale } flickerstreak@1: flickerstreak@1: local BASE_TRANSLATIONS, DEBUGGING, TRANSLATIONS, BASE_LOCALE, TRANSLATION_TABLES, REVERSE_TRANSLATIONS, STRICTNESS, DYNAMIC_LOCALES, CURRENT_LOCALE, NAME flickerstreak@1: flickerstreak@22: local _G = _G flickerstreak@1: local rawget = rawget flickerstreak@1: local rawset = rawset flickerstreak@1: local type = type flickerstreak@22: local pairs = pairs flickerstreak@22: local next = next flickerstreak@22: local getmetatable = getmetatable flickerstreak@22: local setmetatable = setmetatable flickerstreak@22: local GetTime = GetTime flickerstreak@22: local geterrorhandler = geterrorhandler flickerstreak@22: local pcall = pcall flickerstreak@22: local ipairs = ipairs flickerstreak@22: local GetLocale = GetLocale flickerstreak@1: flickerstreak@1: local newRegistries = {} flickerstreak@1: local scheduleClear flickerstreak@1: flickerstreak@1: local lastSelf flickerstreak@22: local strict__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@22: local nonstrict__index = function(self, key) flickerstreak@22: lastSelf = self flickerstreak@22: local t = rawget(self, TRANSLATIONS) flickerstreak@22: if t then flickerstreak@22: local value = rawget(t, key) flickerstreak@22: if value then flickerstreak@22: rawset(self, key, value) flickerstreak@22: return value flickerstreak@22: end flickerstreak@22: end flickerstreak@22: local value = (rawget(self, BASE_TRANSLATIONS) or AceLocale.prototype)[key] flickerstreak@22: rawset(self, key, value) flickerstreak@22: return value flickerstreak@22: 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 function clearCache(self) flickerstreak@22: for k, v in pairs(AceLocale.prototype) do flickerstreak@22: if type(v) == "function" and type(rawget(self, k)) == "function" then flickerstreak@22: self[k] = nil flickerstreak@22: end flickerstreak@22: end flickerstreak@1: if not rawget(self, BASE_TRANSLATIONS) then flickerstreak@1: return flickerstreak@1: end flickerstreak@1: flickerstreak@1: local cache = self[BASE_TRANSLATIONS] flickerstreak@1: rawset(self, REVERSE_TRANSLATIONS, nil) flickerstreak@1: flickerstreak@1: for k in pairs(self) do flickerstreak@1: if rawget(cache, k) ~= nil then flickerstreak@1: self[k] = nil flickerstreak@1: end flickerstreak@1: end flickerstreak@1: rawset(self, 'tmp', true) flickerstreak@1: self.tmp = nil flickerstreak@1: end flickerstreak@1: flickerstreak@22: local strict_instance_mt, nonstrict_instance_mt flickerstreak@22: local baseTranslations_mt flickerstreak@22: flickerstreak@1: local function refixInstance(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[BASE_TRANSLATIONS] flickerstreak@1: if getmetatable(baseTranslations) then flickerstreak@1: setmetatable(baseTranslations, nil) flickerstreak@1: end flickerstreak@1: if translations == baseTranslations or instance[STRICTNESS] then flickerstreak@22: setmetatable(instance, strict_instance_mt) flickerstreak@1: flickerstreak@22: setmetatable(translations, baseTranslations_mt) flickerstreak@1: else flickerstreak@22: setmetatable(instance, nonstrict_instance_mt) flickerstreak@1: flickerstreak@22: setmetatable(baseTranslations, baseTranslations_mt) flickerstreak@1: end flickerstreak@1: else flickerstreak@22: setmetatable(instance, strict_instance_mt) flickerstreak@1: end flickerstreak@1: clearCache(instance) flickerstreak@1: newRegistries[instance] = true flickerstreak@1: scheduleClear() flickerstreak@1: return instance flickerstreak@1: end flickerstreak@1: flickerstreak@1: function AceLocale:new(name) 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: AceLocale.registry[name] = refixInstance({ flickerstreak@1: [STRICTNESS] = false, flickerstreak@1: [NAME] = name, flickerstreak@1: }) flickerstreak@1: newRegistries[AceLocale.registry[name]] = true flickerstreak@1: return AceLocale.registry[name] flickerstreak@1: end flickerstreak@1: flickerstreak@1: function AceLocale.prototype:EnableDebugging() flickerstreak@1: if rawget(self, BASE_TRANSLATIONS) then flickerstreak@1: AceLocale.error(self, "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:EnableDynamicLocales(override) flickerstreak@1: AceLocale.argCheck(self, override, 2, "boolean", "nil") flickerstreak@1: if not override and rawget(self, BASE_TRANSLATIONS) then flickerstreak@1: AceLocale.error(self, "Cannot enable dynamic locales after a translation has been registered.") flickerstreak@1: end flickerstreak@1: if not rawget(self, DYNAMIC_LOCALES) then flickerstreak@1: rawset(self, DYNAMIC_LOCALES, true) flickerstreak@1: if rawget(self, BASE_LOCALE) then flickerstreak@1: if not rawget(self, TRANSLATION_TABLES) then flickerstreak@1: rawset(self, TRANSLATION_TABLES, {}) flickerstreak@1: end flickerstreak@1: self[TRANSLATION_TABLES][self[BASE_LOCALE]] = self[BASE_TRANSLATIONS] flickerstreak@1: self[TRANSLATION_TABLES][self[CURRENT_LOCALE]] = self[TRANSLATIONS] flickerstreak@1: end flickerstreak@1: end 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, BASE_LOCALE) 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, BASE_TRANSLATIONS) and GetLocale() ~= locale then flickerstreak@1: if rawget(self, DEBUGGING) or rawget(self, DYNAMIC_LOCALES) then flickerstreak@1: if not rawget(self, TRANSLATION_TABLES) then flickerstreak@1: rawset(self, TRANSLATION_TABLES, {}) flickerstreak@1: end flickerstreak@1: if self[TRANSLATION_TABLES][locale] then flickerstreak@1: AceLocale.error(self, "Cannot provide the same locale more than once. %q provided twice.", locale) 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: self[TRANSLATION_TABLES][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, BASE_TRANSLATIONS) then flickerstreak@1: rawset(self, BASE_TRANSLATIONS, t) flickerstreak@1: rawset(self, BASE_LOCALE, 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[BASE_TRANSLATIONS], 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, BASE_LOCALE), locale) flickerstreak@1: end flickerstreak@1: end flickerstreak@1: end flickerstreak@1: rawset(self, CURRENT_LOCALE, locale) flickerstreak@7: if not rawget(self, 'reverse') then flickerstreak@7: rawset(self, 'reverse', setmetatable({}, { __index = function(self2, key) flickerstreak@7: local self = AceLocale.reverseToBase[self2] flickerstreak@7: if not rawget(self, REVERSE_TRANSLATIONS) then flickerstreak@7: self:GetReverseTranslation(key) flickerstreak@7: end flickerstreak@7: self.reverse = self[REVERSE_TRANSLATIONS] flickerstreak@7: return self.reverse[key] flickerstreak@7: end })) flickerstreak@7: AceLocale.reverseToBase[self.reverse] = self flickerstreak@7: end flickerstreak@1: refixInstance(self) flickerstreak@1: if rawget(self, DEBUGGING) or rawget(self, DYNAMIC_LOCALES) then flickerstreak@1: if not rawget(self, TRANSLATION_TABLES) then flickerstreak@1: rawset(self, TRANSLATION_TABLES, {}) flickerstreak@1: end flickerstreak@1: self[TRANSLATION_TABLES][locale] = t flickerstreak@1: end flickerstreak@1: t = nil flickerstreak@1: end flickerstreak@1: flickerstreak@1: function AceLocale.prototype:SetLocale(locale) flickerstreak@1: AceLocale.argCheck(self, locale, 2, "string", "boolean") flickerstreak@1: if not rawget(self, DYNAMIC_LOCALES) then flickerstreak@1: AceLocale.error(self, "Cannot call `SetLocale' without first calling `EnableDynamicLocales'.") flickerstreak@1: end flickerstreak@1: if not rawget(self, TRANSLATION_TABLES) then flickerstreak@1: AceLocale.error(self, "Cannot call `SetLocale' without first calling `RegisterTranslations'.") flickerstreak@1: end flickerstreak@1: if locale == true then flickerstreak@1: locale = GetLocale() flickerstreak@1: if not self[TRANSLATION_TABLES][locale] then flickerstreak@1: locale = self[BASE_LOCALE] flickerstreak@1: end flickerstreak@1: end flickerstreak@1: flickerstreak@1: if self[CURRENT_LOCALE] == locale then flickerstreak@1: return flickerstreak@1: end flickerstreak@1: flickerstreak@1: if not self[TRANSLATION_TABLES][locale] then flickerstreak@1: AceLocale.error(self, "Locale %q not registered.", locale) flickerstreak@1: end flickerstreak@1: flickerstreak@1: self[TRANSLATIONS] = self[TRANSLATION_TABLES][locale] flickerstreak@1: self[CURRENT_LOCALE] = locale flickerstreak@1: refixInstance(self) flickerstreak@1: end flickerstreak@1: flickerstreak@1: function AceLocale.prototype:GetLocale() flickerstreak@1: if not rawget(self, TRANSLATION_TABLES) then flickerstreak@1: AceLocale.error(self, "Cannot call `GetLocale' without first calling `RegisterTranslations'.") flickerstreak@1: end flickerstreak@1: return self[CURRENT_LOCALE] flickerstreak@1: end flickerstreak@1: flickerstreak@1: local function iter(t, position) flickerstreak@1: return (next(t, position)) flickerstreak@1: end flickerstreak@1: flickerstreak@1: function AceLocale.prototype:IterateAvailableLocales() flickerstreak@1: if not rawget(self, DYNAMIC_LOCALES) then flickerstreak@1: AceLocale.error(self, "Cannot call `IterateAvailableLocales' without first calling `EnableDynamicLocales'.") flickerstreak@1: end flickerstreak@1: if not rawget(self, TRANSLATION_TABLES) then flickerstreak@1: AceLocale.error(self, "Cannot call `IterateAvailableLocales' without first calling `RegisterTranslations'.") flickerstreak@1: end flickerstreak@1: return iter, self[TRANSLATION_TABLES], nil flickerstreak@1: end flickerstreak@1: flickerstreak@1: function AceLocale.prototype:HasLocale(locale) flickerstreak@1: if not rawget(self, DYNAMIC_LOCALES) then flickerstreak@1: AceLocale.error(self, "Cannot call `HasLocale' without first calling `EnableDynamicLocales'.") flickerstreak@1: end flickerstreak@1: AceLocale.argCheck(self, locale, 2, "string") flickerstreak@1: return rawget(self, TRANSLATION_TABLES) and self[TRANSLATION_TABLES][locale] ~= 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: local function initReverse(self) flickerstreak@7: rawset(self, REVERSE_TRANSLATIONS, setmetatable({}, { __index = function(_, key) flickerstreak@7: AceLocale.error(self, "Reverse translation for %q does not exist", key) flickerstreak@7: end })) flickerstreak@1: local alpha = self[TRANSLATIONS] flickerstreak@1: local bravo = self[REVERSE_TRANSLATIONS] 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:GetTranslation(text) flickerstreak@1: AceLocale.argCheck(self, text, 1, "string", "number") flickerstreak@1: if not rawget(self, TRANSLATIONS) then flickerstreak@1: AceLocale.error(self, "No translations registered") flickerstreak@1: end flickerstreak@1: return self[text] flickerstreak@1: end flickerstreak@1: flickerstreak@1: function AceLocale.prototype:GetStrictTranslation(text) flickerstreak@1: AceLocale.argCheck(self, text, 1, "string", "number") 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: local value = rawget(x, text) flickerstreak@1: if value == nil then flickerstreak@22: local _, ret = pcall(AceLocale.error, self, "Translation %q does not exist for locale %s", text, self[CURRENT_LOCALE]) flickerstreak@22: geterrorhandler()(ret) flickerstreak@22: return text flickerstreak@1: end flickerstreak@1: return value flickerstreak@1: end flickerstreak@1: flickerstreak@1: function AceLocale.prototype:GetReverseTranslation(text) flickerstreak@1: local x = rawget(self, REVERSE_TRANSLATIONS) 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[REVERSE_TRANSLATIONS] flickerstreak@1: end flickerstreak@1: local translation = x[text] flickerstreak@1: if not translation then flickerstreak@22: local _, ret = pcall(AceLocale.error, self, "Reverse translation for %q does not exist", text) flickerstreak@22: geterrorhandler()(ret) flickerstreak@22: return 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, REVERSE_TRANSLATIONS) 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[REVERSE_TRANSLATIONS] flickerstreak@1: end flickerstreak@1: return next, x, nil flickerstreak@1: end flickerstreak@1: flickerstreak@1: function AceLocale.prototype:HasTranslation(text) flickerstreak@1: AceLocale.argCheck(self, text, 1, "string", "number") 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 rawget(x, text) and true flickerstreak@1: end flickerstreak@1: flickerstreak@22: function AceLocale.prototype:HasBaseTranslation(text) flickerstreak@22: AceLocale.argCheck(self, text, 1, "string", "number") flickerstreak@22: local x = rawget(self, BASE_TRANSLATIONS) flickerstreak@22: if not x then flickerstreak@22: AceLocale.error(self, "No translations registered") flickerstreak@22: end flickerstreak@22: return rawget(x, text) and true flickerstreak@22: end flickerstreak@22: flickerstreak@1: function AceLocale.prototype:HasReverseTranslation(text) flickerstreak@1: local x = rawget(self, REVERSE_TRANSLATIONS) 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[REVERSE_TRANSLATIONS] flickerstreak@1: end flickerstreak@7: return rawget(x, text) and true flickerstreak@1: end 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", "esES"} 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[TRANSLATION_TABLES][locale] then flickerstreak@7: DEFAULT_CHAT_FRAME:AddMessage(("Locale %q not found"):format(locale)) flickerstreak@1: else flickerstreak@1: localizations[locale] = self[TRANSLATION_TABLES][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@7: DEFAULT_CHAT_FRAME:AddMessage(("Locale %q complete"):format(locale)) flickerstreak@1: else flickerstreak@7: DEFAULT_CHAT_FRAME:AddMessage(("Locale %q missing:"):format(locale)) flickerstreak@1: for word in pairs(t) do flickerstreak@7: DEFAULT_CHAT_FRAME:AddMessage((" %q"):format(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@22: local _, ret = pcall(AceLocale.error, lastSelf or self, "Translation %q does not exist.", k) flickerstreak@22: geterrorhandler()(ret) flickerstreak@22: return 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: self.frame = oldLib and oldLib.frame or CreateFrame("Frame") flickerstreak@1: self.registry = oldLib and oldLib.registry or {} flickerstreak@1: self.BASE_TRANSLATIONS = oldLib and oldLib.BASE_TRANSLATIONS or {} flickerstreak@1: self.DEBUGGING = oldLib and oldLib.DEBUGGING or {} flickerstreak@1: self.TRANSLATIONS = oldLib and oldLib.TRANSLATIONS or {} flickerstreak@1: self.BASE_LOCALE = oldLib and oldLib.BASE_LOCALE or {} flickerstreak@1: self.TRANSLATION_TABLES = oldLib and oldLib.TRANSLATION_TABLES or {} flickerstreak@1: self.REVERSE_TRANSLATIONS = oldLib and oldLib.REVERSE_TRANSLATIONS or {} flickerstreak@1: self.STRICTNESS = oldLib and oldLib.STRICTNESS or {} flickerstreak@1: self.NAME = oldLib and oldLib.NAME or {} flickerstreak@1: self.DYNAMIC_LOCALES = oldLib and oldLib.DYNAMIC_LOCALES or {} flickerstreak@1: self.CURRENT_LOCALE = oldLib and oldLib.CURRENT_LOCALE or {} flickerstreak@7: self.reverseToBase = oldLib and oldLib.reverseToBase or {} flickerstreak@1: flickerstreak@1: BASE_TRANSLATIONS = self.BASE_TRANSLATIONS flickerstreak@1: DEBUGGING = self.DEBUGGING flickerstreak@1: TRANSLATIONS = self.TRANSLATIONS flickerstreak@1: BASE_LOCALE = self.BASE_LOCALE flickerstreak@1: TRANSLATION_TABLES = self.TRANSLATION_TABLES flickerstreak@1: REVERSE_TRANSLATIONS = self.REVERSE_TRANSLATIONS flickerstreak@1: STRICTNESS = self.STRICTNESS flickerstreak@1: NAME = self.NAME flickerstreak@1: DYNAMIC_LOCALES = self.DYNAMIC_LOCALES flickerstreak@1: CURRENT_LOCALE = self.CURRENT_LOCALE flickerstreak@1: flickerstreak@22: strict_instance_mt = { flickerstreak@22: __index = strict__index, flickerstreak@22: __newindex = __newindex, flickerstreak@22: __tostring = __tostring flickerstreak@22: } flickerstreak@22: flickerstreak@22: nonstrict_instance_mt = { flickerstreak@22: __index = nonstrict__index, flickerstreak@22: __newindex = __newindex, flickerstreak@22: __tostring = __tostring flickerstreak@22: } flickerstreak@22: flickerstreak@22: baseTranslations_mt = { flickerstreak@22: __index = AceLocale.prototype flickerstreak@22: } flickerstreak@1: flickerstreak@1: local GetTime = GetTime flickerstreak@1: local timeUntilClear = GetTime() + 5 flickerstreak@1: scheduleClear = function() flickerstreak@1: if next(newRegistries) then flickerstreak@1: self.frame:Show() flickerstreak@1: timeUntilClear = GetTime() + 5 flickerstreak@1: end flickerstreak@1: end flickerstreak@1: flickerstreak@22: for name, instance in pairs(self.registry) do flickerstreak@22: local name = name flickerstreak@22: setmetatable(instance, nil) flickerstreak@22: instance[NAME] = name flickerstreak@22: local strict flickerstreak@22: if instance[STRICTNESS] ~= nil then flickerstreak@22: strict = instance[STRICTNESS] flickerstreak@22: elseif instance[TRANSLATIONS] ~= instance[BASE_TRANSLATIONS] then flickerstreak@22: if getmetatable(instance[TRANSLATIONS]).__index == oldLib.prototype then flickerstreak@22: strict = true flickerstreak@1: end flickerstreak@1: end flickerstreak@22: instance[STRICTNESS] = strict and true or false flickerstreak@22: refixInstance(instance) flickerstreak@1: end flickerstreak@1: flickerstreak@1: self.frame:SetScript("OnEvent", scheduleClear) flickerstreak@1: self.frame:SetScript("OnUpdate", function() -- (this, elapsed) flickerstreak@1: if timeUntilClear - GetTime() <= 0 then flickerstreak@1: self.frame:Hide() flickerstreak@1: for k in pairs(newRegistries) do flickerstreak@1: clearCache(k) flickerstreak@1: newRegistries[k] = nil flickerstreak@1: k = nil flickerstreak@1: end flickerstreak@1: end flickerstreak@1: end) flickerstreak@1: self.frame:UnregisterAllEvents() flickerstreak@1: self.frame:RegisterEvent("ADDON_LOADED") flickerstreak@1: self.frame:RegisterEvent("PLAYER_ENTERING_WORLD") flickerstreak@1: self.frame:Show() 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) flickerstreak@22: --[[ flickerstreak@22: if true then -- debug flickerstreak@22: local L = AceLocale:new(MINOR_VERSION ~= 100000 and "AceLocale_DEBUG" or "AceLocale_DEBUG3") flickerstreak@22: L:RegisterTranslations("enUS", function() return { flickerstreak@22: Monkey = true, flickerstreak@22: House = true, flickerstreak@22: } end) flickerstreak@22: flickerstreak@22: L:RegisterTranslations("deDE", function() return { flickerstreak@22: Monkey = "Affe" flickerstreak@22: } end) flickerstreak@22: flickerstreak@22: L = AceLocale:new(MINOR_VERSION ~= 100000 and "AceLocale_DEBUG" or "AceLocale_DEBUG3") flickerstreak@22: assert(L.Monkey == "Monkey") flickerstreak@22: assert(L.House == "House") flickerstreak@22: if not L.Debug then flickerstreak@22: local pants = L.Pants flickerstreak@22: assert(not pants) flickerstreak@22: end flickerstreak@22: assert(L.Debug) flickerstreak@22: assert(L.Debug == AceLocale.prototype.Debug) flickerstreak@22: flickerstreak@22: if MINOR_VERSION == 100000 then flickerstreak@22: L = AceLocale:new("AceLocale_DEBUG") flickerstreak@22: assert(L.Monkey == "Monkey") flickerstreak@22: assert(L.House == "House") flickerstreak@22: assert(L.Debug) flickerstreak@22: assert(type(L.Debug) == "function") flickerstreak@22: assert(AceLocale.prototype.Debug) flickerstreak@22: assert(type(AceLocale.prototype.Debug) == "function") flickerstreak@22: assert(L.Debug == AceLocale.prototype.Debug) flickerstreak@22: end flickerstreak@22: flickerstreak@22: local L = AceLocale:new(MINOR_VERSION ~= 100000 and "AceLocale_DEBUG2" or "AceLocale_DEBUG4") flickerstreak@22: L:RegisterTranslations("deDE", function() return { flickerstreak@22: Affe = true, flickerstreak@22: Haus = true, flickerstreak@22: } end) flickerstreak@22: flickerstreak@22: L:RegisterTranslations("enUS", function() return { flickerstreak@22: Affe = "Monkey" flickerstreak@22: } end) flickerstreak@22: flickerstreak@22: L = AceLocale:new(MINOR_VERSION ~= 100000 and "AceLocale_DEBUG2" or "AceLocale_DEBUG4") flickerstreak@22: assert(L.Affe == "Monkey") flickerstreak@22: assert(L.Haus == "Haus") flickerstreak@22: assert(L.Debug) flickerstreak@22: assert(L.Debug == AceLocale.prototype.Debug) flickerstreak@22: flickerstreak@22: if MINOR_VERSION == 100000 then flickerstreak@22: L = AceLocale:new("AceLocale_DEBUG2") flickerstreak@22: assert(L.Affe == "Monkey") flickerstreak@22: assert(L.Haus == "Haus") flickerstreak@22: assert(L.Debug) flickerstreak@22: assert(L.Debug == AceLocale.prototype.Debug) flickerstreak@22: end flickerstreak@22: flickerstreak@22: local L = AceLocale:new(MINOR_VERSION ~= 100000 and "AceLocale_DEBUG5" or "AceLocale_DEBUG6") flickerstreak@22: L:RegisterTranslations("deDE", function() return { flickerstreak@22: Affe = true, flickerstreak@22: Haus = true, flickerstreak@22: } end) flickerstreak@22: flickerstreak@22: L:RegisterTranslations("enUS", function() return { flickerstreak@22: Affe = "Monkey" flickerstreak@22: } end) flickerstreak@22: flickerstreak@22: L:SetStrictness(true) flickerstreak@22: flickerstreak@22: L = AceLocale:new(MINOR_VERSION ~= 100000 and "AceLocale_DEBUG5" or "AceLocale_DEBUG6") flickerstreak@22: assert(L.Affe == "Monkey") flickerstreak@22: assert(L.Haus == "Haus") flickerstreak@22: assert(L.Debug) flickerstreak@22: assert(L.Debug == AceLocale.prototype.Debug) flickerstreak@22: flickerstreak@22: if MINOR_VERSION == 100000 then flickerstreak@22: L = AceLocale:new("AceLocale_DEBUG5") flickerstreak@22: assert(L.Affe == "Monkey") flickerstreak@22: assert(L.Haus == "Haus") flickerstreak@22: assert(L.Debug) flickerstreak@22: assert(L.Debug == AceLocale.prototype.Debug) flickerstreak@22: end flickerstreak@22: end flickerstreak@22: ]]