Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add i18n support with language optin in VideoJS #235

Merged
merged 2 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/components/MediaPlayer/MediaPlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ const MediaPlayer = ({ enableFileDownload = false, enablePIP = false }) => {
poster: isVideo ? getPoster(manifest, canvasIndex) : null,
controls: true,
fluid: true,
language: "en", // TODO:: fill this information from props
controlBar: {
// Define and order control bar controls
// See https://docs.videojs.com/tutorial-components.html for options of what
Expand Down
22 changes: 16 additions & 6 deletions src/components/MediaPlayer/MediaPlayer.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { render, screen, waitFor } from '@testing-library/react';
import { act, render, screen, waitFor } from '@testing-library/react';
import { withManifestAndPlayerProvider } from '../../services/testing-helpers';
import MediaPlayer from './MediaPlayer';
import audioManifest from '@TestData/transcript-canvas';
Expand All @@ -10,6 +10,16 @@ let manifestState = {
playlist: { isPlaylist: false, markers: [], isEditing: false }
};
describe('MediaPlayer component', () => {
let originalError;
beforeEach(() => {
originalError = console.error;
console.error = jest.fn();
});

afterAll(() => {
console.error = originalError;
});

describe('with audio manifest', () => {
beforeEach(() => {
const PlayerWithManifest = withManifestAndPlayerProvider(MediaPlayer, {
Expand Down Expand Up @@ -67,7 +77,7 @@ describe('MediaPlayer component', () => {
initialPlayerState: {},
enableFileDownload: true,
});
render(<PlayerWithManifest />);
await act(async () => render(<PlayerWithManifest />));
expect(screen.queryByTestId('videojs-file-download')).toBeInTheDocument();
});
});
Expand Down Expand Up @@ -96,12 +106,12 @@ describe('MediaPlayer component', () => {
});

describe('with multiple canvases', () => {
test('renders previous/next section buttons', () => {
test('renders previous/next section buttons', async () => {
const PlayerWithManifest = withManifestAndPlayerProvider(MediaPlayer, {
initialManifestState: { ...manifestState, manifest: videoManifest, canvasIndex: 0 },
initialPlayerState: {},
});
render(<PlayerWithManifest />);
await act(async () => render(<PlayerWithManifest />));
expect(screen.queryByTestId('videojs-next-button')).toBeInTheDocument();
expect(screen.queryByTestId('videojs-previous-button')).toBeInTheDocument();
});
Expand All @@ -126,7 +136,7 @@ describe('MediaPlayer component', () => {
expect(screen.getByText('You do not have permission to playback this item.')).toBeInTheDocument();
});

test('renders player for a accessible Canvas', () => {
test('renders player for a accessible Canvas', async () => {
const PlayerWithManifest = withManifestAndPlayerProvider(MediaPlayer, {
initialManifestState: {
manifest: playlistManifest,
Expand All @@ -135,7 +145,7 @@ describe('MediaPlayer component', () => {
},
initialPlayerState: {},
});
render(<PlayerWithManifest />);
await act(async () => render(<PlayerWithManifest />));
expect(screen.queryByTestId('inaccessible-item')).not.toBeInTheDocument();
expect(
screen.queryAllByTestId('videojs-video-element').length
Expand Down
27 changes: 25 additions & 2 deletions src/components/MediaPlayer/VideoJS/VideoJSPlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React from 'react';
import PropTypes from 'prop-types';
import videojs from 'video.js';
import 'videojs-hotkeys';

import 'videojs-markers-plugin/dist/videojs-markers-plugin';
import 'videojs-markers-plugin/dist/videojs.markers.plugin.css';

Expand All @@ -26,6 +25,7 @@ import {
} from '@Services/iiif-parser';
import { checkSrcRange, getMediaFragment } from '@Services/utility-helpers';

/** VideoJS custom components */
import VideoJSProgress from './components/js/VideoJSProgress';
import VideoJSCurrentTime from './components/js/VideoJSCurrentTime';
import VideoJSFileDownload from './components/js/VideoJSFileDownload';
Expand Down Expand Up @@ -91,22 +91,45 @@ function VideoJSPlayer({
let currentNavItemRef = React.useRef();
currentNavItemRef.current = currentNavItem;

// Using dynamic imports to enforce code-splitting in webpack
// https://webpack.js.org/api/module-methods/#dynamic-expressions-in-import
const loadResources = async (langKey) => {
try {
const resources = await import(`video.js/dist/lang/${langKey}.json`);
return resources;
} catch (e) {
console.error(`${langKey} is not available, defaulting to English`);
const resources = await import('video.js/dist/lang/en.json');
return resources;
}
};

/**
* Initialize player when creating for the first time and cleanup
* when unmounting after the player is being used
*/
React.useEffect(() => {
React.useEffect(async () => {
const options = {
...videoJSOptions,
};

setCIndex(canvasIndex);

// Dynamically load the selected language from VideoJS's lang files
let selectedLang;
await loadResources(options.language)
.then((res) => {
selectedLang = JSON.stringify(res);
});
let languageJSON = JSON.parse(selectedLang);

let newPlayer;
if (playerRef.current != null) {
videojs.addLanguage(options.language, languageJSON);
newPlayer = videojs(playerRef.current, options);
}


/* Another way to add a component to the controlBar */
// newPlayer.getChild('controlBar').addChild('vjsYo', {});

Expand Down