Skip to content
FrameworkStyle

Switch locale dynamically

Change the player language at runtime in HTML and React

Video.js starts in English and re-resolves translations when you opt into another active locale. How you trigger that depends on the platform.

Prerequisites

  • A target locale from a shipped lazy-loaded pack, a registered custom pack, or a Chrome browser translation fallback

Ambient <html lang>

The simplest switch updates the document language and lets providers pick it up:

document.documentElement.lang = 'fr';

Mounted <media-i18n> providers observe lang on <html> and ancestors. No remount required.

Explicit provider language

<media-i18n lang="es" id="provider">
  <video-player>...</video-player>
</media-i18n>

<script type="module">
  document.querySelector('#lang-picker').addEventListener('change', (e) => {
    document.getElementById('provider').lang = e.target.value;
  });
</script>

Or set document.documentElement.lang when the mounted <media-i18n> inherits ambient language.

Avoid flash while switching

Async lazy loads and browser translation fallback can briefly show English. Preload copy before the active locale changes with one of three common patterns:

Preload at bootstrap

Side-effect locale modules register shipped packs before the provider renders. Preload every language in a fixed picker to avoid the async gap.

import '@videojs/html/i18n/locales/es/register';
import '@videojs/html/i18n/locales/fr/register';
<media-i18n id="provider">
  <video-player>...</video-player>
</media-i18n>

<script type="module">
  document.getElementById('provider').lang = 'fr';
</script>

Browser translation fallback

If a switched locale has no registered or shipped pack, Video.js can use Chrome’s Browser Translation API when the browser exposes globalThis.Translator and already has the target model installed. This is automatic after lazy loading is attempted.

Do not rely on it for instant switching. Preload or register reviewed packs when the language picker should update without a flash of English.

What’s next?