View Single Post
02-10-12, 02:12 AM   #6
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Why not? You already get a reference to the addon at the top of every file:

Code:
-- Example #1
local ADDON_NAME, addon = ...
So you simply get the L key from the addon table:

Code:
-- Example #2
local ADDON_NAME, addon = ...
local L = addon.L
Though if you didn't care about speed or efficiency, you could look it up in the addon table every time:

Code:
-- Example #3
local ADDON_NAME, addon = ...
blah:SetText(addon.L["Blah blah"])
And, if you put the addon table in the global namespace, as you did in one of your examples:

Code:
-- Example #4
local ADDON_NAME, addon = ...
_G[ADDON_NAME] = addon
Then the L table is also accessible from the global namespace, through the addon:

Code:
-- Example #5
print(_G[ADDON_NAME].L["Blah blah"])
If for some reason you really really wanted the L table to be in the global namespace separately, you could do that too, in the localization file, by adding:

Code:
-- Example #6
MyAddon_L = L
This would create a global variable "MyAddon_L" containing your L table.

Generally, however, you should avoid putting things into the global namespace separately. If you want your addon to be accessible from outside its own files (eg. by other addons) put one table into the global namespace, and add everything else to that table, as in examples 2–5. Example #6 is something you should get into the habit of not doing.
  Reply With Quote