-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1523 from yext/feature/chinese-i18n
chinese i18n tweaks update edge supported locales for voice search (#1519) rename zh-CN to zh-Hans and zh-TW to zh-Hant (#1517)
- Loading branch information
Showing
6 changed files
with
128 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
File renamed without changes.
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* Parses a locale code into its constituent parts. | ||
* Performs case formatting on the result. | ||
* | ||
* @param {string} localeCode | ||
* @returns { language: string, modifier?: string, region?: string } | ||
*/ | ||
export 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 Error( | ||
`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 (parsedLocale.modifier) { | ||
parsedLocale.modifier = capitalizeFirstLetterOnly(parsedLocale.modifier); | ||
} | ||
if (parsedLocale.region) { | ||
parsedLocale.region = parsedLocale.region.toUpperCase(); | ||
} | ||
|
||
return parsedLocale; | ||
} |
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