diff options
| author | rxliuli <rxliuli@gmail.com> | 2025-11-04 05:03:50 +0800 |
|---|---|---|
| committer | rxliuli <rxliuli@gmail.com> | 2025-11-04 05:03:50 +0800 |
| commit | bce557cc2dc767628bed6aac87301a1be7c5431b (patch) | |
| tree | b51a051228d01fe3306cd7626d4a96768aadb944 /shared/localization/src/getLocAttributes.ts | |
init commit
Diffstat (limited to 'shared/localization/src/getLocAttributes.ts')
| -rw-r--r-- | shared/localization/src/getLocAttributes.ts | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/shared/localization/src/getLocAttributes.ts b/shared/localization/src/getLocAttributes.ts new file mode 100644 index 0000000..2f462db --- /dev/null +++ b/shared/localization/src/getLocAttributes.ts @@ -0,0 +1,78 @@ +import { getPageDir } from './getPageDir'; + +/** + * Checks if a string contains language script + * ex. "zh-Hant-HK", "zh-Hant-TW", "zh-Hans-CN" + * @param {string} locale + * @returns {boolean} + */ +const hasSupportedLanguageScript = (locale: string): boolean => { + const SUPPORTED_SCRIPTS = ['-hans-', '-hant-']; + + const formattedLocale = locale.toLowerCase(); + return SUPPORTED_SCRIPTS.some((item) => formattedLocale.includes(item)); +}; + +/** + * + * BCP47 https://www.w3.org/International/articles/language-tags/ + * + * @param {string} language https://en.wikipedia.org/wiki/ISO_639 + * @param {string} region https://en.wikipedia.org/wiki/ISO_3166-1 + * @param {string} script https://en.wikipedia.org/wiki/ISO_15924 + + */ +const buildBcp47String = ( + language: string, + region: string, + script?: string, +): string => { + let capitalizeScript: string | null = null; + if (script) { + capitalizeScript = + script[0].toUpperCase() + script.substring(1).toLowerCase(); + } + let bcp47Arr = [ + language.toLowerCase(), + capitalizeScript, + region.toUpperCase(), + ]; + + return bcp47Arr.filter((item) => item !== null).join('-'); +}; + +/** + * @description + * get values to be used in <html> tag lang and dir attributes. + * + * @param {string} locale + * @returns { { dir: 'rtl' | 'ltr', lang: string }} HTML dir + lang values + */ + +export function getLocAttributes(locale: string): { + dir: 'rtl' | 'ltr'; + lang: string; +} { + const pageDir = getPageDir(locale); + let bcp47 = locale; + + const localeStrings = locale.split('-'); + + // region index in array + const regionIndex = hasSupportedLanguageScript(locale) ? 2 : 1; + + const language = localeStrings[0]; + const script = hasSupportedLanguageScript(locale) + ? localeStrings[1] + : undefined; + const region = localeStrings[regionIndex]; + + if (language && region) { + bcp47 = buildBcp47String(language, region, script); + } + + return { + dir: pageDir, + lang: bcp47, + }; +} |
