Skip to content

Commit

Permalink
feat: Enhance Audio Language Switching (shaka-project#5223)
Browse files Browse the repository at this point in the history
Resolves shaka-project#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 committed 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
Expand Up @@ -50,6 +50,7 @@ Jozef Chúťka <jozefchutka@gmail.com>
Jun Hong Chong <chongjunhong@gmail.com>
Jürgen Kartnaller <kartnaller@lovelysystems.com>
JW Player <*@jwplayer.com>
Konstantin Grushetsky <github@grushetsky.net>
Lucas Gabriel Sánchez <unkiwii@gmail.com>
Martin Stark <martin.stark@eyevinn.se>
Matthias Van Parijs <matvp91@gmail.com>
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS
Expand Up @@ -76,6 +76,7 @@ Jozef Chúťka <jozefchutka@gmail.com>
Julian Domingo <juliandomingo@google.com>
Jun Hong Chong <chongjunhong@gmail.com>
Jürgen Kartnaller <kartnaller@lovelysystems.com>
Konstantin Grushetsky <github@grushetsky.net>
Leandro Ribeiro Moreira <leandro.ribeiro.moreira@gmail.com>
Lucas Gabriel Sánchez <unkiwii@gmail.com>
Martin Stark <martin.stark@eyevinn.se>
Expand Down
19 changes: 11 additions & 8 deletions lib/util/language_utils.js
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
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.