flickerstreak@28: --[[ $Id: AceLocale-3.0.lua 60131 2008-02-03 13:03:56Z nevcairiel $ ]] flickerstreak@28: local MAJOR,MINOR = "AceLocale-3.0", 1 flickerstreak@28: flickerstreak@28: local AceLocale, oldminor = LibStub:NewLibrary(MAJOR, MINOR) flickerstreak@28: flickerstreak@28: if not AceLocale then return end -- no upgrade needed flickerstreak@28: flickerstreak@28: local gameLocale = GetLocale() flickerstreak@28: if gameLocale == "enGB" then flickerstreak@28: gameLocale = "enUS" flickerstreak@28: end flickerstreak@28: flickerstreak@28: AceLocale.apps = AceLocale.apps or {} -- array of ["AppName"]=localetableref flickerstreak@28: AceLocale.appnames = AceLocale.appnames or {} -- array of [localetableref]="AppName" flickerstreak@28: flickerstreak@28: -- This metatable is used on all tables returned from GetLocale flickerstreak@28: local readmeta = { flickerstreak@28: __index = function(self, key) -- requesting totally unknown entries: fire off a nonbreaking error and return key flickerstreak@28: geterrorhandler()(MAJOR..": "..tostring(AceLocale.appnames[self])..": Missing entry for '"..tostring(key).."'") flickerstreak@28: rawset(self, key, key) -- only need to see the warning once, really flickerstreak@28: return key flickerstreak@28: end flickerstreak@28: } flickerstreak@28: flickerstreak@28: -- Remember the locale table being registered right now (it gets set by :NewLocale()) flickerstreak@28: local registering flickerstreak@28: flickerstreak@28: -- local assert false function flickerstreak@28: local assertfalse = function() assert(false) end flickerstreak@28: flickerstreak@28: -- This metatable proxy is used when registering nondefault locales flickerstreak@28: local writeproxy = setmetatable({}, { flickerstreak@28: __newindex = function(self, key, value) flickerstreak@28: rawset(registering, key, value == true and key or value) -- assigning values: replace 'true' with key string flickerstreak@28: end, flickerstreak@28: __index = assertfalse flickerstreak@28: }) flickerstreak@28: flickerstreak@28: -- This metatable proxy is used when registering the default locale. flickerstreak@28: -- It refuses to overwrite existing values flickerstreak@28: -- Reason 1: Allows loading locales in any order flickerstreak@28: -- Reason 2: If 2 modules have the same string, but only the first one to be flickerstreak@28: -- loaded has a translation for the current locale, the translation flickerstreak@28: -- doesn't get overwritten. flickerstreak@28: -- flickerstreak@28: local writedefaultproxy = setmetatable({}, { flickerstreak@28: __newindex = function(self, key, value) flickerstreak@28: if not rawget(registering, key) then flickerstreak@28: rawset(registering, key, value == true and key or value) flickerstreak@28: end flickerstreak@28: end, flickerstreak@28: __index = assertfalse flickerstreak@28: }) flickerstreak@28: flickerstreak@28: -- AceLocale:NewLocale(application, locale, isDefault) flickerstreak@28: -- flickerstreak@28: -- application (string) - unique name of addon / module flickerstreak@28: -- locale (string) - name of locale to register, e.g. "enUS", "deDE", etc... flickerstreak@28: -- isDefault (string) - if this is the default locale being registered flickerstreak@28: -- flickerstreak@28: -- Returns a table where localizations can be filled out, or nil if the locale is not needed flickerstreak@28: function AceLocale:NewLocale(application, locale, isDefault) flickerstreak@28: flickerstreak@28: -- GAME_LOCALE allows translators to test translations of addons without having that wow client installed flickerstreak@28: -- Ammo: I still think this is a bad idea, for instance an addon that checks for some ingame string will fail, just because some other addon flickerstreak@28: -- gives the user the illusion that they can run in a different locale? Ditch this whole thing or allow a setting per 'application'. I'm of the flickerstreak@28: -- opinion to remove this. flickerstreak@28: local gameLocale = GAME_LOCALE or gameLocale flickerstreak@28: flickerstreak@28: if locale ~= gameLocale and not isDefault then flickerstreak@28: return -- nop, we don't need these translations flickerstreak@28: end flickerstreak@28: flickerstreak@28: local app = AceLocale.apps[application] flickerstreak@28: flickerstreak@28: if not app then flickerstreak@28: app = setmetatable({}, readmeta) flickerstreak@28: AceLocale.apps[application] = app flickerstreak@28: AceLocale.appnames[app] = application flickerstreak@28: end flickerstreak@28: flickerstreak@28: registering = app -- remember globally for writeproxy and writedefaultproxy flickerstreak@28: flickerstreak@28: if isDefault then flickerstreak@28: return writedefaultproxy flickerstreak@28: end flickerstreak@28: flickerstreak@28: return writeproxy flickerstreak@28: end flickerstreak@28: flickerstreak@28: -- AceLocale:GetLocale(application [, silent]) flickerstreak@28: -- flickerstreak@28: -- application (string) - unique name of addon flickerstreak@28: -- silent (boolean) - if true, the locale is optional, silently return nil if it's not found flickerstreak@28: -- flickerstreak@28: -- Returns localizations for the current locale (or default locale if translations are missing) flickerstreak@28: -- Errors if nothing is registered (spank developer, not just a missing translation) flickerstreak@28: function AceLocale:GetLocale(application, silent) flickerstreak@28: if not silent and not AceLocale.apps[application] then flickerstreak@28: error("Usage: GetLocale(application[, silent]): 'application' - No locales registered for '"..tostring(application).."'", 2) flickerstreak@28: end flickerstreak@28: return AceLocale.apps[application] flickerstreak@28: end