Xiiph@0: --- **AceLocale-3.0** manages localization in addons, allowing for multiple locale to be registered with fallback to the base locale for untranslated strings. Xiiph@0: -- @class file Xiiph@0: -- @name AceLocale-3.0 Xiiph@0: -- @release $Id: AceLocale-3.0.lua 895 2009-12-06 16:28:55Z nevcairiel $ Xiiph@0: local MAJOR,MINOR = "AceLocale-3.0", 2 Xiiph@0: Xiiph@0: local AceLocale, oldminor = LibStub:NewLibrary(MAJOR, MINOR) Xiiph@0: Xiiph@0: if not AceLocale then return end -- no upgrade needed Xiiph@0: Xiiph@0: -- Lua APIs Xiiph@0: local assert, tostring, error = assert, tostring, error Xiiph@0: local setmetatable, rawset, rawget = setmetatable, rawset, rawget Xiiph@0: Xiiph@0: -- Global vars/functions that we don't upvalue since they might get hooked, or upgraded Xiiph@0: -- List them here for Mikk's FindGlobals script Xiiph@0: -- GLOBALS: GAME_LOCALE, geterrorhandler Xiiph@0: Xiiph@0: local gameLocale = GetLocale() Xiiph@0: if gameLocale == "enGB" then Xiiph@0: gameLocale = "enUS" Xiiph@0: end Xiiph@0: Xiiph@0: AceLocale.apps = AceLocale.apps or {} -- array of ["AppName"]=localetableref Xiiph@0: AceLocale.appnames = AceLocale.appnames or {} -- array of [localetableref]="AppName" Xiiph@0: Xiiph@0: -- This metatable is used on all tables returned from GetLocale Xiiph@0: local readmeta = { Xiiph@0: __index = function(self, key) -- requesting totally unknown entries: fire off a nonbreaking error and return key Xiiph@0: rawset(self, key, key) -- only need to see the warning once, really Xiiph@0: geterrorhandler()(MAJOR..": "..tostring(AceLocale.appnames[self])..": Missing entry for '"..tostring(key).."'") Xiiph@0: return key Xiiph@0: end Xiiph@0: } Xiiph@0: Xiiph@0: -- This metatable is used on all tables returned from GetLocale if the silent flag is true, it does not issue a warning on unknown keys Xiiph@0: local readmetasilent = { Xiiph@0: __index = function(self, key) -- requesting totally unknown entries: return key Xiiph@0: rawset(self, key, key) -- only need to invoke this function once Xiiph@0: return key Xiiph@0: end Xiiph@0: } Xiiph@0: Xiiph@0: -- Remember the locale table being registered right now (it gets set by :NewLocale()) Xiiph@0: -- NOTE: Do never try to register 2 locale tables at once and mix their definition. Xiiph@0: local registering Xiiph@0: Xiiph@0: -- local assert false function Xiiph@0: local assertfalse = function() assert(false) end Xiiph@0: Xiiph@0: -- This metatable proxy is used when registering nondefault locales Xiiph@0: local writeproxy = setmetatable({}, { Xiiph@0: __newindex = function(self, key, value) Xiiph@0: rawset(registering, key, value == true and key or value) -- assigning values: replace 'true' with key string Xiiph@0: end, Xiiph@0: __index = assertfalse Xiiph@0: }) Xiiph@0: Xiiph@0: -- This metatable proxy is used when registering the default locale. Xiiph@0: -- It refuses to overwrite existing values Xiiph@0: -- Reason 1: Allows loading locales in any order Xiiph@0: -- Reason 2: If 2 modules have the same string, but only the first one to be Xiiph@0: -- loaded has a translation for the current locale, the translation Xiiph@0: -- doesn't get overwritten. Xiiph@0: -- Xiiph@0: local writedefaultproxy = setmetatable({}, { Xiiph@0: __newindex = function(self, key, value) Xiiph@0: if not rawget(registering, key) then Xiiph@0: rawset(registering, key, value == true and key or value) Xiiph@0: end Xiiph@0: end, Xiiph@0: __index = assertfalse Xiiph@0: }) Xiiph@0: Xiiph@0: --- Register a new locale (or extend an existing one) for the specified application. Xiiph@0: -- :NewLocale will return a table you can fill your locale into, or nil if the locale isn't needed for the players Xiiph@0: -- game locale. Xiiph@0: -- @paramsig application, locale[, isDefault[, silent]] Xiiph@0: -- @param application Unique name of addon / module Xiiph@0: -- @param locale Name of the locale to register, e.g. "enUS", "deDE", etc. Xiiph@0: -- @param isDefault If this is the default locale being registered (your addon is written in this language, generally enUS) Xiiph@0: -- @param silent If true, the locale will not issue warnings for missing keys. Can only be set on the default locale. Xiiph@0: -- @usage Xiiph@0: -- -- enUS.lua Xiiph@0: -- local L = LibStub("AceLocale-3.0"):NewLocale("TestLocale", "enUS", true) Xiiph@0: -- L["string1"] = true Xiiph@0: -- Xiiph@0: -- -- deDE.lua Xiiph@0: -- local L = LibStub("AceLocale-3.0"):NewLocale("TestLocale", "deDE") Xiiph@0: -- if not L then return end Xiiph@0: -- L["string1"] = "Zeichenkette1" Xiiph@0: -- @return Locale Table to add localizations to, or nil if the current locale is not required. Xiiph@0: function AceLocale:NewLocale(application, locale, isDefault, silent) Xiiph@0: Xiiph@0: if silent and not isDefault then Xiiph@0: error("Usage: NewLocale(application, locale[, isDefault[, silent]]): 'silent' can only be specified for the default locale", 2) Xiiph@0: end Xiiph@0: Xiiph@0: -- GAME_LOCALE allows translators to test translations of addons without having that wow client installed Xiiph@0: -- 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 Xiiph@0: -- 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 Xiiph@0: -- opinion to remove this. Xiiph@0: local gameLocale = GAME_LOCALE or gameLocale Xiiph@0: Xiiph@0: if locale ~= gameLocale and not isDefault then Xiiph@0: return -- nop, we don't need these translations Xiiph@0: end Xiiph@0: Xiiph@0: local app = AceLocale.apps[application] Xiiph@0: Xiiph@0: if not app then Xiiph@0: app = setmetatable({}, silent and readmetasilent or readmeta) Xiiph@0: AceLocale.apps[application] = app Xiiph@0: AceLocale.appnames[app] = application Xiiph@0: end Xiiph@0: Xiiph@0: registering = app -- remember globally for writeproxy and writedefaultproxy Xiiph@0: Xiiph@0: if isDefault then Xiiph@0: return writedefaultproxy Xiiph@0: end Xiiph@0: Xiiph@0: return writeproxy Xiiph@0: end Xiiph@0: Xiiph@0: --- Returns localizations for the current locale (or default locale if translations are missing). Xiiph@0: -- Errors if nothing is registered (spank developer, not just a missing translation) Xiiph@0: -- @param application Unique name of addon / module Xiiph@0: -- @param silent If true, the locale is optional, silently return nil if it's not found (defaults to false, optional) Xiiph@0: -- @return The locale table for the current language. Xiiph@0: function AceLocale:GetLocale(application, silent) Xiiph@0: if not silent and not AceLocale.apps[application] then Xiiph@0: error("Usage: GetLocale(application[, silent]): 'application' - No locales registered for '"..tostring(application).."'", 2) Xiiph@0: end Xiiph@0: return AceLocale.apps[application] Xiiph@0: end