Using AceLocale-3.0
AceLocale-3.0 for WAR uses the LibStub versioning library - make sure to include both LibStub.lua and AceLocale-3.0.lua in your .mod's <Files> section if embedding AceLocale-3.0 for WAR.
Registering translations
Setting up a given locale's translations is quite simple with AceLocale. To begin with, create a new Lua file for the locale - it's recommended that this include the locale name for ease of reference, but it's not a requirement.
You'll need to add this new file to your addon's .mod file, and make sure it comes before your main code files - otherwise, it might not be loaded before your code is trying to use it!
At the beginning of the new file, fetch a new locale set from AceLocale:
local T = LibStub("WAR-AceLocale-3.0"):NewLocale("MyAddon", "enUS", true)
The first argument to NewLocale is the name of your addon (must be consistent across all locales, so that AceLocale can associate them with your addon). The second is the locale identifier (currently available are enUS, deDE, frFR, koKR, ruRU, itIT, jpJP, zhCN, and zhTW). The final argument is a boolean value specifying whether the locale you're defining should be the default locale. For most addons, this will probably be true for enUS and false for every other locale.
After you've fetched the locale object, it's just a matter of setting up the table of translations:
if T then T["identifier"] = "Translation for that identifier" T["something"] = "Translation for something" end
The if block is present because AceLocale will return a nil object if you've already defined the locale you requested an object for - this prevents problems if you accidentally try to define translations for a given locale twice.
Using translations
In your main code file (Core.lua or whatever you've named it), ask AceLocale to give you the proper translation object:
local T = LibStub("WAR-AceLocale-3.0"):GetLocale("MyAddon", true)
The first argument is the same addon name that you specified in your locale file(s). The second is a boolean value specifying whether AceLocale should fail silently if locale information is found or not. true means no error message will be displayed if locale info cannot be loaded.
After you've acquired the translation object, it's just a matter of substituting it in wherever you previously would have used the pre-localization string:
function MyAddon:MyFunction()
self:Print(T["identifier"])
if userinput == T["something"] then
doSomething()
end
end
Mixed-in variables
If you want to use text elements mixed with variables for different output you can also use functions in your locale table. So the word or text element order does not matter in your script and translations will sound more natural.
-- enUS: T['Added X DKP to player Y.'] = function(X,Y) return 'Added ' .. X .. ' DKP for player ' .. Y .. '.'; end
-- deDE: T['Added X DKP to player Y.'] = function(X,Y) return X .. ' DKP f\195\188r Spieler ' .. Y .. ' hinzugef\195\188gt.'; end
-- script.lua: self:Print(T['Added X DKP to player Y.'](dkp_value, playername));
Facts
- Date created
- 30 Sep 2008
- Last updated
- 30 Sep 2008