@tolgee/core 4.9.2 → 4.9.3-rc.04b22e6.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.
Files changed (288) hide show
  1. package/dist/tolgee.cjs.js +1277 -7023
  2. package/dist/tolgee.cjs.js.map +1 -1
  3. package/dist/tolgee.cjs.min.js +1 -1
  4. package/dist/tolgee.cjs.min.js.map +1 -1
  5. package/dist/tolgee.esm.js +1407 -0
  6. package/dist/tolgee.esm.js.map +1 -0
  7. package/dist/tolgee.esm.min.mjs +1 -1
  8. package/dist/tolgee.esm.min.mjs.map +1 -1
  9. package/dist/tolgee.umd.js +1277 -7023
  10. package/dist/tolgee.umd.js.map +1 -1
  11. package/dist/tolgee.umd.min.js +1 -1
  12. package/dist/tolgee.umd.min.js.map +1 -1
  13. package/lib/Controller/Cache/Cache.d.ts +22 -0
  14. package/lib/Controller/Cache/helpers.d.ts +4 -0
  15. package/lib/Controller/Controller.d.ts +110 -0
  16. package/lib/Controller/Events/EventEmitter.d.ts +6 -0
  17. package/lib/Controller/Events/EventEmitterSelective.d.ts +7 -0
  18. package/lib/Controller/Events/Events.d.ts +14 -0
  19. package/lib/Controller/Plugins/Plugins.d.ts +36 -0
  20. package/lib/Controller/State/State.d.ts +39 -0
  21. package/lib/Controller/State/helpers.d.ts +6 -0
  22. package/lib/Controller/State/initObserverOptions.d.ts +13 -0
  23. package/lib/Controller/State/initState.d.ts +47 -0
  24. package/lib/Controller/ValueObserver.d.ts +5 -0
  25. package/lib/FormatSimple/FormatError.d.ts +7 -0
  26. package/lib/FormatSimple/FormatSimple.d.ts +3 -0
  27. package/lib/FormatSimple/formatParser.d.ts +1 -0
  28. package/lib/FormatSimple/formatter.d.ts +2 -0
  29. package/lib/Tolgee.d.ts +2 -68
  30. package/lib/TranslateParams.d.ts +2 -0
  31. package/lib/{Constants/Global.d.ts → constants.d.ts} +1 -3
  32. package/lib/helpers.d.ts +3 -0
  33. package/lib/index.d.ts +4 -8
  34. package/lib/types.d.ts +244 -84
  35. package/package.json +21 -29
  36. package/src/Controller/Cache/Cache.ts +296 -0
  37. package/src/Controller/Cache/helpers.ts +37 -0
  38. package/src/Controller/Controller.ts +310 -0
  39. package/src/Controller/Events/EventEmitter.ts +30 -0
  40. package/src/Controller/Events/EventEmitterSelective.test.ts +125 -0
  41. package/src/Controller/Events/EventEmitterSelective.ts +188 -0
  42. package/src/Controller/Events/Events.ts +66 -0
  43. package/src/Controller/Plugins/Plugins.ts +315 -0
  44. package/src/Controller/State/State.ts +175 -0
  45. package/src/Controller/State/helpers.ts +41 -0
  46. package/src/Controller/State/initObserverOptions.ts +38 -0
  47. package/src/Controller/State/initState.ts +81 -0
  48. package/src/Controller/ValueObserver.ts +26 -0
  49. package/src/FormatSimple/FormatError.ts +26 -0
  50. package/src/FormatSimple/FormatSimple.ts +13 -0
  51. package/src/FormatSimple/formatParser.ts +133 -0
  52. package/src/FormatSimple/formatter.test.ts +190 -0
  53. package/src/FormatSimple/formatter.ts +19 -0
  54. package/src/Tolgee.ts +79 -330
  55. package/src/TranslateParams.test.ts +41 -0
  56. package/src/TranslateParams.ts +51 -0
  57. package/src/__test/backend.test.ts +48 -0
  58. package/src/__test/cache.test.ts +148 -0
  59. package/src/__test/client.test.ts +48 -0
  60. package/src/__test/events.test.ts +33 -0
  61. package/src/__test/format.simple.test.ts +26 -0
  62. package/src/__test/initialization.test.ts +85 -0
  63. package/src/__test/jest-setup.ts +2 -0
  64. package/src/__test/languageDetection.test.ts +129 -0
  65. package/src/__test/languageStorage.test.ts +145 -0
  66. package/src/__test/languages.test.ts +112 -0
  67. package/src/__test/loading.test.ts +39 -0
  68. package/src/__test/namespaces.test.ts +99 -0
  69. package/src/__test/namespacesFallback.test.ts +74 -0
  70. package/src/__test/plugins.test.ts +136 -0
  71. package/src/__test/testTools.ts +7 -0
  72. package/src/{Constants/Global.ts → constants.ts} +1 -6
  73. package/src/helpers.ts +17 -0
  74. package/src/index.ts +9 -8
  75. package/src/types.ts +338 -90
  76. package/README.md +0 -45
  77. package/dist/Constants/Global.d.ts +0 -6
  78. package/dist/Constants/ModifierKey.d.ts +0 -6
  79. package/dist/Errors/ApiHttpError.d.ts +0 -5
  80. package/dist/Observer.d.ts +0 -14
  81. package/dist/Observer.test.d.ts +0 -2
  82. package/dist/Properties.d.ts +0 -17
  83. package/dist/Properties.test.d.ts +0 -1
  84. package/dist/Tolgee.d.ts +0 -68
  85. package/dist/Tolgee.test.d.ts +0 -1
  86. package/dist/TolgeeConfig.d.ts +0 -69
  87. package/dist/TolgeeConfig.test.d.ts +0 -1
  88. package/dist/__integration/FormatterIcu.test.d.ts +0 -1
  89. package/dist/__integration/FormatterMissing.d.ts +0 -1
  90. package/dist/__integration/Tolgee.test.d.ts +0 -1
  91. package/dist/__integration/TolgeeInvisible.test.d.ts +0 -1
  92. package/dist/__integration/mockTranslations.d.ts +0 -7
  93. package/dist/__integration/testConfig.d.ts +0 -9
  94. package/dist/__testFixtures/classMock.d.ts +0 -3
  95. package/dist/__testFixtures/createElement.d.ts +0 -2
  96. package/dist/__testFixtures/createTestDom.d.ts +0 -9
  97. package/dist/__testFixtures/mocked.d.ts +0 -20
  98. package/dist/__testFixtures/setupAfterEnv.d.ts +0 -8
  99. package/dist/helpers/NodeHelper.d.ts +0 -14
  100. package/dist/helpers/TextHelper.d.ts +0 -5
  101. package/dist/helpers/TextHelper.test.d.ts +0 -1
  102. package/dist/helpers/commonTypes.d.ts +0 -2
  103. package/dist/helpers/encoderPolyfill.d.ts +0 -8
  104. package/dist/helpers/secret.d.ts +0 -6
  105. package/dist/helpers/secret.test.d.ts +0 -1
  106. package/dist/helpers/sleep.d.ts +0 -1
  107. package/dist/highlighter/HighlightFunctionsInitializer.d.ts +0 -10
  108. package/dist/highlighter/HighlightFunctionsInitializer.test.d.ts +0 -1
  109. package/dist/highlighter/MouseEventHandler.d.ts +0 -29
  110. package/dist/highlighter/MouseEventHandler.test.d.ts +0 -1
  111. package/dist/highlighter/TranslationHighlighter.d.ts +0 -14
  112. package/dist/highlighter/TranslationHighlighter.test.d.ts +0 -1
  113. package/dist/index.d.ts +0 -10
  114. package/dist/internal.d.ts +0 -2
  115. package/dist/modules/IcuFormatter.d.ts +0 -2
  116. package/dist/modules/IcuFormatter.test.d.ts +0 -1
  117. package/dist/modules/index.d.ts +0 -1
  118. package/dist/services/ApiHttpService.d.ts +0 -15
  119. package/dist/services/CoreService.d.ts +0 -18
  120. package/dist/services/CoreService.test.d.ts +0 -1
  121. package/dist/services/DependencyService.d.ts +0 -39
  122. package/dist/services/DependencyService.test.d.ts +0 -1
  123. package/dist/services/ElementRegistrar.d.ts +0 -19
  124. package/dist/services/ElementRegistrar.test.d.ts +0 -1
  125. package/dist/services/EventEmitter.d.ts +0 -13
  126. package/dist/services/EventService.d.ts +0 -9
  127. package/dist/services/ModuleService.d.ts +0 -5
  128. package/dist/services/ScreenshotService.d.ts +0 -15
  129. package/dist/services/Subscription.d.ts +0 -5
  130. package/dist/services/TextService.d.ts +0 -14
  131. package/dist/services/TextService.test.d.ts +0 -1
  132. package/dist/services/TranslationService.d.ts +0 -75
  133. package/dist/services/TranslationService.test.d.ts +0 -1
  134. package/dist/services/__mocks__/CoreService.d.ts +0 -2
  135. package/dist/tolgee.esm.mjs +0 -7150
  136. package/dist/tolgee.esm.mjs.map +0 -1
  137. package/dist/toolsManager/Messages.d.ts +0 -8
  138. package/dist/toolsManager/Messages.test.d.ts +0 -1
  139. package/dist/toolsManager/PluginManager.d.ts +0 -21
  140. package/dist/toolsManager/PluginManager.test.d.ts +0 -1
  141. package/dist/types/DTOs.d.ts +0 -20
  142. package/dist/types/apiSchema.generated.d.ts +0 -6185
  143. package/dist/types.d.ts +0 -123
  144. package/dist/wrappers/AbstractWrapper.d.ts +0 -8
  145. package/dist/wrappers/NodeHandler.d.ts +0 -18
  146. package/dist/wrappers/WrappedHandler.d.ts +0 -8
  147. package/dist/wrappers/invisible/AttributeHandler.d.ts +0 -8
  148. package/dist/wrappers/invisible/Coder.d.ts +0 -7
  149. package/dist/wrappers/invisible/ContentHandler.d.ts +0 -6
  150. package/dist/wrappers/invisible/CoreHandler.d.ts +0 -10
  151. package/dist/wrappers/invisible/InvisibleWrapper.d.ts +0 -18
  152. package/dist/wrappers/invisible/ValueMemory.d.ts +0 -5
  153. package/dist/wrappers/invisible/ValueMemory.test.d.ts +0 -1
  154. package/dist/wrappers/text/AttributeHandler.d.ts +0 -8
  155. package/dist/wrappers/text/AttributeHandler.test.d.ts +0 -1
  156. package/dist/wrappers/text/Coder.d.ts +0 -15
  157. package/dist/wrappers/text/Coder.test.d.ts +0 -1
  158. package/dist/wrappers/text/ContentHandler.d.ts +0 -8
  159. package/dist/wrappers/text/ContentHandler.test.d.ts +0 -1
  160. package/dist/wrappers/text/CoreHandler.d.ts +0 -17
  161. package/dist/wrappers/text/CoreHandler.test.d.ts +0 -1
  162. package/dist/wrappers/text/TextWrapper.d.ts +0 -20
  163. package/index.js +0 -7
  164. package/lib/Constants/ModifierKey.d.ts +0 -6
  165. package/lib/Errors/ApiHttpError.d.ts +0 -5
  166. package/lib/Observer.d.ts +0 -14
  167. package/lib/Properties.d.ts +0 -17
  168. package/lib/TolgeeConfig.d.ts +0 -69
  169. package/lib/helpers/NodeHelper.d.ts +0 -14
  170. package/lib/helpers/TextHelper.d.ts +0 -5
  171. package/lib/helpers/commonTypes.d.ts +0 -2
  172. package/lib/helpers/encoderPolyfill.d.ts +0 -8
  173. package/lib/helpers/secret.d.ts +0 -6
  174. package/lib/helpers/sleep.d.ts +0 -1
  175. package/lib/highlighter/HighlightFunctionsInitializer.d.ts +0 -10
  176. package/lib/highlighter/MouseEventHandler.d.ts +0 -29
  177. package/lib/highlighter/TranslationHighlighter.d.ts +0 -14
  178. package/lib/modules/IcuFormatter.d.ts +0 -2
  179. package/lib/modules/index.d.ts +0 -1
  180. package/lib/services/ApiHttpService.d.ts +0 -15
  181. package/lib/services/CoreService.d.ts +0 -18
  182. package/lib/services/DependencyService.d.ts +0 -39
  183. package/lib/services/ElementRegistrar.d.ts +0 -19
  184. package/lib/services/EventEmitter.d.ts +0 -13
  185. package/lib/services/EventService.d.ts +0 -9
  186. package/lib/services/ModuleService.d.ts +0 -5
  187. package/lib/services/ScreenshotService.d.ts +0 -15
  188. package/lib/services/Subscription.d.ts +0 -5
  189. package/lib/services/TextService.d.ts +0 -14
  190. package/lib/services/TranslationService.d.ts +0 -75
  191. package/lib/toolsManager/Messages.d.ts +0 -8
  192. package/lib/toolsManager/PluginManager.d.ts +0 -21
  193. package/lib/types/DTOs.d.ts +0 -20
  194. package/lib/types/apiSchema.generated.d.ts +0 -6185
  195. package/lib/wrappers/AbstractWrapper.d.ts +0 -8
  196. package/lib/wrappers/NodeHandler.d.ts +0 -18
  197. package/lib/wrappers/WrappedHandler.d.ts +0 -8
  198. package/lib/wrappers/invisible/AttributeHandler.d.ts +0 -8
  199. package/lib/wrappers/invisible/Coder.d.ts +0 -7
  200. package/lib/wrappers/invisible/ContentHandler.d.ts +0 -6
  201. package/lib/wrappers/invisible/CoreHandler.d.ts +0 -10
  202. package/lib/wrappers/invisible/InvisibleWrapper.d.ts +0 -18
  203. package/lib/wrappers/invisible/ValueMemory.d.ts +0 -5
  204. package/lib/wrappers/text/AttributeHandler.d.ts +0 -8
  205. package/lib/wrappers/text/Coder.d.ts +0 -15
  206. package/lib/wrappers/text/ContentHandler.d.ts +0 -8
  207. package/lib/wrappers/text/CoreHandler.d.ts +0 -17
  208. package/lib/wrappers/text/TextWrapper.d.ts +0 -20
  209. package/src/Constants/ModifierKey.ts +0 -6
  210. package/src/Errors/ApiHttpError.ts +0 -8
  211. package/src/Observer.test.ts +0 -119
  212. package/src/Observer.ts +0 -68
  213. package/src/Properties.test.ts +0 -150
  214. package/src/Properties.ts +0 -112
  215. package/src/Tolgee.test.ts +0 -473
  216. package/src/TolgeeConfig.test.ts +0 -21
  217. package/src/TolgeeConfig.ts +0 -134
  218. package/src/__integration/FormatterIcu.test.ts +0 -80
  219. package/src/__integration/FormatterMissing.ts +0 -54
  220. package/src/__integration/Tolgee.test.ts +0 -90
  221. package/src/__integration/TolgeeInvisible.test.ts +0 -145
  222. package/src/__integration/mockTranslations.ts +0 -6
  223. package/src/__integration/testConfig.ts +0 -16
  224. package/src/__testFixtures/classMock.ts +0 -11
  225. package/src/__testFixtures/createElement.ts +0 -43
  226. package/src/__testFixtures/createTestDom.ts +0 -26
  227. package/src/__testFixtures/mocked.ts +0 -25
  228. package/src/__testFixtures/setupAfterEnv.ts +0 -34
  229. package/src/helpers/NodeHelper.ts +0 -90
  230. package/src/helpers/TextHelper.test.ts +0 -62
  231. package/src/helpers/TextHelper.ts +0 -58
  232. package/src/helpers/commonTypes.ts +0 -8
  233. package/src/helpers/encoderPolyfill.ts +0 -96
  234. package/src/helpers/secret.test.ts +0 -61
  235. package/src/helpers/secret.ts +0 -68
  236. package/src/helpers/sleep.ts +0 -2
  237. package/src/highlighter/HighlightFunctionsInitializer.test.ts +0 -40
  238. package/src/highlighter/HighlightFunctionsInitializer.ts +0 -61
  239. package/src/highlighter/MouseEventHandler.test.ts +0 -151
  240. package/src/highlighter/MouseEventHandler.ts +0 -191
  241. package/src/highlighter/TranslationHighlighter.test.ts +0 -177
  242. package/src/highlighter/TranslationHighlighter.ts +0 -113
  243. package/src/internal.ts +0 -2
  244. package/src/modules/IcuFormatter.test.ts +0 -21
  245. package/src/modules/IcuFormatter.ts +0 -39
  246. package/src/modules/index.ts +0 -1
  247. package/src/services/ApiHttpService.ts +0 -85
  248. package/src/services/CoreService.test.ts +0 -141
  249. package/src/services/CoreService.ts +0 -76
  250. package/src/services/DependencyService.test.ts +0 -51
  251. package/src/services/DependencyService.ts +0 -116
  252. package/src/services/ElementRegistrar.test.ts +0 -131
  253. package/src/services/ElementRegistrar.ts +0 -108
  254. package/src/services/EventEmitter.ts +0 -52
  255. package/src/services/EventService.ts +0 -14
  256. package/src/services/ModuleService.ts +0 -14
  257. package/src/services/ScreenshotService.ts +0 -31
  258. package/src/services/Subscription.ts +0 -7
  259. package/src/services/TextService.test.ts +0 -88
  260. package/src/services/TextService.ts +0 -82
  261. package/src/services/TranslationService.test.ts +0 -358
  262. package/src/services/TranslationService.ts +0 -417
  263. package/src/services/__mocks__/CoreService.ts +0 -17
  264. package/src/toolsManager/Messages.test.ts +0 -79
  265. package/src/toolsManager/Messages.ts +0 -60
  266. package/src/toolsManager/PluginManager.test.ts +0 -108
  267. package/src/toolsManager/PluginManager.ts +0 -129
  268. package/src/types/DTOs.ts +0 -25
  269. package/src/types/apiSchema.generated.ts +0 -6208
  270. package/src/wrappers/AbstractWrapper.ts +0 -14
  271. package/src/wrappers/NodeHandler.ts +0 -143
  272. package/src/wrappers/WrappedHandler.ts +0 -28
  273. package/src/wrappers/invisible/AttributeHandler.ts +0 -23
  274. package/src/wrappers/invisible/Coder.ts +0 -65
  275. package/src/wrappers/invisible/ContentHandler.ts +0 -15
  276. package/src/wrappers/invisible/CoreHandler.ts +0 -17
  277. package/src/wrappers/invisible/InvisibleWrapper.ts +0 -59
  278. package/src/wrappers/invisible/ValueMemory.test.ts +0 -25
  279. package/src/wrappers/invisible/ValueMemory.ts +0 -16
  280. package/src/wrappers/text/AttributeHandler.test.ts +0 -118
  281. package/src/wrappers/text/AttributeHandler.ts +0 -25
  282. package/src/wrappers/text/Coder.test.ts +0 -298
  283. package/src/wrappers/text/Coder.ts +0 -202
  284. package/src/wrappers/text/ContentHandler.test.ts +0 -185
  285. package/src/wrappers/text/ContentHandler.ts +0 -21
  286. package/src/wrappers/text/CoreHandler.test.ts +0 -106
  287. package/src/wrappers/text/CoreHandler.ts +0 -45
  288. package/src/wrappers/text/TextWrapper.ts +0 -69
