@@ -3,11 +3,13 @@ import { aTimeout, elementUpdated, expect, fixture, html } from '@open-wc/testin
33import { umbExtensionsRegistry } from '@umbraco-cms/backoffice/extension-registry' ;
44import { UmbLocalizationController } from '@umbraco-cms/backoffice/localization-api' ;
55import { umbLocalizationRegistry } from './registry/localization.registry.js' ;
6+ import type { ManifestLocalization } from './extensions/localization.extension.js' ;
67
7- const english = {
8+ const english : ManifestLocalization = {
89 type : 'localization' ,
910 alias : 'test.en' ,
1011 name : 'Test English' ,
12+ weight : 100 ,
1113 meta : {
1214 culture : 'en' ,
1315 localizations : {
@@ -27,7 +29,75 @@ const english = {
2729 } ,
2830} ;
2931
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 = {
31101 type : 'localization' ,
32102 alias : 'test.da' ,
33103 name : 'Test Danish' ,
@@ -53,8 +123,7 @@ describe('umb-localize', () => {
53123 } ) ;
54124
55125 describe ( 'localization' , ( ) => {
56- umbExtensionsRegistry . register ( english ) ;
57- umbExtensionsRegistry . register ( danish ) ;
126+ umbExtensionsRegistry . registerMany ( [ english , englishUs , danish ] ) ;
58127
59128 beforeEach ( async ( ) => {
60129 umbLocalizationRegistry . loadLanguage ( english . meta . culture ) ;
@@ -123,13 +192,50 @@ describe('umb-localize', () => {
123192 it ( 'should change the value if the language is changed' , async ( ) => {
124193 expect ( element . shadowRoot ?. innerHTML ) . to . contain ( 'Close' ) ;
125194
195+ // Change to Danish
126196 umbLocalizationRegistry . loadLanguage ( danish . meta . culture ) ;
127197 await aTimeout ( 0 ) ;
128198 await elementUpdated ( element ) ;
129-
130199 expect ( element . shadowRoot ?. innerHTML ) . to . contain ( 'Luk' ) ;
131200 } ) ;
132201
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+
133239 it ( 'should use the slot if translation is not found' , async ( ) => {
134240 element . key = 'non-existing-key' ;
135241 await elementUpdated ( element ) ;
0 commit comments