-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSFUtils_LoadLanguage.lua
More file actions
103 lines (85 loc) · 3.58 KB
/
SFUtils_LoadLanguage.lua
File metadata and controls
103 lines (85 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
-- LibSFUtils is already defined in prior loaded file
LibSFUtils = LibSFUtils or {}
local sfutil = LibSFUtils
-- -------------------------------------------------------
-- This enhanced version of ESO's SafeAddString() will create the
-- string if it does not already exist (new behaviour) and overwrite
-- the string if it does exist and is not an older version (original
-- behaviour).
-- in the first use case, stringId is a "string" type
-- in the second use case, stringId is a "number" type (the actual ID)
-- a third use case allows using SafeAddString to overwrite an existing
-- string definition with the "name" of the id passed in vs the numeric id
--
-- Note that based on testing of the 100026 version of ZOS's SafeAddString(),
-- it does NOT properly enforce version protection.
function sfutil.SafeAddString(stringId, stringValue, stringVersion)
--if not stringId then return end
local id = stringId
if type(stringId) == "string" then
id = _G[stringId]
if not id then
ZO_CreateStringId(stringId, stringValue)
id = _G[stringId]
SafeAddVersion(id, stringVersion)
else
SafeAddString(id, stringValue, stringVersion)
end
elseif type(stringId) == "number" then
if not GetString(stringId) then
-- It's really a bad idea to add this without first doing the ZO_CreateString
-- although ZOS allows it.
assert(false,"Tried to use LibSFUtils.SafeAddString on a numeric stringId that a string had not been created for.")
else
SafeAddString(stringId, stringValue, stringVersion)
end
else
assert(false,"Tried to use LibSFUtils.SafeAddString on a stringId that was not a string or a number(id)")
end
end
-- load strings for the client language (or default if the
-- client language is not supported)
--
function sfutil.LoadLanguage(lang_strings, defaultLang)
if lang_strings == nil or type(lang_strings) ~= "table"then
-- invalid parameter
--d("LoadLanguage: Invalid lang_strings parameter")
return
end
defaultLang = sfutil.nilDefault(defaultLang, "en")
-- get current language
local lang = GetCVar("language.2")
--check for supported languages
local chosen = lang
if lang_strings[lang] == nil then
chosen = defaultLang
end
if( lang_strings[chosen] == nil or type(lang_strings[chosen]) ~= "table" ) then
-- chosen language is not in lang_strings table
--d("LoadLanguage: Chosen language is not in lang_strings table")
assert(false,"Could not find localization tables for default ("..defaultLang..") or current ("..lang..") languages")
return
end
-- load strings for default language
local dlocalstr = lang_strings[defaultLang]
if lang_strings[defaultLang] then
for stringId, stringValue in pairs(dlocalstr) do
sfutil.SafeAddString(stringId, stringValue, 1)
end
else
-- default language is not in lang_strings table
--d("LoadLanguage: Default language ("..defaultLang..") is not in lang_strings table")
end
-- load strings for current language
if lang ~= defaultLang then
local localstr = lang_strings[lang]
if lang_strings[lang] then
for stringId, stringValue in pairs(localstr) do
sfutil.SafeAddString(stringId, stringValue, 2)
end
--else
-- current language is not in lang_strings table
--d("LoadLanguage: Current language ("..lang..") is not in lang_strings table")
end
end
end