@@ -3,11 +3,13 @@ import { aTimeout, elementUpdated, expect, fixture, html } from '@open-wc/testin
3
3
import { umbExtensionsRegistry } from '@umbraco-cms/backoffice/extension-registry' ;
4
4
import { UmbLocalizationController } from '@umbraco-cms/backoffice/localization-api' ;
5
5
import { umbLocalizationRegistry } from './registry/localization.registry.js' ;
6
+ import type { ManifestLocalization } from './extensions/localization.extension.js' ;
6
7
7
- const english = {
8
+ const english : ManifestLocalization = {
8
9
type : 'localization' ,
9
10
alias : 'test.en' ,
10
11
name : 'Test English' ,
12
+ weight : 100 ,
11
13
meta : {
12
14
culture : 'en' ,
13
15
localizations : {
@@ -27,7 +29,75 @@ const english = {
27
29
} ,
28
30
} ;
29
31
30
- const danish = {
32
+ const englishUs : ManifestLocalization = {
33
+ type : 'localization' ,
34
+ alias : 'test.en-us' ,
35
+ name : 'Test English (US)' ,
36
+ weight : 100 ,
37
+ meta : {
38
+ culture : 'en-us' ,
39
+ localizations : {
40
+ general : {
41
+ close : 'Close US' ,
42
+ overridden : 'Overridden' ,
43
+ } ,
44
+ } ,
45
+ } ,
46
+ } ;
47
+
48
+ // This is a factory function that returns the localization object.
49
+ const asyncFactory = async ( localizations : Record < string , any > , delay : number ) => {
50
+ await aTimeout ( delay ) ; // Simulate async loading
51
+ return {
52
+ // Simulate a JS module that exports a localization object.
53
+ default : localizations ,
54
+ } ;
55
+ } ;
56
+
57
+ // This is an async localization that overrides the previous one.
58
+ const englishAsyncOverride : ManifestLocalization = {
59
+ type : 'localization' ,
60
+ alias : 'test.en.async-override' ,
61
+ name : 'Test English Async Override' ,
62
+ weight : - 100 ,
63
+ meta : {
64
+ culture : 'en-us' ,
65
+ } ,
66
+ js : ( ) =>
67
+ asyncFactory (
68
+ {
69
+ general : {
70
+ close : 'Close Async' ,
71
+ overridden : 'Overridden Async' ,
72
+ } ,
73
+ } ,
74
+ 100 ,
75
+ ) ,
76
+ } ;
77
+
78
+ // This is another async localization that loads later than the previous one and overrides it because of a lower weight.
79
+ const english2AsyncOverride : ManifestLocalization = {
80
+ type : 'localization' ,
81
+ alias : 'test.en.async-override-2' ,
82
+ name : 'Test English Async Override 2' ,
83
+ weight : - 200 ,
84
+ meta : {
85
+ culture : 'en-us' ,
86
+ } ,
87
+ js : ( ) =>
88
+ asyncFactory (
89
+ {
90
+ general : {
91
+ close : 'Another Async Close' ,
92
+ } ,
93
+ } ,
94
+ 200 , // This will load after the first async override
95
+ // so it should override the close translation.
96
+ // The overridden translation should not be overridden.
97
+ ) ,
98
+ } ;
99
+
100
+ const danish : ManifestLocalization = {
31
101
type : 'localization' ,
32
102
alias : 'test.da' ,
33
103
name : 'Test Danish' ,
@@ -53,8 +123,7 @@ describe('umb-localize', () => {
53
123
} ) ;
54
124
55
125
describe ( 'localization' , ( ) => {
56
- umbExtensionsRegistry . register ( english ) ;
57
- umbExtensionsRegistry . register ( danish ) ;
126
+ umbExtensionsRegistry . registerMany ( [ english , englishUs , danish ] ) ;
58
127
59
128
beforeEach ( async ( ) => {
60
129
umbLocalizationRegistry . loadLanguage ( english . meta . culture ) ;
@@ -123,13 +192,50 @@ describe('umb-localize', () => {
123
192
it ( 'should change the value if the language is changed' , async ( ) => {
124
193
expect ( element . shadowRoot ?. innerHTML ) . to . contain ( 'Close' ) ;
125
194
195
+ // Change to Danish
126
196
umbLocalizationRegistry . loadLanguage ( danish . meta . culture ) ;
127
197
await aTimeout ( 0 ) ;
128
198
await elementUpdated ( element ) ;
129
-
130
199
expect ( element . shadowRoot ?. innerHTML ) . to . contain ( 'Luk' ) ;
131
200
} ) ;
132
201
202
+ it ( 'should fall back to the fallback language if the key is not found' , async ( ) => {
203
+ expect ( element . shadowRoot ?. innerHTML ) . to . contain ( 'Close' ) ;
204
+
205
+ // Change to US English
206
+ umbLocalizationRegistry . loadLanguage ( englishUs . meta . culture ) ;
207
+ await aTimeout ( 0 ) ;
208
+ await elementUpdated ( element ) ;
209
+ expect ( element . shadowRoot ?. innerHTML ) . to . contain ( 'Close US' ) ;
210
+
211
+ element . key = 'general_overridden' ;
212
+ await elementUpdated ( element ) ;
213
+ expect ( element . shadowRoot ?. innerHTML ) . to . contain ( 'Overridden' ) ;
214
+
215
+ element . key = 'general_logout' ;
216
+ await elementUpdated ( element ) ;
217
+ expect ( element . shadowRoot ?. innerHTML ) . to . contain ( 'Log out' ) ;
218
+ } ) ;
219
+
220
+ it ( 'should accept a lazy loaded localization' , async ( ) => {
221
+ umbExtensionsRegistry . registerMany ( [ englishAsyncOverride , english2AsyncOverride ] ) ;
222
+ umbLocalizationRegistry . loadLanguage ( englishAsyncOverride . meta . culture ) ;
223
+ await aTimeout ( 200 ) ; // Wait for the async override to load
224
+
225
+ await elementUpdated ( element ) ;
226
+ expect ( element . shadowRoot ?. innerHTML ) . to . contain (
227
+ 'Another Async Close' ,
228
+ '(async) Should have overridden the close (from first language)' ,
229
+ ) ;
230
+
231
+ element . key = 'general_overridden' ;
232
+ await elementUpdated ( element ) ;
233
+ expect ( element . shadowRoot ?. innerHTML ) . to . contain (
234
+ 'Overridden Async' ,
235
+ '(async) Should not have overridden the overridden (from first language)' ,
236
+ ) ;
237
+ } ) ;
238
+
133
239
it ( 'should use the slot if translation is not found' , async ( ) => {
134
240
element . key = 'non-existing-key' ;
135
241
await elementUpdated ( element ) ;
0 commit comments