From 5cb2fd743c6a07836f595de1b6340a833f14afe6 Mon Sep 17 00:00:00 2001 From: Frudrax Cheng Date: Sat, 21 Dec 2024 23:22:20 +0800 Subject: [PATCH] Convert language codes from RFC-1766 to RFC-4646 --- lingui.config.js | 2 +- src/components/hooks/dates.ts | 6 ++--- src/locale/deviceLocales.ts | 24 ++++++++++--------- src/locale/helpers.ts | 8 +++---- src/locale/i18n.ts | 10 ++++---- src/locale/i18n.web.ts | 8 +++---- src/locale/languages.ts | 8 +++---- src/locale/locales/{zh-HK => yue}/messages.po | 0 8 files changed, 34 insertions(+), 32 deletions(-) rename src/locale/locales/{zh-HK => yue}/messages.po (100%) diff --git a/lingui.config.js b/lingui.config.js index da43e35615..013cb7ce2b 100644 --- a/lingui.config.js +++ b/lingui.config.js @@ -29,8 +29,8 @@ module.exports = { 'tr', 'uk', 'vi', + 'yue', 'zh-CN', - 'zh-HK', 'zh-TW', ], catalogs: [ diff --git a/src/components/hooks/dates.ts b/src/components/hooks/dates.ts index a1967316cd..09d48620b5 100644 --- a/src/components/hooks/dates.ts +++ b/src/components/hooks/dates.ts @@ -73,9 +73,9 @@ const locales: Record = { tr, uk, vi, - ['zh-CN']: zhCN, - ['zh-HK']: zhHK, - ['zh-TW']: zhTW, + ['yue']: zhHK, + ['zh-Hans-CN']: zhCN, + ['zh-Hant-TW']: zhTW, } /** diff --git a/src/locale/deviceLocales.ts b/src/locale/deviceLocales.ts index 1abaa20f69..7827fecd31 100644 --- a/src/locale/deviceLocales.ts +++ b/src/locale/deviceLocales.ts @@ -14,7 +14,7 @@ type LocalWithLanguageCode = Locale & { * {@link https://github.com/bluesky-social/social-app/pull/4461} * {@link https://xml.coverpages.org/iso639a.html} * - * Convert Chinese language tags for Native. + * Convert Chinese language tags for Web (RFC 1766 to RFC 4646). * * {@link https://datatracker.ietf.org/doc/html/rfc5646#appendix-A} * {@link https://developer.apple.com/documentation/packagedescription/languagetag} @@ -41,17 +41,19 @@ export function getLocales() { } if (typeof locale.languageTag === 'string') { - if (locale.languageTag.startsWith('zh-Hans')) { - // Simplified Chinese to zh-CN - locale.languageTag = 'zh-CN' + if ( + locale.languageTag === 'zh-CN' || + locale.languageTag.startsWith('zh-Hans') + ) { + // Simplified Chinese to zh-Hans-CN + locale.languageTag = 'zh-Hans-CN' } - if (locale.languageTag.startsWith('zh-Hant')) { - // Traditional Chinese to zh-TW - locale.languageTag = 'zh-TW' - } - if (locale.languageTag.startsWith('yue')) { - // Cantonese (Yue) to zh-HK - locale.languageTag = 'zh-HK' + if ( + locale.languageTag === 'zh-TW' || + locale.languageTag.startsWith('zh-Hant') + ) { + // Traditional Chinese to zh-Hant-TW + locale.languageTag = 'zh-Hant-TW' } } diff --git a/src/locale/helpers.ts b/src/locale/helpers.ts index ed31faa498..17093d131f 100644 --- a/src/locale/helpers.ts +++ b/src/locale/helpers.ts @@ -173,11 +173,11 @@ export function sanitizeAppLanguageSetting(appLanguage: string): AppLanguage { return AppLanguage.uk case 'vi': return AppLanguage.vi - case 'zh-CN': + case 'yue': + return AppLanguage.yue + case 'zh-Hans-CN': return AppLanguage.zh_CN - case 'zh-HK': - return AppLanguage.zh_HK - case 'zh-TW': + case 'zh-Hant-TW': return AppLanguage.zh_TW default: continue diff --git a/src/locale/i18n.ts b/src/locale/i18n.ts index 167e36d354..25e735ba50 100644 --- a/src/locale/i18n.ts +++ b/src/locale/i18n.ts @@ -39,8 +39,8 @@ import {messages as messagesTh} from '#/locale/locales/th/messages' import {messages as messagesTr} from '#/locale/locales/tr/messages' import {messages as messagesUk} from '#/locale/locales/uk/messages' import {messages as messagesVi} from '#/locale/locales/vi/messages' +import {messages as messagesYue} from '#/locale/locales/yue/messages' import {messages as messagesZh_CN} from '#/locale/locales/zh-CN/messages' -import {messages as messagesZh_HK} from '#/locale/locales/zh-HK/messages' import {messages as messagesZh_TW} from '#/locale/locales/zh-TW/messages' import {useLanguagePrefs} from '#/state/preferences' @@ -261,16 +261,16 @@ export async function dynamicActivate(locale: AppLanguage) { ]) break } - case AppLanguage.zh_CN: { - i18n.loadAndActivate({locale, messages: messagesZh_CN}) + case AppLanguage.yue: { + i18n.loadAndActivate({locale, messages: messagesYue}) await Promise.all([ import('@formatjs/intl-pluralrules/locale-data/zh'), import('@formatjs/intl-numberformat/locale-data/zh'), ]) break } - case AppLanguage.zh_HK: { - i18n.loadAndActivate({locale, messages: messagesZh_HK}) + case AppLanguage.zh_CN: { + i18n.loadAndActivate({locale, messages: messagesZh_CN}) await Promise.all([ import('@formatjs/intl-pluralrules/locale-data/zh'), import('@formatjs/intl-numberformat/locale-data/zh'), diff --git a/src/locale/i18n.web.ts b/src/locale/i18n.web.ts index 83e4acce08..685d964340 100644 --- a/src/locale/i18n.web.ts +++ b/src/locale/i18n.web.ts @@ -120,12 +120,12 @@ export async function dynamicActivate(locale: AppLanguage) { mod = await import(`./locales/vi/messages`) break } - case AppLanguage.zh_CN: { - mod = await import(`./locales/zh-CN/messages`) + case AppLanguage.yue: { + mod = await import(`./locales/yue/messages`) break } - case AppLanguage.zh_HK: { - mod = await import(`./locales/zh-HK/messages`) + case AppLanguage.zh_CN: { + mod = await import(`./locales/zh-CN/messages`) break } case AppLanguage.zh_TW: { diff --git a/src/locale/languages.ts b/src/locale/languages.ts index 6bd0c7f64e..b9abb0c851 100644 --- a/src/locale/languages.ts +++ b/src/locale/languages.ts @@ -33,9 +33,9 @@ export enum AppLanguage { tr = 'tr', uk = 'uk', vi = 'vi', - zh_CN = 'zh-CN', - zh_HK = 'zh-HK', - zh_TW = 'zh-TW', + yue = 'yue', + zh_CN = 'zh-Hans-CN', + zh_TW = 'zh-Hant-TW', } interface AppLanguageConfig { @@ -72,9 +72,9 @@ export const APP_LANGUAGES: AppLanguageConfig[] = [ {code2: AppLanguage.tr, name: 'Türkçe – Turkish'}, {code2: AppLanguage.uk, name: 'Українська – Ukrainian'}, {code2: AppLanguage.vi, name: 'Tiếng Việt – Vietnamese'}, + {code2: AppLanguage.yue, name: '粵文 – Cantonese'}, {code2: AppLanguage.zh_CN, name: '简体中文 – Simplified Chinese'}, {code2: AppLanguage.zh_TW, name: '繁體中文 – Traditional Chinese'}, - {code2: AppLanguage.zh_HK, name: '粵文 – Cantonese'}, ] export const LANGUAGES: Language[] = [ diff --git a/src/locale/locales/zh-HK/messages.po b/src/locale/locales/yue/messages.po similarity index 100% rename from src/locale/locales/zh-HK/messages.po rename to src/locale/locales/yue/messages.po