Skip to content

Commit

Permalink
fix: preserve default options when adding new translations
Browse files Browse the repository at this point in the history
Refactored language modules to merge with existing default translations in
`videojs.options.language`. This prevents overriding default configurations when calling
`videojs.addLanguage`. Changes have been applied across all packages in the monorepo and the
templates for the generation of new packages.

Other changes:
- Removed spaces from the skip button translation keys.
- Expose language files as assets into the final build.
  • Loading branch information
jboix committed Jun 10, 2024
1 parent 883cbea commit 2efcfd1
Show file tree
Hide file tree
Showing 12 changed files with 284 additions and 45 deletions.
105 changes: 103 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"node-plop": "^0.32.0",
"parcel": "^2.12.0",
"plop": "^4.0.1",
"rollup-plugin-copy": "^3.5.0",
"sass": "^1.75.0",
"semantic-release": "^23.1.1",
"semantic-release-monorepo": "^8.0.2",
Expand Down
27 changes: 17 additions & 10 deletions packages/pillarbox-playlist/src/lang/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import * as de from './de.json';
import * as en from './en.json';
import * as fr from './fr.json';
import * as it from './it.json';
import * as rm from './rm.json';
import de from './de.json';
import en from './en.json';
import fr from './fr.json';
import it from './it.json';
import rm from './rm.json';
import videojs from 'video.js';

videojs.addLanguage('de', de);
videojs.addLanguage('en', en);
videojs.addLanguage('fr', fr);
videojs.addLanguage('it', it);
videojs.addLanguage('rm', rm);
function extendLanguage(code, data) {
videojs.addLanguage(code, {
...videojs.options.language[code] ?? {},
...data
});
}

extendLanguage('de', de);
extendLanguage('en', en);
extendLanguage('fr', fr);
extendLanguage('it', it);
extendLanguage('rm', rm);
31 changes: 31 additions & 0 deletions packages/pillarbox-playlist/test/language.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import '../src/lang';
import pillarbox from '@srgssr/pillarbox-web';

window.HTMLMediaElement.prototype.load = () => {};

describe('Language', () => {
let player;

beforeEach(() => {
document.body.innerHTML = '<video id="test-video" class="video-js"></video>';

const videoElement = document.querySelector('#test-video');

player = pillarbox(videoElement);
});

afterEach(() => {
player.dispose();
vi.resetAllMocks();
});

it('should use the correct French translations', () => {
// When
player.language('fr');

// Then
expect(player.localize('Play')).toBe('Lecture');
expect(player.localize('Shuffle')).toBe('Mélanger');
});
});
15 changes: 11 additions & 4 deletions packages/pillarbox-playlist/vite.config.ui.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { defineConfig } from 'vite';
import babel from '@rollup/plugin-babel';
import copy from 'rollup-plugin-copy';

/**
* Vite's configuration for the lib build.
Expand Down Expand Up @@ -30,10 +31,16 @@ export default defineConfig({
entryFileNames: 'pillarbox-playlist-ui.cjs'
}
],
plugins: [babel({
babelHelpers: 'bundled',
exclude: 'node_modules/**'
})]
plugins: [
babel({
babelHelpers: 'bundled',
exclude: 'node_modules/**'
}),
copy({
targets: [{ src: 'src/lang/*.json', dest: 'dist/lang' }],
hook: 'writeBundle'
})
]
}
}
});
27 changes: 17 additions & 10 deletions packages/skip-button/src/lang/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import * as de from './de.json';
import * as en from './en.json';
import * as fr from './fr.json';
import * as it from './it.json';
import * as rm from './rm.json';
import de from './de.json';
import en from './en.json';
import fr from './fr.json';
import it from './it.json';
import rm from './rm.json';
import pillarbox from '@srgssr/pillarbox-web';

pillarbox.addLanguage('de', de);
pillarbox.addLanguage('en', en);
pillarbox.addLanguage('fr', fr);
pillarbox.addLanguage('it', it);
pillarbox.addLanguage('rm', rm);
function extendLanguage(code, data) {
pillarbox.addLanguage(code, {
...pillarbox.options.language[code] ?? {},
...data
});
}

extendLanguage('de', de);
extendLanguage('en', en);
extendLanguage('fr', fr);
extendLanguage('it', it);
extendLanguage('rm', rm);
31 changes: 31 additions & 0 deletions packages/skip-button/test/language.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import '../src/lang';
import pillarbox from '@srgssr/pillarbox-web';

window.HTMLMediaElement.prototype.load = () => {};

describe('Language', () => {
let player;

beforeEach(() => {
document.body.innerHTML = '<video id="test-video" class="video-js"></video>';

const videoElement = document.querySelector('#test-video');

player = pillarbox(videoElement);
});

afterEach(() => {
player.dispose();
vi.resetAllMocks();
});

it('should use the correct French translations', () => {
// When
player.language('fr');

// Then
expect(player.localize('Play')).toBe('Lecture');
expect(player.localize('Skip credits')).toBe('Passer');
});
});
15 changes: 11 additions & 4 deletions packages/skip-button/vite.config.lib.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { defineConfig } from 'vite';
import babel from '@rollup/plugin-babel';
import copy from 'rollup-plugin-copy';

/**
* Vite's configuration for the lib build.
Expand All @@ -19,10 +20,16 @@ export default defineConfig({
},
rollupOptions: {
external: ['@srgssr/pillarbox-web'],
plugins: [babel({
babelHelpers: 'bundled',
exclude: 'node_modules/**'
})]
plugins: [
babel({
babelHelpers: 'bundled',
exclude: 'node_modules/**'
}),
copy({
targets: [{ src: 'src/lang/*.json', dest: 'dist/lang' }],
hook: 'writeBundle'
})
]
}
}
});
5 changes: 4 additions & 1 deletion scripts/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ export default function(plop) {
templateFiles: './template/**',
globOptions: {
dot: true,
ignore: !data.wantLocalization ? '**/src/lang/**' : undefined
ignore: !data.wantLocalization ? [
'**/src/lang/**',
'**/test/language.spec.js.hbs'
] : undefined
},
data: {
importAlias: data.platform === 'pillarbox' ? '@srgssr/pillarbox-web' : 'video.js'
Expand Down
27 changes: 17 additions & 10 deletions scripts/template/src/lang/index.js.hbs
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import * as de from './de.json';
import * as en from './en.json';
import * as fr from './fr.json';
import * as it from './it.json';
import * as rm from './rm.json';
import de from './de.json';
import en from './en.json';
import fr from './fr.json';
import it from './it.json';
import rm from './rm.json';
import {{platform}} from '{{importAlias}}';

{{platform}}.addLanguage('de', de);
{{platform}}.addLanguage('en', en);
{{platform}}.addLanguage('fr', fr);
{{platform}}.addLanguage('it', it);
{{platform}}.addLanguage('rm', rm);
function extendLanguage(code, data) {
{{platform}}.addLanguage(code, {
...{{platform}}.options.language[code] ?? {},
...data
});
}

extendLanguage('de', de);
extendLanguage('en', en);
extendLanguage('fr', fr);
extendLanguage('it', it);
extendLanguage('rm', rm);
Loading

0 comments on commit 2efcfd1

Please sign in to comment.