2022-05-17 07:47:35 +02:00
|
|
|
/**
|
|
|
|
* Although videojs has its own i18n system, it is not trivial to pass our
|
|
|
|
* translation data to it. Instead of maintaining two sets of localization
|
|
|
|
* systems, we'll just ignore the videojs one and update the strings ourselves
|
|
|
|
* through events.
|
|
|
|
*
|
|
|
|
* We use events because that's the only way to update components with dynamic
|
|
|
|
* text like "Play/Pause" in videojs. We can't just define the set of texts at
|
|
|
|
* initialization, unless we use custom components to do that.
|
|
|
|
*
|
|
|
|
* For the case of 'MuteToggle', videojs changes the text at 'loadstart', so
|
|
|
|
* this event was chosen as the "lowest common denominator" to update the other
|
|
|
|
* static-text components.
|
2022-05-17 08:57:14 +02:00
|
|
|
*
|
|
|
|
* --- Notes ---
|
2022-05-17 09:10:28 +02:00
|
|
|
* (1) 'AutoplayNextButton' and 'TheaterModeButton' handles i18n (among other
|
|
|
|
* things) on its own.
|
2022-05-17 07:47:35 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
// @flow
|
|
|
|
import videojs from 'video.js';
|
|
|
|
import type { Player } from '../../videojs';
|
|
|
|
|
|
|
|
const VERSION = '1.0.0';
|
|
|
|
const defaultOptions = {};
|
|
|
|
|
2022-05-17 10:15:38 +02:00
|
|
|
function logError(msg) {
|
|
|
|
// @if process.env.LOG_VIDEOJS_I18N='true'
|
|
|
|
console.error(msg);
|
|
|
|
// @endif
|
|
|
|
}
|
|
|
|
|
2022-05-17 07:47:35 +02:00
|
|
|
function setLabel(controlBar, childName, label) {
|
|
|
|
try {
|
|
|
|
controlBar.getChild(childName).controlText(label);
|
|
|
|
} catch (e) {
|
|
|
|
// We want to be notified, at least on dev, over any structural changes,
|
|
|
|
// so don't check for null children and let the error surface.
|
2022-05-17 10:15:38 +02:00
|
|
|
logError(childName + ': ' + e);
|
2022-05-17 07:47:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function resolveCtrlText(e, player) {
|
|
|
|
if (player) {
|
|
|
|
const ctrlBar = player.getChild('controlBar');
|
|
|
|
switch (e.type) {
|
|
|
|
case 'play':
|
|
|
|
setLabel(ctrlBar, 'PlayToggle', __('Pause (space)'));
|
|
|
|
break;
|
2022-05-17 10:15:38 +02:00
|
|
|
|
2022-05-17 07:47:35 +02:00
|
|
|
case 'pause':
|
|
|
|
setLabel(ctrlBar, 'PlayToggle', __('Play (space)'));
|
|
|
|
break;
|
2022-05-17 10:15:38 +02:00
|
|
|
|
2022-05-17 07:47:35 +02:00
|
|
|
case 'volumechange':
|
2022-05-17 10:15:38 +02:00
|
|
|
try {
|
|
|
|
ctrlBar
|
|
|
|
.getChild('VolumePanel')
|
|
|
|
.getChild('MuteToggle')
|
|
|
|
.controlText(player.muted() || player.volume() === 0 ? __('Unmute (m)') : __('Mute (m)'));
|
|
|
|
} catch (e) {
|
|
|
|
logError('MuteToggle: ' + e);
|
|
|
|
}
|
2022-05-17 07:47:35 +02:00
|
|
|
break;
|
2022-05-17 10:15:38 +02:00
|
|
|
|
2022-05-17 07:47:35 +02:00
|
|
|
case 'fullscreenchange':
|
|
|
|
setLabel(ctrlBar, 'FullscreenToggle', player.isFullscreen() ? __('Exit Fullscreen (f)') : __('Fullscreen (f)'));
|
|
|
|
break;
|
2022-05-17 10:15:38 +02:00
|
|
|
|
2022-05-17 07:47:35 +02:00
|
|
|
case 'loadstart':
|
|
|
|
// --- Do everything ---
|
|
|
|
setLabel(ctrlBar, 'PlaybackRateMenuButton', __('Playback Rate (<, >)'));
|
|
|
|
setLabel(ctrlBar, 'QualityButton', __('Quality'));
|
|
|
|
setLabel(ctrlBar, 'PlayNextButton', __('Play Next (SHIFT+N)'));
|
|
|
|
setLabel(ctrlBar, 'PlayPreviousButton', __('Play Previous (SHIFT+P)'));
|
2022-05-17 10:15:38 +02:00
|
|
|
setLabel(ctrlBar, 'ChaptersButton', __('Chapters'));
|
|
|
|
setLabel(ctrlBar, 'ChromecastButton', __('Open Chromecast menu'));
|
|
|
|
setLabel(ctrlBar, 'FullscreenToggle', player.isFullscreen() ? __('Exit Fullscreen (f)') : __('Fullscreen (f)'));
|
|
|
|
|
|
|
|
try {
|
|
|
|
ctrlBar
|
|
|
|
.getChild('VolumePanel')
|
|
|
|
.getChild('MuteToggle')
|
|
|
|
.controlText(player.muted() || player.volume() === 0 ? __('Unmute (m)') : __('Mute (m)'));
|
|
|
|
} catch (e) {
|
|
|
|
logError('MuteToggle: ' + e);
|
|
|
|
}
|
2022-05-17 07:47:35 +02:00
|
|
|
|
|
|
|
resolveCtrlText({ type: 'play' });
|
|
|
|
resolveCtrlText({ type: 'pause' });
|
|
|
|
resolveCtrlText({ type: 'volumechange' });
|
|
|
|
resolveCtrlText({ type: 'fullscreenchange' });
|
|
|
|
break;
|
2022-05-17 10:15:38 +02:00
|
|
|
|
2022-05-17 07:47:35 +02:00
|
|
|
default:
|
2022-05-17 10:15:38 +02:00
|
|
|
logError('Unexpected: ' + e.type);
|
|
|
|
break;
|
2022-05-17 07:47:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function onPlayerReady(player: Player, options: {}) {
|
|
|
|
const h = (e) => resolveCtrlText(e, player);
|
|
|
|
|
|
|
|
player.on('play', h);
|
|
|
|
player.on('pause', h);
|
|
|
|
player.on('loadstart', h);
|
|
|
|
player.on('fullscreenchange', h);
|
|
|
|
player.on('volumechange', h);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Odysee custom i18n plugin.
|
|
|
|
* @param options
|
|
|
|
*/
|
|
|
|
function i18n(options: {}) {
|
|
|
|
this.ready(() => onPlayerReady(this, videojs.mergeOptions(defaultOptions, options)));
|
|
|
|
}
|
|
|
|
|
|
|
|
videojs.registerPlugin('i18n', i18n);
|
|
|
|
i18n.VERSION = VERSION;
|
|
|
|
|
|
|
|
export default i18n;
|