@quenty/clienttranslator 4.4.0 → 4.5.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,26 @@
|
|
|
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
|
+
# [4.5.0-canary.241.a4e8214.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/clienttranslator@4.4.1...@quenty/clienttranslator@4.5.0-canary.241.a4e8214.0) (2022-01-03)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Features
|
|
10
|
+
|
|
11
|
+
* Add JSONTranslator:ObserveFormatByKey(key, argData) ([60c9ece](https://github.com/Quenty/NevermoreEngine/commit/60c9ecec2679f4392f1ee8c4f3008dfe9ec68ffa))
|
|
12
|
+
* Suppress localization warnings when game is not published ([f0961eb](https://github.com/Quenty/NevermoreEngine/commit/f0961ebbc2359a8e7f89576664c0fb80ca9bfdae))
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
## [4.4.1](https://github.com/Quenty/NevermoreEngine/compare/@quenty/clienttranslator@4.4.0...@quenty/clienttranslator@4.4.1) (2021-12-30)
|
|
19
|
+
|
|
20
|
+
**Note:** Version bump only for package @quenty/clienttranslator
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
6
26
|
# [4.4.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/clienttranslator@4.3.0...@quenty/clienttranslator@4.4.0) (2021-12-18)
|
|
7
27
|
|
|
8
28
|
|
package/README.md
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
## ClientTranslator
|
|
2
2
|
<div align="center">
|
|
3
|
-
<a href="http://quenty.github.io/
|
|
4
|
-
<img src="https://
|
|
3
|
+
<a href="http://quenty.github.io/NevermoreEngine/">
|
|
4
|
+
<img src="https://github.com/Quenty/NevermoreEngine/actions/workflows/docs.yml/badge.svg" alt="Documentation status" />
|
|
5
5
|
</a>
|
|
6
6
|
<a href="https://discord.gg/mhtGUS8">
|
|
7
|
-
<img src="https://img.shields.io/
|
|
7
|
+
<img src="https://img.shields.io/discord/385151591524597761?color=5865F2&label=discord&logo=discord&logoColor=white" alt="Discord" />
|
|
8
8
|
</a>
|
|
9
9
|
<a href="https://github.com/Quenty/NevermoreEngine/actions">
|
|
10
10
|
<img src="https://github.com/Quenty/NevermoreEngine/actions/workflows/build.yml/badge.svg" alt="Build and release status" />
|
|
@@ -13,6 +13,8 @@
|
|
|
13
13
|
|
|
14
14
|
Gets local translator for player
|
|
15
15
|
|
|
16
|
+
<div align="center"><a href="https://quenty.github.io/NevermoreEngine/api/JSONTranslator">View docs →</a></div>
|
|
17
|
+
|
|
16
18
|
## Installation
|
|
17
19
|
```
|
|
18
20
|
npm install @quenty/clienttranslator --save
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@quenty/clienttranslator",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.5.0-canary.241.a4e8214.0",
|
|
4
4
|
"description": "Gets local translator for player",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Roblox",
|
|
@@ -25,12 +25,15 @@
|
|
|
25
25
|
"Quenty"
|
|
26
26
|
],
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@quenty/
|
|
29
|
-
"@quenty/
|
|
30
|
-
"@quenty/
|
|
28
|
+
"@quenty/blend": "2.1.0-canary.241.a4e8214.0",
|
|
29
|
+
"@quenty/loader": "3.2.0-canary.241.a4e8214.0",
|
|
30
|
+
"@quenty/maid": "2.0.2",
|
|
31
|
+
"@quenty/promise": "3.4.0-canary.241.a4e8214.0",
|
|
32
|
+
"@quenty/pseudolocalize": "2.0.1",
|
|
33
|
+
"@quenty/rx": "3.6.0-canary.241.a4e8214.0"
|
|
31
34
|
},
|
|
32
35
|
"publishConfig": {
|
|
33
36
|
"access": "public"
|
|
34
37
|
},
|
|
35
|
-
"gitHead": "
|
|
38
|
+
"gitHead": "a4e821471f35998d63f38a4f4a578e07b4e79035"
|
|
36
39
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
--[=[
|
|
2
|
+
Utility function that loads a translator from a folder or a table.
|
|
3
|
+
@class JSONTranslator
|
|
4
|
+
]=]
|
|
4
5
|
|
|
5
6
|
local require = require(script.Parent.loader).load(script)
|
|
6
7
|
|
|
@@ -12,11 +13,36 @@ local JsonToLocalizationTable = require("JsonToLocalizationTable")
|
|
|
12
13
|
local PseudoLocalize = require("PseudoLocalize")
|
|
13
14
|
local LocalizationServiceUtils = require("LocalizationServiceUtils")
|
|
14
15
|
local Promise = require("Promise")
|
|
16
|
+
local Observable = require("Observable")
|
|
17
|
+
local Maid = require("Maid")
|
|
18
|
+
local Blend = require("Blend")
|
|
19
|
+
local Rx = require("Rx")
|
|
15
20
|
|
|
16
21
|
local JSONTranslator = {}
|
|
17
22
|
JSONTranslator.ClassName = "JSONTranslator"
|
|
18
23
|
JSONTranslator.__index = JSONTranslator
|
|
19
24
|
|
|
25
|
+
--[=[
|
|
26
|
+
Constructs a new JSONTranslator from the given args.
|
|
27
|
+
|
|
28
|
+
```lua
|
|
29
|
+
local translator = JSONTranslator.new("en", {
|
|
30
|
+
actions = {
|
|
31
|
+
respawn = "Respawn {playerName}";
|
|
32
|
+
};
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
print(translator:FormatByKey("actions.respawn"), { playerName = "Quenty"}) --> Respawn Quenty
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
```lua
|
|
39
|
+
local translator = JSONTranslator.new(script)
|
|
40
|
+
-- assume there is an `en.json` underneath the script with valid JSON.
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
@param ... any
|
|
44
|
+
@return JSONTranslator
|
|
45
|
+
]=]
|
|
20
46
|
function JSONTranslator.new(...)
|
|
21
47
|
local self = setmetatable({}, JSONTranslator)
|
|
22
48
|
|
|
@@ -39,11 +65,21 @@ function JSONTranslator.new(...)
|
|
|
39
65
|
return self
|
|
40
66
|
end
|
|
41
67
|
|
|
68
|
+
--[=[
|
|
69
|
+
Returns a promise that will resolve once the translator is loaded from the cloud.
|
|
70
|
+
@return Promise
|
|
71
|
+
]=]
|
|
42
72
|
function JSONTranslator:PromiseLoaded()
|
|
43
73
|
return self._promiseTranslator
|
|
44
74
|
end
|
|
45
75
|
|
|
46
|
-
|
|
76
|
+
--[=[
|
|
77
|
+
Makes the translator fall back to another translator if an entry cannot be found.
|
|
78
|
+
|
|
79
|
+
Mostly just used for testing.
|
|
80
|
+
|
|
81
|
+
@param translator JSONTranslator | Translator
|
|
82
|
+
]=]
|
|
47
83
|
function JSONTranslator:FallbackTo(translator)
|
|
48
84
|
assert(translator, "Bad translator")
|
|
49
85
|
assert(translator.FormatByKey, "Bad translator")
|
|
@@ -51,6 +87,12 @@ function JSONTranslator:FallbackTo(translator)
|
|
|
51
87
|
table.insert(self._fallbacks, translator)
|
|
52
88
|
end
|
|
53
89
|
|
|
90
|
+
--[=[
|
|
91
|
+
Formats the resulting entry by args.
|
|
92
|
+
@param key string
|
|
93
|
+
@param args table?
|
|
94
|
+
@return Promise<string>
|
|
95
|
+
]=]
|
|
54
96
|
function JSONTranslator:PromiseFormatByKey(key, args)
|
|
55
97
|
assert(self ~= JSONTranslator, "Construct a new version of this class to use it")
|
|
56
98
|
assert(type(key) == "string", "Key must be a string")
|
|
@@ -60,7 +102,51 @@ function JSONTranslator:PromiseFormatByKey(key, args)
|
|
|
60
102
|
end)
|
|
61
103
|
end
|
|
62
104
|
|
|
63
|
-
|
|
105
|
+
--[=[
|
|
106
|
+
Observes the translated value
|
|
107
|
+
@param key string
|
|
108
|
+
@param argData table? -- May have observables (or convertable to observables) in it.
|
|
109
|
+
@return Observable<string>
|
|
110
|
+
]=]
|
|
111
|
+
function JSONTranslator:ObserveFormatByKey(key, argData)
|
|
112
|
+
assert(self ~= JSONTranslator, "Construct a new version of this class to use it")
|
|
113
|
+
assert(type(key) == "string", "Key must be a string")
|
|
114
|
+
|
|
115
|
+
local argObservable
|
|
116
|
+
if argData then
|
|
117
|
+
local args = {}
|
|
118
|
+
for argKey, value in pairs(argData) do
|
|
119
|
+
args[argKey] = Blend.toPropertyObservable(value) or Rx.of(value)
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
argObservable = Rx.combineLatest(args)
|
|
123
|
+
else
|
|
124
|
+
argObservable = nil
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
return Observable.new(function(sub)
|
|
128
|
+
local maid = Maid.new()
|
|
129
|
+
|
|
130
|
+
maid:GivePromise(self._promiseTranslator:Then(function()
|
|
131
|
+
if argObservable then
|
|
132
|
+
maid:GiveTask(argObservable:Subscribe(function(args)
|
|
133
|
+
sub:Fire(self:FormatByKey(key, args))
|
|
134
|
+
end))
|
|
135
|
+
else
|
|
136
|
+
sub:Fire(self:FormatByKey(key, nil))
|
|
137
|
+
end
|
|
138
|
+
end))
|
|
139
|
+
|
|
140
|
+
return maid
|
|
141
|
+
end)
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
--[=[
|
|
145
|
+
Formats or errors if the cloud translations are not loaded.
|
|
146
|
+
@param key string
|
|
147
|
+
@param args table?
|
|
148
|
+
@return string
|
|
149
|
+
]=]
|
|
64
150
|
function JSONTranslator:FormatByKey(key, args)
|
|
65
151
|
assert(self ~= JSONTranslator, "Construct a new version of this class to use it")
|
|
66
152
|
assert(type(key) == "string", "Key must be a string")
|
|
@@ -140,6 +226,9 @@ function JSONTranslator:_formatByKeyTestMode(key, args)
|
|
|
140
226
|
return key
|
|
141
227
|
end
|
|
142
228
|
|
|
229
|
+
--[=[
|
|
230
|
+
Cleans up the translator and deletes the localization table if it exists.
|
|
231
|
+
]=]
|
|
143
232
|
function JSONTranslator:Destroy()
|
|
144
233
|
self._localizationTable:Destroy()
|
|
145
234
|
self._localizationTable = nil
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
--[=[
|
|
2
|
+
@class LocalizationServiceUtils
|
|
3
|
+
]=]
|
|
4
4
|
|
|
5
5
|
local require = require(script.Parent.loader).load(script)
|
|
6
6
|
|
|
@@ -8,6 +8,7 @@ local LocalizationService = game:GetService("LocalizationService")
|
|
|
8
8
|
local RunService = game:GetService("RunService")
|
|
9
9
|
|
|
10
10
|
local Promise = require("Promise")
|
|
11
|
+
local ERROR_PUBLISH_REQUIRED = "Publishing the game is required to use GetTranslatorForPlayerAsync API."
|
|
11
12
|
|
|
12
13
|
local LocalizationServiceUtils = {}
|
|
13
14
|
|
|
@@ -49,7 +50,9 @@ function LocalizationServiceUtils.promiseTranslator(player)
|
|
|
49
50
|
end)
|
|
50
51
|
|
|
51
52
|
return asyncTranslatorPromise:Catch(function(err)
|
|
52
|
-
|
|
53
|
+
if err ~= ERROR_PUBLISH_REQUIRED then
|
|
54
|
+
warn(("[LocalizationServiceUtils.promiseTranslator] - %s"):format(tostring(err)))
|
|
55
|
+
end
|
|
53
56
|
|
|
54
57
|
-- Fallback to just local stuff
|
|
55
58
|
local translator = LocalizationService:GetTranslatorForPlayer(player)
|
|
@@ -1,15 +1,20 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
--[=[
|
|
2
|
+
Utility to build a localization table from json, intended to be used with rojo. Can also handle Rojo json
|
|
3
|
+
objects turned into tables!
|
|
4
|
+
|
|
5
|
+
@class JsonToLocalizationTable
|
|
6
|
+
]=]
|
|
4
7
|
|
|
5
8
|
local HttpService = game:GetService("HttpService")
|
|
6
9
|
|
|
7
10
|
local JsonToLocalizationTable = {}
|
|
8
11
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
12
|
+
--[[
|
|
13
|
+
Recursively iterates through the object to construct strings and add it to the localization table
|
|
14
|
+
@param localeId string -- The localizationid to add
|
|
15
|
+
@param baseKey string -- the key to add
|
|
16
|
+
@param object any -- The value to iterate over
|
|
17
|
+
]]
|
|
13
18
|
local function recurseAdd(localizationTable, localeId, baseKey, object)
|
|
14
19
|
if baseKey ~= "" then
|
|
15
20
|
baseKey = baseKey .. "."
|
|
@@ -34,9 +39,11 @@ local function recurseAdd(localizationTable, localeId, baseKey, object)
|
|
|
34
39
|
end
|
|
35
40
|
end
|
|
36
41
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
--
|
|
42
|
+
--[=[
|
|
43
|
+
Extracts the locale from the name
|
|
44
|
+
@param name string -- The name to parse
|
|
45
|
+
@return string -- The locale
|
|
46
|
+
]=]
|
|
40
47
|
function JsonToLocalizationTable.localeFromName(name)
|
|
41
48
|
if name:sub(-5) == ".json" then
|
|
42
49
|
return name:sub(1, #name-5)
|
|
@@ -45,8 +52,10 @@ function JsonToLocalizationTable.localeFromName(name)
|
|
|
45
52
|
end
|
|
46
53
|
end
|
|
47
54
|
|
|
48
|
-
|
|
49
|
-
|
|
55
|
+
--[=[
|
|
56
|
+
Loads a folder into a localization table
|
|
57
|
+
@param folder Folder -- A Roblox folder with StringValues containing JSON, named with the localization in mind
|
|
58
|
+
]=]
|
|
50
59
|
function JsonToLocalizationTable.loadFolder(folder)
|
|
51
60
|
local localizationTable = Instance.new("LocalizationTable")
|
|
52
61
|
for _, item in pairs(folder:GetDescendants()) do
|
|
@@ -61,6 +70,12 @@ function JsonToLocalizationTable.loadFolder(folder)
|
|
|
61
70
|
return localizationTable
|
|
62
71
|
end
|
|
63
72
|
|
|
73
|
+
--[=[
|
|
74
|
+
Extracts the locale from the folder, or a locale and table.
|
|
75
|
+
@param first Instance | string
|
|
76
|
+
@param second table?
|
|
77
|
+
@return LocalizationTable
|
|
78
|
+
]=]
|
|
64
79
|
function JsonToLocalizationTable.toLocalizationTable(first, second)
|
|
65
80
|
if typeof(first) == "Instance" then
|
|
66
81
|
local result = JsonToLocalizationTable.loadFolder(first)
|
|
@@ -75,16 +90,24 @@ function JsonToLocalizationTable.toLocalizationTable(first, second)
|
|
|
75
90
|
end
|
|
76
91
|
end
|
|
77
92
|
|
|
93
|
+
--[=[
|
|
94
|
+
Extracts the locale from the name
|
|
95
|
+
@param localeId string -- the defaultlocaleId
|
|
96
|
+
@param dataTable table -- Data table to load from
|
|
97
|
+
@return LocalizationTable
|
|
98
|
+
]=]
|
|
78
99
|
function JsonToLocalizationTable.loadTable(localeId, dataTable)
|
|
79
100
|
local localizationTable = Instance.new("LocalizationTable")
|
|
80
101
|
recurseAdd(localizationTable, localeId, "", dataTable)
|
|
81
102
|
return localizationTable
|
|
82
103
|
end
|
|
83
104
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
105
|
+
--[=[
|
|
106
|
+
Adds json to a localization table
|
|
107
|
+
@param localizationTable LocalizationTable -- The localization table to add to
|
|
108
|
+
@param localeId string -- The localeId to use
|
|
109
|
+
@param json string -- The json to add with
|
|
110
|
+
]=]
|
|
88
111
|
function JsonToLocalizationTable.addJsonToTable(localizationTable, localeId, json)
|
|
89
112
|
local decodedTable = HttpService:JSONDecode(json)
|
|
90
113
|
recurseAdd(localizationTable, localeId, "", decodedTable)
|