Skip to content

Commit

Permalink
Add support for CKBox translations in CDN injector.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mati365 committed Oct 29, 2024
1 parent d53bb12 commit 29f8aaf
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/cdn/ckbox/createCKBoxCdnBundlePack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -30,13 +31,19 @@ export function createCKBoxBundlePack(
{
version,
theme = 'lark',
translations,
createCustomCdnUrl = createCKBoxCdnUrl
}: CKBoxCdnBundlePackConfig
): CKCdnResourcesAdvancedPack<Window['CKBox']> {
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.
Expand Down Expand Up @@ -74,6 +81,11 @@ export type CKBoxCdnBundlePackConfig = {
*/
version: CKBoxCdnVersion;

/**
* The list of translations to load.
*/
translations?: Array<string>;

/**
* The theme of the CKBox bundle. Default is 'lark'.
*/
Expand Down
20 changes: 20 additions & 0 deletions tests/cdn/ckbox/createCKBoxCdnBundlePack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 29f8aaf

Please sign in to comment.