annotate lib/AceLocale-3.0/AceLocale-3.0.lua @ 28:21bcaf8215ff

- converted to Ace3 - rearranged file layout - configGUI menus not working right now
author Flick <flickerstreak@gmail.com>
date Mon, 17 Mar 2008 18:24:53 +0000
parents
children
rev   line source
flickerstreak@28 1 --[[ $Id: AceLocale-3.0.lua 60131 2008-02-03 13:03:56Z nevcairiel $ ]]
flickerstreak@28 2 local MAJOR,MINOR = "AceLocale-3.0", 1
flickerstreak@28 3
flickerstreak@28 4 local AceLocale, oldminor = LibStub:NewLibrary(MAJOR, MINOR)
flickerstreak@28 5
flickerstreak@28 6 if not AceLocale then return end -- no upgrade needed
flickerstreak@28 7
flickerstreak@28 8 local gameLocale = GetLocale()
flickerstreak@28 9 if gameLocale == "enGB" then
flickerstreak@28 10 gameLocale = "enUS"
flickerstreak@28 11 end
flickerstreak@28 12
flickerstreak@28 13 AceLocale.apps = AceLocale.apps or {} -- array of ["AppName"]=localetableref
flickerstreak@28 14 AceLocale.appnames = AceLocale.appnames or {} -- array of [localetableref]="AppName"
flickerstreak@28 15
flickerstreak@28 16 -- This metatable is used on all tables returned from GetLocale
flickerstreak@28 17 local readmeta = {
flickerstreak@28 18 __index = function(self, key) -- requesting totally unknown entries: fire off a nonbreaking error and return key
flickerstreak@28 19 geterrorhandler()(MAJOR..": "..tostring(AceLocale.appnames[self])..": Missing entry for '"..tostring(key).."'")
flickerstreak@28 20 rawset(self, key, key) -- only need to see the warning once, really
flickerstreak@28 21 return key
flickerstreak@28 22 end
flickerstreak@28 23 }
flickerstreak@28 24
flickerstreak@28 25 -- Remember the locale table being registered right now (it gets set by :NewLocale())
flickerstreak@28 26 local registering
flickerstreak@28 27
flickerstreak@28 28 -- local assert false function
flickerstreak@28 29 local assertfalse = function() assert(false) end
flickerstreak@28 30
flickerstreak@28 31 -- This metatable proxy is used when registering nondefault locales
flickerstreak@28 32 local writeproxy = setmetatable({}, {
flickerstreak@28 33 __newindex = function(self, key, value)
flickerstreak@28 34 rawset(registering, key, value == true and key or value) -- assigning values: replace 'true' with key string
flickerstreak@28 35 end,
flickerstreak@28 36 __index = assertfalse
flickerstreak@28 37 })
flickerstreak@28 38
flickerstreak@28 39 -- This metatable proxy is used when registering the default locale.
flickerstreak@28 40 -- It refuses to overwrite existing values
flickerstreak@28 41 -- Reason 1: Allows loading locales in any order
flickerstreak@28 42 -- Reason 2: If 2 modules have the same string, but only the first one to be
flickerstreak@28 43 -- loaded has a translation for the current locale, the translation
flickerstreak@28 44 -- doesn't get overwritten.
flickerstreak@28 45 --
flickerstreak@28 46 local writedefaultproxy = setmetatable({}, {
flickerstreak@28 47 __newindex = function(self, key, value)
flickerstreak@28 48 if not rawget(registering, key) then
flickerstreak@28 49 rawset(registering, key, value == true and key or value)
flickerstreak@28 50 end
flickerstreak@28 51 end,
flickerstreak@28 52 __index = assertfalse
flickerstreak@28 53 })
flickerstreak@28 54
flickerstreak@28 55 -- AceLocale:NewLocale(application, locale, isDefault)
flickerstreak@28 56 --
flickerstreak@28 57 -- application (string) - unique name of addon / module
flickerstreak@28 58 -- locale (string) - name of locale to register, e.g. "enUS", "deDE", etc...
flickerstreak@28 59 -- isDefault (string) - if this is the default locale being registered
flickerstreak@28 60 --
flickerstreak@28 61 -- Returns a table where localizations can be filled out, or nil if the locale is not needed
flickerstreak@28 62 function AceLocale:NewLocale(application, locale, isDefault)
flickerstreak@28 63
flickerstreak@28 64 -- GAME_LOCALE allows translators to test translations of addons without having that wow client installed
flickerstreak@28 65 -- 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 66 -- 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 67 -- opinion to remove this.
flickerstreak@28 68 local gameLocale = GAME_LOCALE or gameLocale
flickerstreak@28 69
flickerstreak@28 70 if locale ~= gameLocale and not isDefault then
flickerstreak@28 71 return -- nop, we don't need these translations
flickerstreak@28 72 end
flickerstreak@28 73
flickerstreak@28 74 local app = AceLocale.apps[application]
flickerstreak@28 75
flickerstreak@28 76 if not app then
flickerstreak@28 77 app = setmetatable({}, readmeta)
flickerstreak@28 78 AceLocale.apps[application] = app
flickerstreak@28 79 AceLocale.appnames[app] = application
flickerstreak@28 80 end
flickerstreak@28 81
flickerstreak@28 82 registering = app -- remember globally for writeproxy and writedefaultproxy
flickerstreak@28 83
flickerstreak@28 84 if isDefault then
flickerstreak@28 85 return writedefaultproxy
flickerstreak@28 86 end
flickerstreak@28 87
flickerstreak@28 88 return writeproxy
flickerstreak@28 89 end
flickerstreak@28 90
flickerstreak@28 91 -- AceLocale:GetLocale(application [, silent])
flickerstreak@28 92 --
flickerstreak@28 93 -- application (string) - unique name of addon
flickerstreak@28 94 -- silent (boolean) - if true, the locale is optional, silently return nil if it's not found
flickerstreak@28 95 --
flickerstreak@28 96 -- Returns localizations for the current locale (or default locale if translations are missing)
flickerstreak@28 97 -- Errors if nothing is registered (spank developer, not just a missing translation)
flickerstreak@28 98 function AceLocale:GetLocale(application, silent)
flickerstreak@28 99 if not silent and not AceLocale.apps[application] then
flickerstreak@28 100 error("Usage: GetLocale(application[, silent]): 'application' - No locales registered for '"..tostring(application).."'", 2)
flickerstreak@28 101 end
flickerstreak@28 102 return AceLocale.apps[application]
flickerstreak@28 103 end