Localization (i18n)

@mattebox/player@0.18.0Needs @mattebox/player-core ^0.11.0 and mattebox ^0.21.0.

Every package ships the labels of its controls in English and in 11 other languages. You register the locales your page offers with MatteboxPlayerElement.addLocale. Each player then takes its locale from its language attribute, or from the nearest lang attribute.

Shipped locales

Each package ships these locales besides English: ar, ca, de, es, fr, hi, it, ja, ko, pt (European) and zh (Simplified).

  • Each locale is a separate entry point, <package>/locales/<language>. A page downloads only the locales it imports.
  • The translations are machine drafts. No native speaker has reviewed them yet.

addLocale

MatteboxPlayerElement.addLocale(language, ...sources) registers a locale for every player on the page. Pass one source per package:

import { MatteboxPlayerElement } from '@mattebox/player';
import de from '@mattebox/player/locales/de';
import playlistDe from '@mattebox/player-playlist/locales/de';

MatteboxPlayerElement.addLocale('de', de, playlistDe);

A source can be a function that returns a dynamic import. The function runs only when a player uses that locale, so a page can register every locale it offers and download one:

MatteboxPlayerElement.addLocale('fr', () => import('@mattebox/player/locales/fr'));
  • A later addLocale call for the same language is merged over the earlier one. addLocale('de', { play: 'Abspielen' }) changes one message.
  • A locale is an object of message ids and strings. The ids and their English text are in the PlayerMessages type, and in PlaylistMessages, CastMessages, GamepadMessages, AdsMessages, LiveCaptionsMessages, SubtitleTranslationMessages and DiagnosticsMessages for the packages.
  • A locale that misses an id fails to compile with satisfies PlayerMessages.

Label resolution order

A control takes its text from the first of these that has it:

  1. Its own label-* attribute. See Labels.
  2. The locale of the player’s language attribute, such as language="fr".
  3. The locale of the nearest lang attribute: on the player, an ancestor, or <html>.
  4. English, which is built into the controls.
  • A tag with a region falls back to its language: de-CH uses de.
  • A message that a locale lacks shows in English.
  • A change of language, or of lang on <html> or on the player, updates every label at once, without a reload.
  • A change of lang on another ancestor is read at the next connect. Set language instead.

message() and localechange

A control you write reads a message with player.message(id). It returns undefined when the locale has no such message.

localechange fires on the player when the locale changes or finishes loading. Render your control again in its listener.

Localized text from your page

A block’s title and message, and a marker’s label, take a string or an object of strings by language tag. The player picks the entry of its own locale, with the same fallback as the labels. See chapter 11.

Example

A page in French that offers German, and downloads each locale only when a player uses it:

<html lang="fr">
  <mattebox-player controls="custom" src="vod/master.m3u8">
    <mbx-control-bar>
      <mbx-play-button></mbx-play-button>
      <mbx-seek-bar></mbx-seek-bar>
      <mbx-subtitles-menu></mbx-subtitles-menu>
    </mbx-control-bar>
  </mattebox-player>

  <script type="module">
    import { MatteboxPlayerElement } from '@mattebox/player';

    MatteboxPlayerElement.addLocale('fr', () => import('@mattebox/player/locales/fr'));
    MatteboxPlayerElement.addLocale('de', () => import('@mattebox/player/locales/de'));

    // The page's own language switch.
    document.querySelector('#german').addEventListener('click', () => {
      document.querySelector('mattebox-player').language = 'de';
    });
  </script>
</html>

Read this page's source on GitHub