@@ -1,82 +0,0 @@
1
- import { TranslationTags, TranslationParamsTags } from '../types';
2
- import { TranslationService } from './TranslationService';
3
- import { Properties } from '../Properties';
4
- import { ModuleService } from './ModuleService';
5
-
6
- export class TextService {
7
- constructor(
8
- private properties: Properties,
9
- private translationService: TranslationService,
10
- private moduleService: ModuleService
11
- ) {}
12
-
13
- async translate(
14
- key: string,
15
- params: TranslationParamsTags<any>,
16
- lang = this.properties.currentLanguage,
17
- orEmpty?: boolean,
18
- defaultValue?: string
19
- ) {
20
- const translation = await this.translationService.getTranslation(
21
- key,
22
- lang,
23
- defaultValue
24
- );
25
- return this.formatTranslation(key, translation, params, lang, orEmpty);
26
- }
27
-
28
- instant(
29
- key: string,
30
- params: TranslationParamsTags<any>,
31
- lang = this.properties.currentLanguage,
32
- orEmpty?: boolean,
33
- defaultValue?: string
34
- ) {
35
- const translation = this.translationService.getFromCacheOrFallback(
36
- key,
37
- lang,
38
- defaultValue
39
- );
40
-
41
- return this.formatTranslation(key, translation, params, lang, orEmpty);
42
- }
43
-
44
- private formatTranslation(
45
- key: string,
46
- translation: string | undefined,
47
- params: TranslationParamsTags<any> | undefined,
48
- lang: string | undefined,
49
- orEmpty: boolean | undefined
50
- ) {
51
- if (translation !== undefined) {
52
- return this.format(translation, params, lang);
53
- }
54
- if (!orEmpty) {
55
- return key;
56
- }
57
- return '';
58
- }
59
-
60
- private readonly format = (
61
- translation: string,
62
- params: TranslationParamsTags<any>,
63
- lang: string | undefined
64
- ): TranslationTags<any> => {
65
- try {
66
- // try to format the text
67
- let result: TranslationTags<any> = translation;
68
- if (this.moduleService.formatter) {
69
- result = this.moduleService.formatter.format({
70
- translation: result,
71
- params,
72
- language: lang || this.properties.currentLanguage,
73
- });
74
- }
75
- return result as TranslationTags<any>;
76
- } catch (e) {
77
- // if string cannot be formatted, throw error
78
- // eslint-disable-next-line no-console
79
- console.error(e);
80
- }
81
- };
82
- }
@@ -1,358 +0,0 @@
1
- jest.dontMock('./TranslationService');
2
- jest.dontMock('../helpers/TextHelper');
3
- jest.dontMock('../types/DTOs');
4
- jest.dontMock('../Errors/ApiHttpError');
5
- jest.dontMock('./DependencyService');
6
-
7
- import { TranslationService } from './TranslationService';
8
- import { getMockedInstance } from '@testFixtures/mocked';
9
- import { ApiHttpService } from './ApiHttpService';
10
- import { Properties } from '../Properties';
11
- import { CoreService } from './CoreService';
12
- import { TranslationData } from '../types/DTOs';
13
- import { ApiHttpError } from '../Errors/ApiHttpError';
14
- import { EventService } from './EventService';
15
- import { DependencyService } from './DependencyService';
16
-
17
- const mockedTranslations = {
18
- en: {
19
- key: 'translated',
20
- 'translation.with.dots': 'Translation with dots',
21
- structured: {
22
- subtree: {
23
- translation: 'Structured subtree translation',
24
- },
25
- },
26
- 'key with: \\\\': {
27
- t: 'Key with strange escapes',
28
- },
29
- just_en: 'Just en.',
30
- },
31
- de: {
32
- key: 'übersetzen',
33
- just_en: '',
34
- },
35
- };
36
-
37
- global.fetch = jest.fn(async (url: string) => {
38
- const isEn = url.indexOf('en.json') > -1;
39
- const isDe = url.indexOf('de.json') > -1;
40
-
41
- return {
42
- json: jest.fn(async () =>
43
- isEn ? mockedTranslations.en : isDe ? mockedTranslations.de : {}
44
- ),
45
- };
46
- }) as any;
47
-
48
- describe('TranslationService', () => {
49
- let translationService: TranslationService;
50
- const languageLoadedEmitMock = jest.fn();
51
- const translationChangedEmitMock = jest.fn();
52
-
53
- beforeEach(async () => {
54
- const dependencyService = new DependencyService();
55
- dependencyService.init({});
56
- translationService = dependencyService.translationService;
57
- (getMockedInstance(EventService) as any).LANGUAGE_LOADED = {
58
- emit: languageLoadedEmitMock,
59
- };
60
- (getMockedInstance(EventService) as any).TRANSLATION_CHANGED = {
61
- emit: translationChangedEmitMock,
62
- };
63
- getMockedInstance(ApiHttpService).fetchJson = jest.fn(
64
- async () => mockedTranslations
65
- );
66
- getMockedInstance(Properties).currentLanguage = 'en';
67
- });
68
-
69
- afterEach(async () => {
70
- jest.clearAllMocks();
71
- });
72
-
73
- describe('(translation loading and retrieval)', () => {
74
- test('will load translations in production mode', async () => {
75
- await translationService.loadTranslations('en');
76
- expect(translationService.getFromCacheOrFallback('key', 'en')).toEqual(
77
- 'translated'
78
- );
79
- });
80
-
81
- test('will load translations in development mode', async () => {
82
- getMockedInstance(Properties).mode = 'development';
83
- await translationService.loadTranslations('en');
84
- expect(translationService.getFromCacheOrFallback('key', 'en')).toEqual(
85
- 'translated'
86
- );
87
- });
88
-
89
- test('will try to load the languages only single time', async () => {
90
- await translationService.loadTranslations('en');
91
- await translationService.loadTranslations('en');
92
- expect(fetch).toBeCalledTimes(1);
93
- });
94
-
95
- test('will get proper translation', async () => {
96
- expect(await translationService.getTranslation('key', 'en')).toEqual(
97
- 'translated'
98
- );
99
- });
100
-
101
- test('will get proper translation containing .', async () => {
102
- expect(
103
- await translationService.getTranslation('translation.with.dots')
104
- ).toEqual('Translation with dots');
105
- });
106
-
107
- test('will get proper structured subtree translation', async () => {
108
- expect(
109
- await translationService.getTranslation(
110
- 'structured.subtree.translation'
111
- )
112
- ).toEqual('Structured subtree translation');
113
- });
114
-
115
- test('will get proper translation on strange key', async () => {
116
- expect(
117
- await translationService.getTranslation('key with: \\\\.t')
118
- ).toEqual('Key with strange escapes');
119
- });
120
-
121
- test('will return fallback lang translation', async () => {
122
- expect(await translationService.getTranslation('key', 'cz')).toEqual(
123
- 'translated'
124
- );
125
- });
126
-
127
- test('will get translations of key', async () => {
128
- getMockedInstance(ApiHttpService).fetchJson = jest.fn(async () => ({
129
- _embedded: {
130
- keys: [
131
- {
132
- translations: {
133
- en: { text: 'translated' },
134
- de: { text: 'übersetzen' },
135
- },
136
- },
137
- ],
138
- },
139
- }));
140
- const [translationData] = await translationService.getTranslationsOfKey(
141
- 'key',
142
- new Set<string>(['en', 'de'])
143
- );
144
- expect(translationData).toEqual({
145
- translations: {
146
- en: { text: 'translated' },
147
- de: { text: 'übersetzen' },
148
- },
149
- });
150
- });
151
-
152
- test('will reset preferred languages when language is not found', async () => {
153
- global['apierror'] = ApiHttpError;
154
- getMockedInstance(ApiHttpService).fetchJson = jest.fn(async () => {
155
- throw new ApiHttpError(
156
- { status: 404 } as Response,
157
- 'language_not_found'
158
- );
159
- });
160
-
161
- delete global.location;
162
- global.location = { reload: jest.fn() } as any;
163
- global.console.error = jest.fn();
164
- getMockedInstance(CoreService).getLanguages = jest.fn(
165
- async () => new Set(['dummyLang'])
166
- );
167
- await translationService.getTranslationsOfKey('aaa', new Set<string>());
168
- expect(getMockedInstance(Properties).preferredLanguages).toEqual(
169
- new Set(['dummyLang'])
170
- );
171
- // eslint-disable-next-line no-console
172
- expect(console.error).toBeCalledTimes(1);
173
- expect(location.reload).toBeCalledTimes(1);
174
- });
175
-
176
- test('will emit on translations load', async () => {
177
- await translationService.loadTranslations('en');
178
- await translationService.loadTranslations('de');
179
- await translationService.loadTranslations('en');
180
-
181
- expect(languageLoadedEmitMock).toBeCalledTimes(2);
182
- expect(languageLoadedEmitMock).toHaveBeenNthCalledWith(1, 'en');
183
- expect(languageLoadedEmitMock).toHaveBeenNthCalledWith(2, 'de');
184
- });
185
-
186
- test("won't throw exception when there is null in translation data", async () => {
187
- getMockedInstance(Properties).mode = 'development';
188
- getMockedInstance(ApiHttpService).fetchJson = jest.fn(async () => ({
189
- en: {
190
- hello: null,
191
- yep: 'Load was successfull.',
192
- },
193
- }));
194
- window.console.error = jest.fn();
195
- await translationService.loadTranslations('en');
196
- expect(window.console.error).toHaveBeenCalledTimes(0);
197
- expect(translationService.getFromCacheOrFallback('yep')).toEqual(
198
- 'Load was successfull.'
199
- );
200
- });
201
-
202
- describe('set translation', () => {
203
- const dummyTranslationData = {
204
- key: 'test key',
205
- translations: { en: 'translation' },
206
- } as TranslationData;
207
-
208
- beforeEach(async () => {
209
- getMockedInstance(ApiHttpService).postJson = jest.fn(async () => {
210
- return {
211
- keyId: undefined,
212
- keyName: 'test key',
213
- translations: {
214
- en: {
215
- text: 'translation',
216
- },
217
- },
218
- };
219
- });
220
- await translationService.setTranslations(dummyTranslationData);
221
- });
222
-
223
- test('will call the api http service', async () => {
224
- expect(getMockedInstance(ApiHttpService).postJson).toBeCalledTimes(1);
225
- expect(getMockedInstance(ApiHttpService).postJson).toBeCalledWith(
226
- 'v2/projects/translations',
227
- dummyTranslationData
228
- );
229
- });
230
-
231
- test('will check the scopes', async () => {
232
- expect(getMockedInstance(CoreService).checkScope).toBeCalledTimes(1);
233
- });
234
-
235
- test('emits translation changed event', async () => {
236
- expect(translationChangedEmitMock).toBeCalledTimes(1);
237
- expect(translationChangedEmitMock).toBeCalledWith(dummyTranslationData);
238
- });
239
-
240
- test('will update the data', async () => {
241
- await translationService.loadTranslations('en');
242
- await translationService.setTranslations(dummyTranslationData);
243
- expect(
244
- await translationService.getTranslation(
245
- dummyTranslationData.key,
246
- 'en'
247
- )
248
- ).toEqual(dummyTranslationData.translations.en);
249
- });
250
- });
251
-
252
- test('will update the data when the key contains .', async () => {
253
- const dummyTranslationData = {
254
- key: 'test.key',
255
- translations: { en: 'translation' },
256
- } as TranslationData;
257
- getMockedInstance(ApiHttpService).postJson = jest.fn(async () => {
258
- return {
259
- keyId: undefined,
260
- keyName: 'test.key',
261
- translations: {
262
- en: {
263
- text: 'translation',
264
- },
265
- },
266
- };
267
- });
268
- await translationService.loadTranslations('en');
269
- await translationService.setTranslations(dummyTranslationData);
270
- expect(
271
- await translationService.getTranslation(dummyTranslationData.key, 'en')
272
- ).toEqual(dummyTranslationData.translations.en);
273
- });
274
-
275
- test('will update the data when the key contains \\.', async () => {
276
- const dummyTranslationData = {
277
- key: 'test\\.key',
278
- translations: { en: 'translation' },
279
- } as TranslationData;
280
- getMockedInstance(ApiHttpService).postJson = jest.fn(async () => {
281
- return {
282
- keyId: undefined,
283
- keyName: 'test\\.key',
284
- translations: {
285
- en: {
286
- text: 'translation',
287
- },
288
- },
289
- };
290
- });
291
- await translationService.loadTranslations('en');
292
- await translationService.setTranslations(dummyTranslationData);
293
- expect(
294
- await translationService.getTranslation(dummyTranslationData.key, 'en')
295
- ).toEqual(dummyTranslationData.translations.en);
296
- });
297
-
298
- test('will call load of fallback language on missing translation', async () => {
299
- translationService.loadTranslations = jest.fn();
300
- getMockedInstance(Properties).config.fallbackLanguage = 'en';
301
- expect(await translationService.getTranslation('aaa', 'cs'));
302
- expect(translationService.loadTranslations).toBeCalledTimes(2);
303
- expect(translationService.loadTranslations).toBeCalledWith('cs');
304
- expect(translationService.loadTranslations).toBeCalledWith('en');
305
- });
306
-
307
- test('will use fallback language on missing translation', async () => {
308
- getMockedInstance(Properties).config.fallbackLanguage = 'en';
309
- expect(
310
- await translationService.getTranslation('translation.with.dots', 'de')
311
- ).toEqual('Translation with dots');
312
- });
313
-
314
- test('getTranslation will return fallback when message is empty string', async () => {
315
- getMockedInstance(Properties).config.fallbackLanguage = 'en';
316
- expect(await translationService.getTranslation('just_en', 'de')).toEqual(
317
- 'Just en.'
318
- );
319
- });
320
-
321
- test('getFromCacheOrCallback will return fallback when message is empty string', async () => {
322
- getMockedInstance(Properties).config.fallbackLanguage = 'en';
323
- (translationService as any).setLanguageData('en', mockedTranslations.en);
324
- (translationService as any).setLanguageData('de', mockedTranslations.de);
325
- expect(
326
- await translationService.getFromCacheOrFallback('just_en', 'de')
327
- ).toEqual('Just en.');
328
- });
329
-
330
- test('uses provided static data', async () => {
331
- getMockedInstance(Properties).config.staticData = {
332
- en: { test: 'Test test' },
333
- };
334
- translationService.initStatic();
335
- expect(await translationService.getTranslation('test', 'en')).toEqual(
336
- 'Test test'
337
- );
338
- });
339
-
340
- test('uses provided promise data', async () => {
341
- getMockedInstance(Properties).config.staticData = {
342
- en: () => new Promise((resolve) => resolve({ test: 'Test test' })),
343
- };
344
- expect(await translationService.getTranslation('test', 'en')).toEqual(
345
- 'Test test'
346
- );
347
- });
348
-
349
- test('uses provided data without init static (when mode is changed dynamically)', async () => {
350
- getMockedInstance(Properties).config.staticData = {
351
- en: { test: 'Test test' },
352
- };
353
- expect(await translationService.getTranslation('test', 'en')).toEqual(
354
- 'Test test'
355
- );
356
- });
357
- });
358
- });