Skip to content

Commit

Permalink
feat: Enhance Audio Language Switching (#5223)
Browse files Browse the repository at this point in the history
Resolves #5222.

It also opens the way for improving the compliance with [RFC
5646](https://datatracker.ietf.org/doc/html/rfc5646) in the future.
  • Loading branch information
grushetsky authored May 11, 2023
1 parent 0da63a0 commit fa041d7
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Jozef Chúťka <[email protected]>
Jun Hong Chong <[email protected]>
Jürgen Kartnaller <[email protected]>
JW Player <*@jwplayer.com>
Konstantin Grushetsky <[email protected]>
Lucas Gabriel Sánchez <[email protected]>
Martin Stark <[email protected]>
Matthias Van Parijs <[email protected]>
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ Jozef Chúťka <[email protected]>
Julian Domingo <[email protected]>
Jun Hong Chong <[email protected]>
Jürgen Kartnaller <[email protected]>
Konstantin Grushetsky <[email protected]>
Leandro Ribeiro Moreira <[email protected]>
Lucas Gabriel Sánchez <[email protected]>
Martin Stark <[email protected]>
Expand Down
19 changes: 11 additions & 8 deletions lib/util/language_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,16 @@ shaka.util.LanguageUtils = class {
static normalize(locale) {
const LanguageUtils = shaka.util.LanguageUtils;

const components = locale.split('-');
const privateusePrefix = 'x-';
const components = locale.split(`-${privateusePrefix}`);
const languageAndRegion = components[0].split('-');

// We are only going to use the language and the region. If there was
// a dialect or anything else, we are throwing it a way.
let language = components[0] || '';
let region = components[1] || '';
// We are only going to use the language, the region and the "privateuse" part (as per https://datatracker.ietf.org/doc/html/rfc5646).
// Anything else is thrown away.
let language = languageAndRegion[0] || '';
let region = languageAndRegion[1] || '';
const privateuse = components[1] ?
`${privateusePrefix}${components[1]}` : '';

// Convert the language to lower case. It is standard for the language code
// to be in lower case, but it will also make the map look-up easier.
Expand All @@ -208,9 +212,8 @@ shaka.util.LanguageUtils = class {
// and this will be a no-op.
region = region.toUpperCase();

return region ?
language + '-' + region :
language;
return `${region ? `${language}-${region}` : language}${
privateuse ? `-${privateuse}` : ''}`;
}

/**
Expand Down
11 changes: 9 additions & 2 deletions test/util/language_utils_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,23 @@ describe('LanguageUtils', () => {
expect(LanguageUtils.normalize('EN')).toBe('en');
});

it('standardized region', () => {
it('standardizes region', () => {
expect(LanguageUtils.normalize('en-US')).toBe('en-US');
expect(LanguageUtils.normalize('en-us')).toBe('en-US');
});

it('ignored unknown base languages', () => {
it('ignores unknown base languages', () => {
expect(LanguageUtils.normalize('elvish')).toBe('elvish');
expect(LanguageUtils.normalize(
'elvish-woodland')).toBe('elvish-WOODLAND');
});

it('supports private use tag', () => {
expect(LanguageUtils.normalize('eng-us-x-1')).toBe('en-US-x-1');
expect(LanguageUtils.normalize('eng-x-1')).toBe('en-x-1');
expect(LanguageUtils.normalize(
'elvish-woodland-x-1')).toBe('elvish-WOODLAND-x-1');
});
});

describe('getLocaleForText', () => {
Expand Down

0 comments on commit fa041d7

Please sign in to comment.