Gaming
 

Making an addon easy to localize

From WoWWiki

Contents

Overview

Making your AddOn easy to localize is not an easy coding task. You will have to make some sacrifices with regards to how readable your code will be.

You do get certain benefits:

  • Separation of data and code allows for easy in-game changes
  • Localization does not require (much) coding knowledge

Some tips

Here follows the tips that I have noticed that I use to keep my addons localized. Note that I do not always use all of these myself (due to the "hack-n-go" mentality I get sometimes) but that I have noticed that these tips tend to make my addons less maintenance-heavy.

Do not use item names

Q: How do you refer to items if not by their names?

A: By using their item ids.

Thanks to CastOptions.lua for the origin of this code snippet:

function MyAddOn_ExtractItemID(link)
	_, _, id = string.find(link, "Hitem:(. ):%d :%d :%d %\124");
	return id;
end

If you are wondering how to get item links, check out the Global API - Item Functions.

Use predefined, global names for skills

By using predefined, global names for skills you can change the skill on-the-fly as well as not have to care what the name actually is!

Do not use spell names

	local spellForceOfNature, spellForceOfNatureIcon
	spellForceOfNature, _, spellForceOfNatureIcon = GetSpellInfo(33831)
	local spellScareBeast = GetSpellInfo(14327)

Or if you need to use them in more than one file stick them in tables:

local _ -- Standard "do not care about this return value".
MyMod.spellNameList["Nature's Swiftness"], _, MyMod.spellIconList["Nature's Swiftness"] = GetSpellInfo(17116)

Separate localization code/variables from normal code

Usually, one file per language / locale is about right. The naming convention is often Locale-<LOCALE>.lua

Examples (German localization, using snippets from the LibKeyBound-1.0 library):

Locale-deDE.lua:

if (GetLocale() ~= "deDE") then
	return
end
local LKB = LibStub("LibKeyBound-1.0")
LKB.locale = {
	["No current bindings"] = "Keine Tasten zugewiesen";
	...
}


LibKeyBound-1.0.lua:

local L

function LibKeyBound:Initialize()
	L = self.locale
	...
end
function LibKeyBound.Binder:OnEnter()
	...
	GameTooltip:AddLine(L["No current bindings"])
	...
end
  • The above example uses the exact phrase to be localized as the key in the locale table as well.
  • This makes your code more readable.
  • "L" is a commonly used shortcut for accessing your locale table