@quenty/localizedtextutils 2.0.1 → 2.1.0-canary.241.a4e8214.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,17 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # [2.1.0-canary.241.a4e8214.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/localizedtextutils@2.0.1...@quenty/localizedtextutils@2.1.0-canary.241.a4e8214.0) (2022-01-03)
7
+
8
+
9
+ ### Features
10
+
11
+ * Add localization text utils changes, including.... ([555edc1](https://github.com/Quenty/NevermoreEngine/commit/555edc16eef46fdf42f386ee6381fec652f95bc5))
12
+
13
+
14
+
15
+
16
+
6
17
  ## [2.0.1](https://github.com/Quenty/NevermoreEngine/compare/@quenty/localizedtextutils@2.0.0...@quenty/localizedtextutils@2.0.1) (2021-12-30)
7
18
 
8
19
  **Note:** Version bump only for package @quenty/localizedtextutils
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quenty/localizedtextutils",
3
- "version": "2.0.1",
3
+ "version": "2.1.0-canary.241.a4e8214.0",
4
4
  "description": "Localized text utils which changes translationKey structures to shared locations",
5
5
  "keywords": [
6
6
  "Roblox",
@@ -27,5 +27,10 @@
27
27
  "publishConfig": {
28
28
  "access": "public"
29
29
  },
30
- "gitHead": "d146c77d0a8e452824de0ab0b4b03ba0370bcc1b"
30
+ "dependencies": {
31
+ "@quenty/attributeutils": "4.1.0-canary.241.a4e8214.0",
32
+ "@quenty/loader": "3.2.0-canary.241.a4e8214.0",
33
+ "@quenty/rx": "3.6.0-canary.241.a4e8214.0"
34
+ },
35
+ "gitHead": "a4e821471f35998d63f38a4f4a578e07b4e79035"
31
36
  }
@@ -3,6 +3,13 @@
3
3
  @class LocalizedTextUtils
4
4
  ]=]
5
5
 
6
+ local HttpService = game:GetService("HttpService")
7
+
8
+ local require = require(script.Parent.loader).load(script)
9
+
10
+ local RxAttributeUtils = require("RxAttributeUtils")
11
+ local Rx = require("Rx")
12
+
6
13
  local LocalizedTextUtils = {}
7
14
 
