Hi there. I'm one of the authors for an addon called lightwell buddy.
It's recently come to my attention that the addon doesn't work well outside of of english locales (big surprise eh?)... but not in ways I expected. I use the spellid's instead of spell names, and all that basic stuff.
Instead it's failing from similar, but more subtle pitfalls. For example, the addon is designed to allow players to enter the names of people they want to deliver personal messages to. I made this function to properly format the names, remove spaces, and if extra words were present, format them like a server name.
[PHP]
function LWB:FormatName(name)
local f = ""
local c = 1
for w in string.gmatch(name, "%a+") do
if w then w = strupper(strsub(w,1,1))..strlower(strsub(w,2)) end
if c == 1 and w then
f = w
elseif c == 2 and w then
f = f.."-"..w
elseif w then
f = f.." "..w
end
c = c + 1
end
return f
end
[/PHP]
Now, apparently this function is not treating ... what would you call them... "non-standard latin characters" like normal text, and is discarding them, or rather even splitting up the text between each non-standard character it finds, so håppy would become H-ppy. I can figure a way to work around this in this particular scenario I'm sure, but my main concern is I don't know where else I might be having issues. For example, do other locals even separate the name and server of each player with a hyphen? Does capitalization or lower case even exist in other locales, and if so, will it cause an issue treating foreign text to functions like strupper()? Are there resources available for me to look into to learn about the issues, and best practices to avoid them?
In general, I'm not too concerned (yet) with making every button, message and tooltip appear in the localized language. That seems easy enough to do if I really wanted... I just want to make sure it FUNCTIONS properly in different locales - without having the benefit of being able to test is there myself.
The string library shipped with WoW does not handle Unicode though all client strings are encoded in Unicode, including character and realm names. The "%a" pattern only considers ASCII characters to be part of a word, so non-ASCII characters like à é ç å are not included. strupper will probably not work either with those characters.
Now, the character name never contains a space (by Blizzard rule) and yes other locales use "-" as a name-separator. So you could work around this ideas:
-- Helper function: remove leading and trailing whitespaces for each passed argument, discarding empty strings
local function trimwords(head, ...)
if not head then return end
head = strtrim(head)
if head == '' then
return trimwords(...)
else
return head, trimwords(...)
end
end
function LWB:FormatName(input)
-- Extract the realm name, if it exists
local name, realm = strsplit(input, '-', 2)
name = strtrim(name)
if realm then
-- Split the realm name using spaces, trim each parts, and concat with spaces again
realm = strjoin(' ', trimwords(strsplit(realm, ' ')))
-- Put the realm name back at
return name..'-'..realm
else
return name
end
end
@OP: If you're cross-posting on WowAce and WoWI, you should generally mention that, as many people read both forums. Also, if someone has already replied on one forum, it helps people on the other forum not waste their time replying with the same information.
@Phanx, thanks. Noted. Brand new to this addon stuff, so I wasn't aware of how interconnected the community of builders/helpers were :-). Wasn't sure if people were active on either, but I was happily mistaken.
@Adirelle, for some reason "local name, realm = strsplit(input, '-', 2)" in your example was returning "nil" for name. And thank you for mentioneing that strupper problem, turns out it was actually cutting out those characters if their name started with a non ascii character.
@Farmbuyer, excellent suggestion :-) I think there's even some realms with numbers in their names, so the original %a would not have been working for those realms anyway. (Area 52 comes to mind).
here's the final version I've got running. Appears to run fine for its purpose. Basically it searches for all non-space patterns first with %S, and then tests the first letter of each word to see if it can be found with the "%a" pattern. Since strlower() does not seem to cause issues with non-ascii characters, the only character I have to worry about is the first one which I try to capitalize.
[PHP]
function LWB:FormatName(name)
local f = ""
local c = 1
for w in string.gmatch(gsub(name,"-"," ",1), "%S+") do
local charTest = strmatch(strsub(w,1,1),"%a")
if w then
if charTest == nil then
w = strlower(w)
else w = strupper(strsub(w,1,1))..strlower(strsub(w,2))
end
end
if c == 1 and w then
f = w
elseif c == 2 and w then
f = f.."-"..w
elseif w then
f = f.." "..w
end
c = c + 1
end
return f
end
[/PHP]
Rollback Post to RevisionRollBack
To post a comment, please login or register a new account.
It's recently come to my attention that the addon doesn't work well outside of of english locales (big surprise eh?)... but not in ways I expected. I use the spellid's instead of spell names, and all that basic stuff.
Instead it's failing from similar, but more subtle pitfalls. For example, the addon is designed to allow players to enter the names of people they want to deliver personal messages to. I made this function to properly format the names, remove spaces, and if extra words were present, format them like a server name.
[PHP]
function LWB:FormatName(name)
local f = ""
local c = 1
for w in string.gmatch(name, "%a+") do
if w then w = strupper(strsub(w,1,1))..strlower(strsub(w,2)) end
if c == 1 and w then
f = w
elseif c == 2 and w then
f = f.."-"..w
elseif w then
f = f.." "..w
end
c = c + 1
end
return f
end
[/PHP]
Now, apparently this function is not treating ... what would you call them... "non-standard latin characters" like normal text, and is discarding them, or rather even splitting up the text between each non-standard character it finds, so håppy would become H-ppy. I can figure a way to work around this in this particular scenario I'm sure, but my main concern is I don't know where else I might be having issues. For example, do other locals even separate the name and server of each player with a hyphen? Does capitalization or lower case even exist in other locales, and if so, will it cause an issue treating foreign text to functions like strupper()? Are there resources available for me to look into to learn about the issues, and best practices to avoid them?
In general, I'm not too concerned (yet) with making every button, message and tooltip appear in the localized language. That seems easy enough to do if I really wanted... I just want to make sure it FUNCTIONS properly in different locales - without having the benefit of being able to test is there myself.
Thanks for your help.
-noob programmer.
Now, the character name never contains a space (by Blizzard rule) and yes other locales use "-" as a name-separator. So you could work around this ideas:
http://www.wowinterface.com/forums/showthread.php?t=41835
@OP: If you're cross-posting on WowAce and WoWI, you should generally mention that, as many people read both forums. Also, if someone has already replied on one forum, it helps people on the other forum not waste their time replying with the same information.
@Adirelle, for some reason "local name, realm = strsplit(input, '-', 2)" in your example was returning "nil" for name. And thank you for mentioneing that strupper problem, turns out it was actually cutting out those characters if their name started with a non ascii character.
@Farmbuyer, excellent suggestion :-) I think there's even some realms with numbers in their names, so the original %a would not have been working for those realms anyway. (Area 52 comes to mind).
here's the final version I've got running. Appears to run fine for its purpose. Basically it searches for all non-space patterns first with %S, and then tests the first letter of each word to see if it can be found with the "%a" pattern. Since strlower() does not seem to cause issues with non-ascii characters, the only character I have to worry about is the first one which I try to capitalize.
[PHP]
function LWB:FormatName(name)
local f = ""
local c = 1
for w in string.gmatch(gsub(name,"-"," ",1), "%S+") do
local charTest = strmatch(strsub(w,1,1),"%a")
if w then
if charTest == nil then
w = strlower(w)
else w = strupper(strsub(w,1,1))..strlower(strsub(w,2))
end
end
if c == 1 and w then
f = w
elseif c == 2 and w then
f = f.."-"..w
elseif w then
f = f.." "..w
end
c = c + 1
end
return f
end
[/PHP]