annotate Details_RaidInfo-SiegeOfOrgrimmar/Libs/AceLocale-3.0/AceLocale-3.0.lua @ 0:0de01324b4f2

first commit
author Tercio
date Tue, 23 Jun 2015 15:26:28 -0300
parents
children
rev   line source
Tercio@0 1 --- **AceLocale-3.0** manages localization in addons, allowing for multiple locale to be registered with fallback to the base locale for untranslated strings.
Tercio@0 2 -- @class file
Tercio@0 3 -- @name AceLocale-3.0
Tercio@0 4 -- @release $Id: AceLocale-3.0.lua 1035 2011-07-09 03:20:13Z kaelten $
Tercio@0 5 local MAJOR,MINOR = "AceLocale-3.0", 6
Tercio@0 6
Tercio@0 7 local AceLocale, oldminor = LibStub:NewLibrary(MAJOR, MINOR)
Tercio@0 8
Tercio@0 9 if not AceLocale then return end -- no upgrade needed
Tercio@0 10
Tercio@0 11 -- Lua APIs
Tercio@0 12 local assert, tostring, error = assert, tostring, error
Tercio@0 13 local getmetatable, setmetatable, rawset, rawget = getmetatable, setmetatable, rawset, rawget
Tercio@0 14
Tercio@0 15 -- Global vars/functions that we don't upvalue since they might get hooked, or upgraded
Tercio@0 16 -- List them here for Mikk's FindGlobals script
Tercio@0 17 -- GLOBALS: GAME_LOCALE, geterrorhandler
Tercio@0 18
Tercio@0 19 local gameLocale = GetLocale()
Tercio@0 20 if gameLocale == "enGB" then
Tercio@0 21 gameLocale = "enUS"
Tercio@0 22 end
Tercio@0 23
Tercio@0 24 AceLocale.apps = AceLocale.apps or {} -- array of ["AppName"]=localetableref
Tercio@0 25 AceLocale.appnames = AceLocale.appnames or {} -- array of [localetableref]="AppName"
Tercio@0 26
Tercio@0 27 -- This metatable is used on all tables returned from GetLocale
Tercio@0 28 local readmeta = {
Tercio@0 29 __index = function(self, key) -- requesting totally unknown entries: fire off a nonbreaking error and return key
Tercio@0 30 rawset(self, key, key) -- only need to see the warning once, really
Tercio@0 31 geterrorhandler()(MAJOR..": "..tostring(AceLocale.appnames[self])..": Missing entry for '"..tostring(key).."'")
Tercio@0 32 return key
Tercio@0 33 end
Tercio@0 34 }
Tercio@0 35
Tercio@0 36 -- 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
Tercio@0 37 local readmetasilent = {
Tercio@0 38 __index = function(self, key) -- requesting totally unknown entries: return key
Tercio@0 39 rawset(self, key, key) -- only need to invoke this function once
Tercio@0 40 return key
Tercio@0 41 end
Tercio@0 42 }
Tercio@0 43
Tercio@0 44 -- Remember the locale table being registered right now (it gets set by :NewLocale())
Tercio@0 45 -- NOTE: Do never try to register 2 locale tables at once and mix their definition.
Tercio@0 46 local registering
Tercio@0 47
Tercio@0 48 -- local assert false function
Tercio@0 49 local assertfalse = function() assert(false) end
Tercio@0 50
Tercio@0 51 -- This metatable proxy is used when registering nondefault locales
Tercio@0 52 local writeproxy = setmetatable({}, {
Tercio@0 53 __newindex = function(self, key, value)
Tercio@0 54 rawset(registering, key, value == true and key or value) -- assigning values: replace 'true' with key string
Tercio@0 55 end,
Tercio@0 56 __index = assertfalse
Tercio@0 57 })
Tercio@0 58
Tercio@0 59 -- This metatable proxy is used when registering the default locale.
Tercio@0 60 -- It refuses to overwrite existing values
Tercio@0 61 -- Reason 1: Allows loading locales in any order
Tercio@0 62 -- Reason 2: If 2 modules have the same string, but only the first one to be
Tercio@0 63 -- loaded has a translation for the current locale, the translation
Tercio@0 64 -- doesn't get overwritten.
Tercio@0 65 --
Tercio@0 66 local writedefaultproxy = setmetatable({}, {
Tercio@0 67 __newindex = function(self, key, value)
Tercio@0 68 if not rawget(registering, key) then
Tercio@0 69 rawset(registering, key, value == true and key or value)
Tercio@0 70 end
Tercio@0 71 end,
Tercio@0 72 __index = assertfalse
Tercio@0 73 })
Tercio@0 74
Tercio@0 75 --- Register a new locale (or extend an existing one) for the specified application.
Tercio@0 76 -- :NewLocale will return a table you can fill your locale into, or nil if the locale isn't needed for the players
Tercio@0 77 -- game locale.
Tercio@0 78 -- @paramsig application, locale[, isDefault[, silent]]
Tercio@0 79 -- @param application Unique name of addon / module
Tercio@0 80 -- @param locale Name of the locale to register, e.g. "enUS", "deDE", etc.
Tercio@0 81 -- @param isDefault If this is the default locale being registered (your addon is written in this language, generally enUS)
Tercio@0 82 -- @param silent If true, the locale will not issue warnings for missing keys. Must be set on the first locale registered. If set to "raw", nils will be returned for unknown keys (no metatable used).
Tercio@0 83 -- @usage
Tercio@0 84 -- -- enUS.lua
Tercio@0 85 -- local L = LibStub("AceLocale-3.0"):NewLocale("TestLocale", "enUS", true)
Tercio@0 86 -- L["string1"] = true
Tercio@0 87 --
Tercio@0 88 -- -- deDE.lua
Tercio@0 89 -- local L = LibStub("AceLocale-3.0"):NewLocale("TestLocale", "deDE")
Tercio@0 90 -- if not L then return end
Tercio@0 91 -- L["string1"] = "Zeichenkette1"
Tercio@0 92 -- @return Locale Table to add localizations to, or nil if the current locale is not required.
Tercio@0 93 function AceLocale:NewLocale(application, locale, isDefault, silent)
Tercio@0 94
Tercio@0 95 -- GAME_LOCALE allows translators to test translations of addons without having that wow client installed
Tercio@0 96 local gameLocale = GAME_LOCALE or gameLocale
Tercio@0 97
Tercio@0 98 local app = AceLocale.apps[application]
Tercio@0 99
Tercio@0 100 if silent and app and getmetatable(app) ~= readmetasilent then
Tercio@0 101 geterrorhandler()("Usage: NewLocale(application, locale[, isDefault[, silent]]): 'silent' must be specified for the first locale registered")
Tercio@0 102 end
Tercio@0 103
Tercio@0 104 if not app then
Tercio@0 105 if silent=="raw" then
Tercio@0 106 app = {}
Tercio@0 107 else
Tercio@0 108 app = setmetatable({}, silent and readmetasilent or readmeta)
Tercio@0 109 end
Tercio@0 110 AceLocale.apps[application] = app
Tercio@0 111 AceLocale.appnames[app] = application
Tercio@0 112 end
Tercio@0 113
Tercio@0 114 if locale ~= gameLocale and not isDefault then
Tercio@0 115 return -- nop, we don't need these translations
Tercio@0 116 end
Tercio@0 117
Tercio@0 118 registering = app -- remember globally for writeproxy and writedefaultproxy
Tercio@0 119
Tercio@0 120 if isDefault then
Tercio@0 121 return writedefaultproxy
Tercio@0 122 end
Tercio@0 123
Tercio@0 124 return writeproxy
Tercio@0 125 end
Tercio@0 126
Tercio@0 127 --- Returns localizations for the current locale (or default locale if translations are missing).
Tercio@0 128 -- Errors if nothing is registered (spank developer, not just a missing translation)
Tercio@0 129 -- @param application Unique name of addon / module
Tercio@0 130 -- @param silent If true, the locale is optional, silently return nil if it's not found (defaults to false, optional)
Tercio@0 131 -- @return The locale table for the current language.
Tercio@0 132 function AceLocale:GetLocale(application, silent)
Tercio@0 133 if not silent and not AceLocale.apps[application] then
Tercio@0 134 error("Usage: GetLocale(application[, silent]): 'application' - No locales registered for '"..tostring(application).."'", 2)
Tercio@0 135 end
Tercio@0 136 return AceLocale.apps[application]
Tercio@0 137 end