From 0ca0bf76eda9f2a9753199a776ffde5fed69fb6e Mon Sep 17 00:00:00 2001 From: Jacob Overgaard <752371+iOvergaard@users.noreply.github.com> Date: Thu, 3 Aug 2023 13:37:56 +0200 Subject: [PATCH] update tests for translation registry --- .../registry/translation.registry.test.ts | 18 +++++++++++++----- .../registry/translation.registry.ts | 4 ++-- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/Umbraco.Web.UI.Client/src/packages/core/localization/registry/translation.registry.test.ts b/src/Umbraco.Web.UI.Client/src/packages/core/localization/registry/translation.registry.test.ts index deb6274ad4..154cfe9c32 100644 --- a/src/Umbraco.Web.UI.Client/src/packages/core/localization/registry/translation.registry.test.ts +++ b/src/Umbraco.Web.UI.Client/src/packages/core/localization/registry/translation.registry.test.ts @@ -60,7 +60,7 @@ const danishRegional: ManifestTranslations = { alias: 'test.da-DK', name: 'Test Danish (Denmark)', meta: { - culture: 'da-DK', + culture: 'da-dk', translations: { general: { close: 'Luk', @@ -72,6 +72,8 @@ const danishRegional: ManifestTranslations = { describe('UmbLocalizeController', () => { umbExtensionsRegistry.register(english); + umbExtensionsRegistry.register(danish); + umbExtensionsRegistry.register(danishRegional); let registry: UmbTranslationRegistry; @@ -85,6 +87,11 @@ describe('UmbLocalizeController', () => { registry.translations.clear(); }); + it('should set the document language and direction', async () => { + expect(document.documentElement.lang).to.equal(english.meta.culture); + expect(document.documentElement.dir).to.equal(english.meta.direction); + }); + it('should load translations for the current language', async () => { expect(registry.translations.has(english.meta.culture)).to.be.true; @@ -104,23 +111,24 @@ describe('UmbLocalizeController', () => { }); it('should load a new language', async () => { - umbExtensionsRegistry.register(danish); registry.loadLanguage(danish.meta.culture); await aTimeout(0); + // Check that the new language is loaded. expect(registry.translations.has(danish.meta.culture)).to.be.true; + + // Check that the new language has the correct translations. const current = registry.translations.get(danish.meta.culture); expect(current).to.have.property('general_close', 'Luk'); }); it('should load translations for the current language and regional', async () => { + // Load the regional language. registry.loadLanguage(danishRegional.meta.culture); - umbExtensionsRegistry.register(danish); - umbExtensionsRegistry.register(danishRegional); - await aTimeout(0); + // Check that both the regional and the base language is loaded. expect(registry.translations.has(danishRegional.meta.culture), 'expected "da-dk" to be present').to.be.true; expect(registry.translations.has(danish.meta.culture), 'expected "da" to be present').to.be.true; }); diff --git a/src/Umbraco.Web.UI.Client/src/packages/core/localization/registry/translation.registry.ts b/src/Umbraco.Web.UI.Client/src/packages/core/localization/registry/translation.registry.ts index 4a90d4d6e7..d625d693c9 100644 --- a/src/Umbraco.Web.UI.Client/src/packages/core/localization/registry/translation.registry.ts +++ b/src/Umbraco.Web.UI.Client/src/packages/core/localization/registry/translation.registry.ts @@ -55,7 +55,7 @@ export class UmbTranslationRegistry { // Notify subscribers that the inner dictionary has changed. return { - $code: userCulture, + $code: extension.meta.culture.toLowerCase(), $dir: extension.meta.direction ?? 'ltr', ...innerDictionary, } satisfies TranslationSet; @@ -66,7 +66,7 @@ export class UmbTranslationRegistry { registerTranslation(...translations); // Set the document language - document.documentElement.lang = locale.baseName; + document.documentElement.lang = locale.baseName.toLowerCase(); // Set the document direction to the direction of the primary language document.documentElement.dir = translations[0].$dir ?? 'ltr';