-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update canonicalizeLocale to handle 中文(chinese) (#239)
J=SLAP-1526 TEST=auto,manual unit tests ran build with zh-hans-ch in locale config, saw that the generated source code used the correct locale in the page tried building a page with a locale with >3 sections, got a jambo warning but build continued without changing the locale
- Loading branch information
Showing
3 changed files
with
145 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,87 @@ | ||
const UserError = require('../errors/usererror'); | ||
|
||
/** | ||
* Normalizes a locale code | ||
* | ||
* @param {string} localeCode | ||
* @returns {string} | ||
*/ | ||
canonicalizeLocale = function(localeCode) { | ||
exports.canonicalizeLocale = function(localeCode) { | ||
if (!localeCode) { | ||
return; | ||
} | ||
const localeCodeSections = localeCode.replace('-', '_') | ||
.split('_'); | ||
|
||
const languageIndex = 0; | ||
const regionIndex = 1; | ||
const { language, modifier, region } = parseLocale(localeCode); | ||
return formatLocale(language, modifier, region); | ||
} | ||
|
||
localeCodeSections[languageIndex] = localeCodeSections[languageIndex].toLowerCase(); | ||
/** | ||
* Parses a locale code into its constituent parts. | ||
* Performs case formatting on the result. | ||
* | ||
* @param {string} localeCode | ||
* @returns { language: string, modifier?: string, region?: string } | ||
*/ | ||
function parseLocale(localeCode) { | ||
const localeCodeSections = localeCode.replace(/-/g, '_').split('_'); | ||
const language = localeCodeSections[0].toLowerCase(); | ||
const parseModifierAndRegion = () => { | ||
const numSections = localeCodeSections.length; | ||
if (numSections === 1) { | ||
return {}; | ||
} else if (numSections === 2 && language === 'zh') { | ||
const ambiguous = localeCodeSections[1].toLowerCase(); | ||
if (['hans', 'hant'].includes(ambiguous)) { | ||
return { modifier: ambiguous }; | ||
} else { | ||
return { region: ambiguous }; | ||
} | ||
} else if (numSections === 2) { | ||
return { region: localeCodeSections[1] }; | ||
} else if (numSections === 3) { | ||
return { | ||
modifier: localeCodeSections[1], | ||
region: localeCodeSections[2] | ||
}; | ||
} else if (numSections > 3) { | ||
throw new UserError( | ||
`Encountered strangely formatted locale "${localeCode}", ` + | ||
`with ${numSections} sections.`); | ||
} | ||
} | ||
const capitalizeFirstLetterOnly = raw => { | ||
return raw.charAt(0).toUpperCase() + raw.slice(1).toLowerCase(); | ||
} | ||
const parsedLocale = { | ||
language, | ||
...parseModifierAndRegion() | ||
}; | ||
|
||
if (localeCodeSections.length > regionIndex) { | ||
localeCodeSections[regionIndex] = localeCodeSections[regionIndex].toUpperCase(); | ||
if (parsedLocale.modifier) { | ||
parsedLocale.modifier = capitalizeFirstLetterOnly(parsedLocale.modifier); | ||
} | ||
if (parsedLocale.region) { | ||
parsedLocale.region = parsedLocale.region.toUpperCase(); | ||
} | ||
|
||
return localeCodeSections.join('_'); | ||
return parsedLocale; | ||
} | ||
exports.canonicalizeLocale = canonicalizeLocale; | ||
exports.parseLocale = parseLocale; | ||
|
||
/** | ||
* Formats a locale code given its constituent parts. | ||
* | ||
* @param {string} language zh in zh-Hans_CH | ||
* @param {string?} modifier Hans in zh-Hans_CH | ||
* @param {string?} region CH in zh-Hans_CH | ||
* @returns | ||
*/ | ||
function formatLocale(language, modifier, region) { | ||
let result = language.toLowerCase(); | ||
if (modifier) { | ||
result += '-' + modifier; | ||
} | ||
if (region) { | ||
result += '_' + region; | ||
} | ||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters