/
LocalizedTextUtils.lua
320 lines (275 loc) · 8.93 KB
/
LocalizedTextUtils.lua
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
--[=[
Localized text utils which changes translationKey structures to shared locations
@class LocalizedTextUtils
]=]
local HttpService = game:GetService("HttpService")
local require = require(script.Parent.loader).load(script)
local RxAttributeUtils = require("RxAttributeUtils")
local Rx = require("Rx")
local LocalizedTextUtils = {}
--[=[
Valid translation args
@type TranslationArgs { [string]: LocalizedTextData | number | string }
@within LocalizedTextUtils
]=]
--[=[
Valid localized text data
@interface LocalizedTextData
.translationKey string
.translationArgs TranslationArgs
@within LocalizedTextUtils
]=]
--[=[
Creates a new localizedtextdata
@param translationKey string
@param translationArgs TranslationArgs
@return LocalizedTextData
]=]
function LocalizedTextUtils.create(translationKey, translationArgs)
assert(type(translationKey) == "string", "Bad translationKey")
assert(type(translationArgs) == "table" or translationArgs == nil, "Bad translationArgs")
return {
translationKey = translationKey;
translationArgs = translationArgs;
}
end
--[=[
Returns whether the given argument is localized text
@param data any
@return boolean
]=]
function LocalizedTextUtils.isLocalizedText(data)
return type(data) == "table"
and type(data.translationKey) == "string"
and (type(data.translationArgs) == "table"
or data.translationArgs == nil)
end
--[=[
Recursively formats the translated text.
@param translator Translator | JSONTranslator
@param translationKey string
@param translationArgs TranslationArgs?
@param extraArgs table?
@return string
]=]
function LocalizedTextUtils.formatByKeyRecursive(translator, translationKey, translationArgs, extraArgs)
assert(translator, "Bad translator")
assert(type(translationKey) == "string", "Bad translationKey")
assert(type(translationArgs) == "table" or translationArgs == nil, "Bad translationArgs")
local formattedArgs = {}
if translationArgs then
for name, value in pairs(translationArgs) do
if type(value) == "table" then
assert(value.translationKey, "Table, but no translationKey")
if value.translationArgs then
formattedArgs[name] = LocalizedTextUtils
.formatByKeyRecursive(translator, value.translationKey, value.translationArgs, extraArgs)
else
formattedArgs[name] = translator:FormatByKey(value.translationKey)
end
else
formattedArgs[name] = value
end
end
end
if extraArgs then
for key, value in pairs(extraArgs) do
formattedArgs[key] = value
end
end
return translator:FormatByKey(translationKey, formattedArgs)
end
--[=[
Observes the recursively formatted translated text.
@param translator Translator | JSONTranslator
@param translationKey string
@param translationArgs TranslationArgs?
@param extraArgs table?
@return Observable<string>
]=]
function LocalizedTextUtils.observeFormatByKeyRecursive(translator, translationKey, translationArgs, extraArgs)
assert(translator, "Bad translator")
assert(type(translationKey) == "string", "Bad translationKey")
assert(type(translationArgs) == "table" or translationArgs == nil, "Bad translationArgs")
local observableFormattedArgs = {}
if translationArgs then
for name, value in pairs(translationArgs) do
if type(value) == "table" then
assert(value.translationKey, "Table, but no translationKey")
if value.translationArgs then
observableFormattedArgs[name] = LocalizedTextUtils
.observeFormatByKeyRecursive(translator, value.translationKey, value.translationArgs, extraArgs)
else
observableFormattedArgs[name] = translator:ObserveFormatByKey(value.translationKey)
end
else
observableFormattedArgs[name] = value
end
end
end
if extraArgs then
for key, value in pairs(extraArgs) do
observableFormattedArgs[key] = value
end
end
return translator:ObserveFormatByKey(translationKey, observableFormattedArgs)
end
--[=[
Observes the translations by string recursively
@param translator Translator | JSONTranslator
@param localizedText LocalizedTextData
@param extraArgs table?
@return Observable<string>
]=]
function LocalizedTextUtils.observeLocalizedTextToString(translator, localizedText, extraArgs)
assert(translator, "Bad translator")
assert(LocalizedTextUtils.isLocalizedText(localizedText), "No localizedText")
return LocalizedTextUtils.observeFormatByKeyRecursive(
translator,
localizedText.translationKey,
localizedText.translationArgs,
extraArgs)
end
--[=[
Recursively formats the translated text
:::tip
Use LocalizedTextUtils.observeLocalizedTextToString(translator, localizedText, extraArgs)
:::
@param translator Translator | JSONTranslator
@param localizedText LocalizedTextData
@param extraArgs table?
@return string
]=]
function LocalizedTextUtils.localizedTextToString(translator, localizedText, extraArgs)
assert(translator, "Bad translator")
assert(LocalizedTextUtils.isLocalizedText(localizedText), "No localizedText")
return LocalizedTextUtils.formatByKeyRecursive(
translator,
localizedText.translationKey,
localizedText.translationArgs,
extraArgs)
end
--[=[
Converts from JSON
@param text string
@return LocalizedTextData?
]=]
function LocalizedTextUtils.fromJSON(text)
assert(type(text) == "string", "Bad text")
local decoded
local ok = pcall(function()
decoded = HttpService:JSONDecode(text)
end)
if not ok then
return nil
end
return decoded
end
--[=[
Converts to JSON
@param localizedText LocalizedTextData
@return string?
]=]
function LocalizedTextUtils.toJSON(localizedText)
assert(LocalizedTextUtils.isLocalizedText(localizedText), "Bad localizedText")
local localized = HttpService:JSONEncode(localizedText)
return localized
end
--[=[
Sets the translation data as an attribute on an instance.
@param obj Instance
@param attributeName string
@param translationKey string
@param translationArgs TranslationArgs
@return LocalizedTextData
]=]
function LocalizedTextUtils.setFromAttribute(obj, attributeName, translationKey, translationArgs)
assert(typeof(obj) == "Instance", "Bad obj")
assert(type(attributeName) == "string", "Bad attributeName")
local localizedText = LocalizedTextUtils.create(translationKey, translationArgs)
obj:SetAttribute(attributeName, LocalizedTextUtils.toJSON(localizedText))
end
--[=[
Reads the data from the attribute
@param obj Instance
@param attributeName string
@return LocalizedTextData
]=]
function LocalizedTextUtils.getFromAttribute(obj, attributeName)
assert(typeof(obj) == "Instance", "Bad obj")
assert(type(attributeName) == "string", "Bad attributeName")
local value = obj:GetAttribute(attributeName)
if type(value) == "string" then
return LocalizedTextUtils.fromJSON(value)
end
return nil
end
--[=[
Gets the translation from a given object's attribute
@param translator Translator | JSONTranslator
@param obj Instance
@param attributeName string
@param extraArgs table?
@return string?
]=]
function LocalizedTextUtils.getTranslationFromAttribute(translator, obj, attributeName, extraArgs)
assert(translator, "Bad translator")
assert(typeof(obj) == "Instance", "Bad obj")
assert(type(attributeName) == "string", "Bad attributeName")
local data = LocalizedTextUtils.getFromAttribute(obj, attributeName)
if data then
return LocalizedTextUtils.localizedTextToString(translator, data, extraArgs)
else
return nil
end
end
--[=[
Ensures an attribute is defined if nothing is there
@param obj Instance
@param attributeName string
@param defaultTranslationKey string
@param defaultTranslationArgs table?
]=]
function LocalizedTextUtils.initializeAttribute(obj, attributeName, defaultTranslationKey, defaultTranslationArgs)
assert(typeof(obj) == "Instance", "Bad obj")
assert(type(attributeName) == "string", "Bad attributeName")
assert(type(defaultTranslationKey) == "string", "Bad defaultTranslationKey")
assert(type(defaultTranslationArgs) == "table", "Bad defaultTranslationArgs")
if LocalizedTextUtils.getFromAttribute(obj, attributeName) then
return
end
LocalizedTextUtils.setFromAttribute(obj, attributeName, defaultTranslationKey, defaultTranslationArgs)
end
--[=[
Returns the translated string from the given object
@param translator Translator | JSONTranslator
@param obj Instance
@param attributeName string
@param extraArgs table?
@return Observable<string?>
]=]
function LocalizedTextUtils.observeTranslation(translator, obj, attributeName, extraArgs)
assert(translator, "Bad translator")
assert(typeof(obj) == "Instance", "Bad obj")
assert(type(attributeName) == "string", "Bad attributeName")
return RxAttributeUtils.observeAttribute(obj, attributeName, nil)
:Pipe({
Rx.switchMap(function(encodedText)
if type(encodedText) == "string" then
local localizedText = LocalizedTextUtils.fromJSON(encodedText)
if localizedText then
return LocalizedTextUtils.observeFormatByKeyRecursive(
translator,
localizedText.translationKey,
localizedText.translationArgs,
extraArgs)
else
return Rx.of(nil)
end
else
return Rx.of(nil)
end
end);
})
end
return LocalizedTextUtils