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

feat: Enhance Audio Language Switching #5223

Merged
merged 1 commit into from
May 11, 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 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