From 29f8aaf3a82904fc1598a4f81d3803e587d639e2 Mon Sep 17 00:00:00 2001 From: Mateusz Baginski Date: Tue, 29 Oct 2024 09:23:01 +0100 Subject: [PATCH] Add support for CKBox translations in CDN injector. --- src/cdn/ckbox/createCKBoxCdnBundlePack.ts | 14 ++++++++++++- .../ckbox/createCKBoxCdnBundlePack.test.ts | 20 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/cdn/ckbox/createCKBoxCdnBundlePack.ts b/src/cdn/ckbox/createCKBoxCdnBundlePack.ts index 7530148..7dbf586 100644 --- a/src/cdn/ckbox/createCKBoxCdnBundlePack.ts +++ b/src/cdn/ckbox/createCKBoxCdnBundlePack.ts @@ -4,6 +4,7 @@ */ import { waitForWindowEntry } from '../../utils/waitForWindowEntry.js'; +import { without } from '../../utils/without.js'; import { getCKBoxInstallationInfo } from '../../installation-info/getCKBoxInstallationInfo.js'; import type { CKCdnResourcesAdvancedPack } from '../../cdn/utils/loadCKCdnResourcesPack.js'; @@ -30,13 +31,19 @@ export function createCKBoxBundlePack( { version, theme = 'lark', + translations, createCustomCdnUrl = createCKBoxCdnUrl }: CKBoxCdnBundlePackConfig ): CKCdnResourcesAdvancedPack { return { // Load the main script of the base features. scripts: [ - createCustomCdnUrl( 'ckbox', 'ckbox.js', version ) + createCustomCdnUrl( 'ckbox', 'ckbox.js', version ), + + // EN bundle is prebuilt into the main script, so we don't need to load it separately. + ...without( [ 'en' ], translations || [] ).map( translation => + createCustomCdnUrl( 'ckbox', `translations/${ translation }.js`, version ) + ) ], // Load optional theme, if provided. It's not required but recommended because it improves the look and feel. @@ -74,6 +81,11 @@ export type CKBoxCdnBundlePackConfig = { */ version: CKBoxCdnVersion; + /** + * The list of translations to load. + */ + translations?: Array; + /** * The theme of the CKBox bundle. Default is 'lark'. */ diff --git a/tests/cdn/ckbox/createCKBoxCdnBundlePack.test.ts b/tests/cdn/ckbox/createCKBoxCdnBundlePack.test.ts index effbed4..48fc087 100644 --- a/tests/cdn/ckbox/createCKBoxCdnBundlePack.test.ts +++ b/tests/cdn/ckbox/createCKBoxCdnBundlePack.test.ts @@ -33,6 +33,26 @@ describe( 'createCKBoxCdnBundlePack', () => { } ); } ); + it( 'should return translations scripts if translations are provided', async () => { + const pack = createCKBoxBundlePack( { + version: '2.5.1', + translations: [ 'es', 'de', 'en' ] // EN should be ignored, as it's prebuilt into the main script. + } ); + + expect( pack ).toMatchObject( { + scripts: [ + 'https://cdn.ckbox.io/ckbox/2.5.1/ckbox.js', + 'https://cdn.ckbox.io/ckbox/2.5.1/translations/es.js', + 'https://cdn.ckbox.io/ckbox/2.5.1/translations/de.js' + ], + stylesheets: [ + 'https://cdn.ckbox.io/ckbox/2.5.1/styles/themes/lark.css' + ], + beforeInject: expect.any( Function ), + checkPluginLoaded: expect.any( Function ) + } ); + } ); + it( 'should not throw an error if the requested version is the same as the installed one', async () => { await loadCKBox( '2.5.1' ); await expect( loadCKBox( '2.5.1' ) ).resolves.not.toThrow();