8
15
  --[=[
@@ -48,16 +55,17 @@ function LocalizedTextUtils.isLocalizedText(data)
48
55
  end
49
56
 
50
57
  --[=[
51
- Recursively formats the translated text
58
+ Recursively formats the translated text.
52
59
  @param translator Translator | JSONTranslator
53
60
  @param translationKey string
54
61
  @param translationArgs TranslationArgs
62
+ @param extraArgs table?
55
63
  @return string
56
64
  ]=]
57
- function LocalizedTextUtils.formatByKeyRecursive(translator, translationKey, translationArgs)
65
+ function LocalizedTextUtils.formatByKeyRecursive(translator, translationKey, translationArgs, extraArgs)
58
66
  assert(translator, "Bad translator")
59
- assert(translationKey, "Bad translationKey")
60
- assert(translationArgs, "Bad translationArgs")
67
+ assert(type(translationKey) == "string", "Bad translationKey")
68
+ assert(type(translationArgs) == "table", "Bad translationArgs")
61
69
 
62
70
  local formattedArgs = {}
63
71
  for name, value in pairs(translationArgs) do
@@ -66,7 +74,7 @@ function LocalizedTextUtils.formatByKeyRecursive(translator, translationKey, tra
66
74
 
67
75
  if value.translationArgs then
68
76
  formattedArgs[name] = LocalizedTextUtils
69
- .formatByKeyRecursive(translator, value.translationKey, value.translationArgs)
77
+ .formatByKeyRecursive(translator, value.translationKey, value.translationArgs, extraArgs)
70
78
  else
71
79
  formattedArgs[name] = translator:FormatByKey(value.translationKey)
72
80
  end
@@ -75,6 +83,12 @@ function LocalizedTextUtils.formatByKeyRecursive(translator, translationKey, tra
75
83
  end
76
84
  end
77
85
 
86
+ if extraArgs then
87
+ for key, value in pairs(extraArgs) do
88
+ formattedArgs[key] = value
89
+ end
90
+ end
91
+
78
92
  return translator:FormatByKey(translationKey, formattedArgs)
79
93
  end
80
94
 
@@ -82,18 +96,154 @@ end
82
96
  Recursively formats the translated text
83
97
  @param translator Translator | JSONTranslator
84
98
  @param localizedText LocalizedTextData
99
+ @param extraArgs table?
85
100
  @return string
86
101
  ]=]
87
- function LocalizedTextUtils.localizedTextToString(translator, localizedText)
102
+ function LocalizedTextUtils.localizedTextToString(translator, localizedText, extraArgs)
88
103
  assert(translator, "Bad translator")
89
- assert(localizedText, "No localizedText")
90
- assert(localizedText.translationKey, "No translationKey")
91
- assert(localizedText.translationArgs, "No translationArgs")
104
+ assert(LocalizedTextUtils.isLocalizedText(localizedText), "No localizedText")
92
105
 
93
106
  return LocalizedTextUtils.formatByKeyRecursive(
94
107
  translator,
95
108
  localizedText.translationKey,
96
- localizedText.translationArgs)
109
+ localizedText.translationArgs,
110
+ extraArgs)
111
+ end
112
+
113
+ --[=[
114
+ Converts from JSON
115
+ @param localizedText LocalizedTextData
116
+ @return LocalizedTextData?
117
+ ]=]
118
+ function LocalizedTextUtils.fromJSON(text)
119
+ assert(type(text) == "string", "Bad text")
120
+
121
+ local decoded
122
+ local ok = pcall(function()
123
+ decoded = HttpService:JSONDecode(text)
124
+ end)
125
+ if not ok then
126
+ return nil
127
+ end
128
+
129
+ return decoded
130
+ end
131
+
132
+ --[=[
133
+ Converts to JSON
134
+ @param localizedText LocalizedTextData
135
+ @return string?
136
+ ]=]
137
+ function LocalizedTextUtils.toJSON(localizedText)
138
+ assert(LocalizedTextUtils.isLocalizedText(localizedText), "Bad localizedText")
139
+
140
+ local localized = HttpService:JSONEncode(localizedText)
141
+ return localized
142
+ end
143
+
144
+ --[=[
145
+ Sets the translation data as an attribute on an instance.
146
+ @param obj Instance
147
+ @param attributeName string
148
+ @param translationKey string
149
+ @param translationArgs TranslationArgs
150
+ @return LocalizedTextData
151
+ ]=]
152
+ function LocalizedTextUtils.setFromAttribute(obj, attributeName, translationKey, translationArgs)
153
+ assert(typeof(obj) == "Instance", "Bad obj")
154
+ assert(type(attributeName) == "string", "Bad attributeName")
155
+
156
+ local localizedText = LocalizedTextUtils.create(translationKey, translationArgs)
157
+ obj:SetAttribute(attributeName, LocalizedTextUtils.toJSON(localizedText))
158
+ end
159
+
160
+ --[=[
161
+ Reads the data from the attribute
162
+ @param obj Instance
163
+ @param attributeName string
164
+ @return LocalizedTextData
165
+ ]=]
166
+ function LocalizedTextUtils.getFromAttribute(obj, attributeName)
167
+ assert(typeof(obj) == "Instance", "Bad obj")
168
+ assert(type(attributeName) == "string", "Bad attributeName")
169
+
170
+ local value = obj:GetAttribute(attributeName)
171
+ if type(value) == "string" then
172
+ return LocalizedTextUtils.fromJSON(value)
173
+ end
174
+
175
+ return nil
176
+ end
177
+
178
+ --[=[
179
+ Gets the translation from a given object's attribute
180
+ @param translator Translator | JSONTranslator
181
+ @param obj Instance
182
+ @param attributeName string
183
+ @param extraArgs table?
184
+ @return string?
185
+ ]=]
186
+ function LocalizedTextUtils.getTranslationFromAttribute(translator, obj, attributeName, extraArgs)
187
+ assert(translator, "Bad translator")
188
+ assert(typeof(obj) == "Instance", "Bad obj")
189
+ assert(type(attributeName) == "string", "Bad attributeName")
190
+
191
+ local data = LocalizedTextUtils.getFromAttribute(obj, attributeName)
192
+ if data then
193
+ return LocalizedTextUtils.localizedTextToString(translator, data, extraArgs)
194
+ else
195
+ return nil
196
+ end
197
+ end
198
+
199
+ --[=[
200
+ Ensures an attribute is defined if nothing is there
201
+ @param obj Instance
202
+ @param attributeName string
203
+ @param defaultTranslationKey string
204
+ @param defaultTranslationArgs table?
205
+ ]=]
206
+ function LocalizedTextUtils.initializeAttribute(obj, attributeName, defaultTranslationKey, defaultTranslationArgs)
207
+ assert(typeof(obj) == "Instance", "Bad obj")
208
+ assert(type(attributeName) == "string", "Bad attributeName")
209
+ assert(type(defaultTranslationKey) == "string", "Bad defaultTranslationKey")
210
+ assert(type(defaultTranslationArgs) == "table", "Bad defaultTranslationArgs")
211
+
212
+ if LocalizedTextUtils.getFromAttribute(obj, attributeName) then
213
+ return
214
+ end
215
+
216
+ LocalizedTextUtils.setFromAttribute(obj, attributeName, defaultTranslationKey, defaultTranslationArgs)
217
+ end
218
+
219
+ --[=[
220
+ Returns the translated string from the given object
221
+ @param translator Translator | JSONTranslator
222
+ @param obj Instance
223
+ @param attributeName string
224
+ @param extraArgs table?
225
+ @return Observable<string?>
226
+ ]=]
227
+ function LocalizedTextUtils.observeTranslation(translator, obj, attributeName, extraArgs)
228
+ assert(translator, "Bad translator")
229
+ assert(typeof(obj) == "Instance", "Bad obj")
230
+ assert(type(attributeName) == "string", "Bad attributeName")
231
+
232
+ return RxAttributeUtils.observeAttribute(obj, attributeName, nil)
233
+ :Pipe({
234
+ Rx.map(function(encodedText)
235
+ if type(encodedText) == "string" then
236
+ local localizedText = LocalizedTextUtils.fromJSON(encodedText)
237
+ if localizedText then
238
+ return LocalizedTextUtils.localizedTextToString(translator, localizedText, extraArgs)
239
+ else
240
+ return nil
241
+ end
242
+ else
243
+ return nil
244
+ end
245
+ end);
246
+ })
97
247
  end
98
248
 
99
249
  return LocalizedTextUtils
@@ -0,0 +1,7 @@
1
+ {
2
+ "name": "node_modules",
3
+ "globIgnorePaths": [ "**/.package-lock.json" ],
4
+ "tree": {
5
+ "$path": { "optional": "../node_modules" }
6
+ }
7
+ }