Skip to content

Self-hosted personal gallery application with online photo management and albums, supporting Live/Motion Photos, EXIF parsing, geolocation recognition, and an explore map.

fix(i18n): fallback to `en` locale for unmapped locale codes (#112)

Timothy Yin79b49050296b
diff --git a/i18n/i18n.config.ts b/i18n/i18n.config.ts
index b2167db6..3520fcf2 100644
--- a/i18n/i18n.config.ts
+++ b/i18n/i18n.config.ts
@@ -1,10 +1,13 @@
export default defineI18nConfig(() => {
return {
fallbackLocale: {
+ 'zh-CN': ['zh-Hans'],
+ 'zh-SG': ['zh-Hans'],
+ 'zh': ['zh-Hans'],
'zh-Hant': ['zh-Hant-TW', 'zh-Hant-HK'],
'zh-TW': ['zh-Hant-TW'],
'zh-HK': ['zh-Hant-HK'],
- 'zh': ['zh-Hans'],
+ 'zh-MO': ['zh-Hant-HK'],
'default': ['en'],
},
}
diff --git a/i18n/localeDetector.ts b/i18n/localeDetector.ts
index 7c7fd593..e23297b5 100644
--- a/i18n/localeDetector.ts
+++ b/i18n/localeDetector.ts
@@ -1,26 +1,39 @@
// Detect based on query, cookie, header
export default defineI18nLocaleDetector((event, config) => {
+ // Supported locales from nuxt config
+ // todo this is hardcoded, maybe find a better way to sync with nuxt config
+ const SUPPORTED_LOCALES = ['zh-Hans', 'zh-Hant-TW', 'zh-Hant-HK', 'en', 'ja']
+
// Helper function to normalize locale codes
- const normalizeLocale = (locale: string | undefined): string | undefined => {
- if (!locale) return undefined
-
- // Map common locale codes to our configured locales
- const localeMap: Record<string, string> = {
- 'zh-CN': 'zh-Hans',
- 'zh-SG': 'zh-Hans',
- zh: 'zh-Hans',
- 'zh-TW': 'zh-Hant-TW',
- 'zh-HK': 'zh-Hant-HK',
- 'zh-MO': 'zh-Hant-HK',
+ const normalizeLocale = (locale: string | undefined): string => {
+ if (!locale) return config.defaultLocale || 'en'
+
+ // Check if locale exists in configured locales
+ if (SUPPORTED_LOCALES.includes(locale)) {
+ return locale
+ }
+
+ // Try to get fallback locale from config.fallbackLocale
+ const fallbackLocales = config.fallbackLocale as
+ | Record<string, string[]>
+ | undefined
+ if (fallbackLocales && fallbackLocales[locale]) {
+ const fallbacks = fallbackLocales[locale]
+ for (const fallback of fallbacks) {
+ if (SUPPORTED_LOCALES.includes(fallback)) {
+ return fallback
+ }
+ }
}
- return localeMap[locale] || locale
+ // Fall back to default locale
+ return config.defaultLocale || 'en'
}
// try to get locale from query
const query = tryQueryLocale(event, { lang: '' }) // disable locale default value with `lang` option
if (query) {
- return normalizeLocale(query.toString()) || query.toString()
+ return normalizeLocale(query.toString())
}
// try to get locale from cookie
@@ -29,13 +42,13 @@ export default defineI18nLocaleDetector((event, config) => {
name: 'chronoframe-locale',
}) // disable locale default value with `lang` option
if (cookie) {
- return normalizeLocale(cookie.toString()) || cookie.toString()
+ return normalizeLocale(cookie.toString())
}
// try to get locale from header (`accept-header`)
const header = tryHeaderLocale(event, { lang: '' }) // disable locale default value with `lang` option
if (header) {
- return normalizeLocale(header.toString()) || header.toString()
+ return normalizeLocale(header.toString())
}
// If the locale cannot be resolved up to this point, it is resolved with the value `defaultLocale` of the locale config passed to the function