Skip to content

Commit

Permalink
chore(language): make normalize function more readable through destru…
Browse files Browse the repository at this point in the history
…cturing (#6094)
  • Loading branch information
vanyaxk authored and avelad committed Jan 16, 2024
1 parent 74b7bf3 commit 863d116
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions lib/util/language_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,25 +192,24 @@ shaka.util.LanguageUtils = class {
const LanguageUtils = shaka.util.LanguageUtils;

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

// 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]}` : '';
const privateuse = privateuseSuffix ?
`${privateusePrefix}${privateuseSuffix}` : '';

// 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.
language = language.toLowerCase();
let language = languageCode.toLowerCase();
language = LanguageUtils.isoMap_.get(language) || language;

// Convert the region to upper case. It is standard for the region to be in
// upper case. If there is no upper code, then it will be an empty string
// and this will be a no-op.
region = region.toUpperCase();
const region = regionCode.toUpperCase();

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

0 comments on commit 863d116

Please sign in to comment.