@quenty/clienttranslator 9.0.0-canary.367.e9fdcbc.0 → 9.0.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,7 +3,112 @@
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
- # [9.0.0-canary.367.e9fdcbc.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/clienttranslator@8.21.0...@quenty/clienttranslator@9.0.0-canary.367.e9fdcbc.0) (2023-06-05)
6
+ # [9.0.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/clienttranslator@8.32.0...@quenty/clienttranslator@9.0.0) (2023-10-11)
7
+
8
+
9
+ ### Bug Fixes
10
+
11
+ * Fix localization firing after close ([aa1ab9b](https://github.com/Quenty/NevermoreEngine/commit/aa1ab9b0a21f9eab19e159012931b0114b24cad0))
12
+ * Fix localization table replication ([c1a7b0a](https://github.com/Quenty/NevermoreEngine/commit/c1a7b0a4105af1a9e6959d0836447ffb8835c0f2))
13
+
14
+
15
+ ### Features
16
+
17
+ * JSONTranslator can exist on server and generate translation keys (improved ergonomics) ([84b84b5](https://github.com/Quenty/NevermoreEngine/commit/84b84b5587b9cfebad9b9bbda7694ba714188d9c))
18
+
19
+
20
+
21
+
22
+
23
+ # [8.32.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/clienttranslator@8.31.0...@quenty/clienttranslator@8.32.0) (2023-09-21)
24
+
25
+ **Note:** Version bump only for package @quenty/clienttranslator
26
+
27
+
28
+
29
+
30
+
31
+ # [8.31.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/clienttranslator@8.30.0...@quenty/clienttranslator@8.31.0) (2023-09-04)
32
+
33
+ **Note:** Version bump only for package @quenty/clienttranslator
34
+
35
+
36
+
37
+
38
+
39
+ # [8.30.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/clienttranslator@8.29.0...@quenty/clienttranslator@8.30.0) (2023-08-23)
40
+
41
+ **Note:** Version bump only for package @quenty/clienttranslator
42
+
43
+
44
+
45
+
46
+
47
+ # [8.29.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/clienttranslator@8.28.0...@quenty/clienttranslator@8.29.0) (2023-08-01)
48
+
49
+ **Note:** Version bump only for package @quenty/clienttranslator
50
+
51
+
52
+
53
+
54
+
55
+ # [8.28.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/clienttranslator@8.27.0...@quenty/clienttranslator@8.28.0) (2023-07-28)
56
+
57
+ **Note:** Version bump only for package @quenty/clienttranslator
58
+
59
+
60
+
61
+
62
+
63
+ # [8.27.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/clienttranslator@8.26.0...@quenty/clienttranslator@8.27.0) (2023-07-25)
64
+
65
+ **Note:** Version bump only for package @quenty/clienttranslator
66
+
67
+
68
+
69
+
70
+
71
+ # [8.26.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/clienttranslator@8.25.0...@quenty/clienttranslator@8.26.0) (2023-07-23)
72
+
73
+ **Note:** Version bump only for package @quenty/clienttranslator
74
+
75
+
76
+
77
+
78
+
79
+ # [8.25.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/clienttranslator@8.24.1...@quenty/clienttranslator@8.25.0) (2023-07-15)
80
+
81
+ **Note:** Version bump only for package @quenty/clienttranslator
82
+
83
+
84
+
85
+
86
+
87
+ ## [8.24.1](https://github.com/Quenty/NevermoreEngine/compare/@quenty/clienttranslator@8.24.0...@quenty/clienttranslator@8.24.1) (2023-07-11)
88
+
89
+ **Note:** Version bump only for package @quenty/clienttranslator
90
+
91
+
92
+
93
+
94
+
95
+ # [8.24.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/clienttranslator@8.23.0...@quenty/clienttranslator@8.24.0) (2023-07-10)
96
+
97
+ **Note:** Version bump only for package @quenty/clienttranslator
98
+
99
+
100
+
101
+
102
+
103
+ # [8.23.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/clienttranslator@8.22.0...@quenty/clienttranslator@8.23.0) (2023-06-17)
104
+
105
+ **Note:** Version bump only for package @quenty/clienttranslator
106
+
107
+
108
+
109
+
110
+
111
+ # [8.22.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/clienttranslator@8.21.0...@quenty/clienttranslator@8.22.0) (2023-06-05)
7
112
 
8
113
  **Note:** Version bump only for package @quenty/clienttranslator
9
114
 
package/README.md CHANGED
@@ -23,6 +23,12 @@ npm install @quenty/clienttranslator --save
23
23
  ## Usage
24
24
  Usage is designed to be simple.
25
25
 
26
+ ## Easy-use scenario
27
+
28
+ 1. Call Translate(data, "blah") on anything
29
+ 2. Translation is magically replicated to clients and can be saved
30
+ 3. Only one place needed to save the data
31
+
26
32
  ## Adding localization files
27
33
 
28
34
  Add files to ReplicatedStorage/i18n. Files will be in string values, and be valid JSON. This allows lookup like this:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quenty/clienttranslator",
3
- "version": "9.0.0-canary.367.e9fdcbc.0",
3
+ "version": "9.0.0",
4
4
  "description": "Gets local translator for player",
5
5
  "keywords": [
6
6
  "Roblox",
@@ -25,17 +25,18 @@
25
25
  "Quenty"
26
26
  ],
27
27
  "dependencies": {
28
- "@quenty/blend": "7.0.0-canary.367.e9fdcbc.0",
29
- "@quenty/instanceutils": "7.14.0",
30
- "@quenty/loader": "6.2.1",
31
- "@quenty/maid": "2.5.0",
32
- "@quenty/promise": "6.5.0",
33
- "@quenty/pseudolocalize": "3.2.0",
34
- "@quenty/rx": "7.11.0",
35
- "@quenty/table": "3.2.0"
28
+ "@quenty/blend": "^7.0.0",
29
+ "@quenty/instanceutils": "^8.0.0",
30
+ "@quenty/loader": "^7.0.0",
31
+ "@quenty/maid": "^2.6.0",
32
+ "@quenty/promise": "^7.0.0",
33
+ "@quenty/pseudolocalize": "^3.2.0",
34
+ "@quenty/rx": "^8.0.0",
35
+ "@quenty/string": "^3.1.0",
36
+ "@quenty/table": "^3.3.0"
36
37
  },
37
38
  "publishConfig": {
38
39
  "access": "public"
39
40
  },
40
- "gitHead": "e9fdcbc6ea1d46e068bf42a08b833099e9005259"
41
+ "gitHead": "fdeae46099587019ec5fc15317dc673aed379400"
41
42
  }
@@ -11,7 +11,8 @@ local RunService = game:GetService("RunService")
11
11
 
12
12
  local JsonToLocalizationTable = {}
13
13
 
14
- local LOCALIZATION_TABLE_NAME = "GeneratedJSONTable"
14
+ local LOCALIZATION_TABLE_NAME_CLIENT = "GeneratedJSONTable_Client"
15
+ local LOCALIZATION_TABLE_NAME_SERVER = "GeneratedJSONTable_Server"
15
16
 
16
17
  --[[
17
18
  Recursively iterates through the object to construct strings and add it to the localization table
@@ -69,11 +70,18 @@ end
69
70
  @return string -- The locale
70
71
  ]=]
71
72
  function JsonToLocalizationTable.getOrCreateLocalizationTable()
72
- local localizationTable = LocalizationService:FindFirstChild(LOCALIZATION_TABLE_NAME)
73
+ local localizationTableName
74
+ if RunService:IsServer() then
75
+ localizationTableName = LOCALIZATION_TABLE_NAME_SERVER
76
+ else
77
+ localizationTableName = LOCALIZATION_TABLE_NAME_CLIENT
78
+ end
79
+
80
+ local localizationTable = LocalizationService:FindFirstChild(localizationTableName)
73
81
 
74
82
  if not localizationTable then
75
83
  localizationTable = Instance.new("LocalizationTable")
76
- localizationTable.Name = LOCALIZATION_TABLE_NAME
84
+ localizationTable.Name = localizationTableName
77
85
 
78
86
  if RunService:IsRunning() then
79
87
  localizationTable.Parent = LocalizationService
@@ -18,15 +18,16 @@ local require = require(script.Parent.loader).load(script)
18
18
  local Players = game:GetService("Players")
19
19
  local RunService = game:GetService("RunService")
20
20
 
21
+ local Blend = require("Blend")
21
22
  local JsonToLocalizationTable = require("JsonToLocalizationTable")
22
- local PseudoLocalize = require("PseudoLocalize")
23
23
  local LocalizationServiceUtils = require("LocalizationServiceUtils")
24
- local Promise = require("Promise")
25
- local Observable = require("Observable")
26
24
  local Maid = require("Maid")
27
- local Blend = require("Blend")
25
+ local Observable = require("Observable")
26
+ local Promise = require("Promise")
27
+ local PseudoLocalize = require("PseudoLocalize")
28
28
  local Rx = require("Rx")
29
29
  local RxInstanceUtils = require("RxInstanceUtils")
30
+ local TranslationKeyUtils = require("TranslationKeyUtils")
30
31
 
31
32
  local JSONTranslator = {}
32
33
  JSONTranslator.ClassName = "JSONTranslator"
@@ -73,7 +74,7 @@ function JSONTranslator.new(translatorName, ...)
73
74
  self._englishTranslator = self._localizationTable:GetTranslator("en")
74
75
  self._fallbacks = {}
75
76
 
76
- if RunService:IsRunning() then
77
+ if RunService:IsRunning() and RunService:IsClient() then
77
78
  self._promiseTranslator = LocalizationServiceUtils.promiseTranslator(Players.LocalPlayer)
78
79
  else
79
80
  self._promiseTranslator = Promise.resolved(self._englishTranslator)
@@ -86,6 +87,10 @@ function JSONTranslator.new(translatorName, ...)
86
87
  return self
87
88
  end
88
89
 
90
+ function JSONTranslator:Init(serviceBag)
91
+ self._serviceBag = assert(serviceBag, "No serviceBag")
92
+ end
93
+
89
94
  --[=[
90
95
  Observes the current locale id for this translator.
91
96
 
@@ -126,6 +131,25 @@ function JSONTranslator:SetEntryValue(translationKey, source, context, localeId,
126
131
  end
127
132
  end
128
133
 
134
+ function JSONTranslator:ObserveTranslation(prefix, text, argData)
135
+ assert(type(prefix) == "string", "Bad text")
136
+ assert(type(text) == "string", "Bad text")
137
+
138
+ return self:ObserveFormatByKey(self:ToTranslationKey(prefix, text), argData)
139
+ end
140
+
141
+ function JSONTranslator:ToTranslationKey(prefix, text)
142
+ assert(type(prefix) == "string", "Bad text")
143
+ assert(type(text) == "string", "Bad text")
144
+
145
+ local translationKey = TranslationKeyUtils.getTranslationKey(prefix, text)
146
+ local context = ("automatic.%s"):format(translationKey)
147
+
148
+ self:SetEntryValue(translationKey, text, context, "en", text)
149
+
150
+ return translationKey
151
+ end
152
+
129
153
  --[=[
130
154
  Gets the current localeId of the translator if it's initialized, or a default if it is not.
131
155
 
@@ -136,7 +160,7 @@ function JSONTranslator:GetLocaleId()
136
160
  local translator = self._promiseTranslator:Wait()
137
161
  return translator.LocaleId
138
162
  else
139
- warn("[JSONTranslator] - Translator is not loaded yet, returning english")
163
+ warn("[JSONTranslator.GetLocaleId] - Translator is not loaded yet, returning english")
140
164
  return "en"
141
165
  end
142
166
  end
@@ -218,7 +242,7 @@ function JSONTranslator:ObserveFormatByKey(key, argData)
218
242
  return Observable.new(function(sub)
219
243
  local maid = Maid.new()
220
244
 
221
- maid:GivePromise(self._promiseTranslator:Then(function(translator)
245
+ maid:GivePromise(self._promiseTranslator):Then(function(translator)
222
246
  if argObservable then
223
247
  maid:GiveTask(Rx.combineLatest({
224
248
  localeId = RxInstanceUtils.observeProperty(translator, "LocaleId");
@@ -231,7 +255,7 @@ function JSONTranslator:ObserveFormatByKey(key, argData)
231
255
  sub:Fire(self:FormatByKey(key, nil))
232
256
  end))
233
257
  end
234
- end))
258
+ end)
235
259
 
236
260
  return maid
237
261
  end)
@@ -322,7 +346,7 @@ function JSONTranslator:_formatByKeyTestMode(key, args)
322
346
  if err then
323
347
  warn(err)
324
348
  else
325
- warn("Failed to localize '" .. key .. "'")
349
+ warn("[JSONTranslator._formatByKeyTestMode] - Failed to localize '" .. key .. "'")
326
350
  end
327
351
 
328
352
  return key
@@ -0,0 +1,18 @@
1
+ --[=[
2
+ @class TranslationKeyUtils
3
+ ]=]
4
+
5
+ local require = require(script.Parent.loader).load(script)
6
+
7
+ local String = require("String")
8
+
9
+ local TranslationKeyUtils = {}
10
+
11
+ function TranslationKeyUtils.getTranslationKey(prefix, text)
12
+ local firstWordsBeginning = string.sub(string.gsub(text, "%s", ""), 1, 20)
13
+ local firstWords = String.toLowerCamelCase(firstWordsBeginning)
14
+
15
+ return prefix .. "." .. firstWords
16
+ end
17
+
18
+ return TranslationKeyUtils