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.

refactor(i18n): consolidate shared localization configuration

Andrey Kondalov3018e4545623
diff --git a/i18n/i18n.config.ts b/i18n/i18n.config.ts
index 1e2bdd45..d4d6767a 100644
--- a/i18n/i18n.config.ts
+++ b/i18n/i18n.config.ts
@@ -1,14 +1,7 @@
+import i18nOptions from './i18n.options'
+
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-MO': ['zh-Hant-HK'],
- default: ['en'],
- },
+ fallbackLocale: i18nOptions.fallbackLocale,
}
})
diff --git a/i18n/i18n.options.ts b/i18n/i18n.options.ts
new file mode 100644
index 00000000..7d266333
--- /dev/null
+++ b/i18n/i18n.options.ts
@@ -0,0 +1,91 @@
+import type { NuxtI18nOptions } from '@nuxtjs/i18n'
+import type { LocaleObject } from '@nuxtjs/i18n'
+import type { ModuleOptions as DayjsModuleOptions } from 'dayjs-nuxt'
+import type { I18nOptions } from 'vue-i18n'
+
+export const defaultLocale = 'en'
+
+type DayjsLocale = NonNullable<DayjsModuleOptions['locales']>[number]
+
+type AppLocaleCode =
+ | 'zh-Hans'
+ | 'zh-Hant-TW'
+ | 'zh-Hant-HK'
+ | 'en'
+ | 'ja'
+
+type AppLocaleObject = LocaleObject<AppLocaleCode> & {
+ label: string
+ language: string
+}
+
+export const locales: AppLocaleObject[] = [
+ {
+ code: 'zh-Hans',
+ name: '简体中文',
+ label: '简体中文 (Simplified Chinese)',
+ file: 'zh-Hans.json',
+ language: 'zh',
+ },
+ {
+ code: 'zh-Hant-TW',
+ name: '繁体中文(台湾)',
+ label: '繁體中文 (Traditional Chinese, Taiwan)',
+ file: 'zh-Hant-TW.json',
+ language: 'zh-TW',
+ },
+ {
+ code: 'zh-Hant-HK',
+ name: '繁体中文(香港)',
+ label: '繁體中文 (Traditional Chinese, Hong Kong)',
+ file: 'zh-Hant-HK.json',
+ language: 'zh-HK',
+ },
+ {
+ code: 'en',
+ name: 'English',
+ label: 'English',
+ file: 'en.json',
+ language: 'en',
+ },
+ {
+ code: 'ja',
+ name: '日本語',
+ label: '日本語 (Japanese)',
+ file: 'ja.json',
+ language: 'ja',
+ },
+]
+
+export const localeLanguages = locales.map(({ language }) => language)
+export const dayjsLocales: DayjsLocale[] = [
+ 'zh-cn',
+ 'zh-tw',
+ 'zh-hk',
+ 'en',
+ 'ja',
+]
+
+export default {
+ experimental: {
+ localeDetector: 'localeDetector.ts',
+ },
+ detectBrowserLanguage: {
+ fallbackLocale: defaultLocale,
+ useCookie: false,
+ cookieKey: 'chronoframe-locale',
+ },
+ strategy: 'no_prefix',
+ defaultLocale,
+ locales,
+ 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-MO': ['zh-Hant-HK'],
+ default: [defaultLocale],
+ },
+} satisfies I18nOptions & NuxtI18nOptions
diff --git a/i18n/localeDetector.ts b/i18n/localeDetector.ts
index e23297b5..9efe734a 100644
--- a/i18n/localeDetector.ts
+++ b/i18n/localeDetector.ts
@@ -1,33 +1,43 @@
+import type { Locale } from 'vue-i18n'
+import i18nOptions from './i18n.options'
+
+const SUPPORTED_LOCALES = i18nOptions.locales.map(({ code }) => code)
+
+function isSupportedLocale(localeString: string): localeString is Locale {
+ return SUPPORTED_LOCALES.some((locale) => locale === localeString)
+}
+
// 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 => {
- if (!locale) return config.defaultLocale || 'en'
+ const normalizeLocale = (locale: string): string => {
+ if (!locale) {
+ return config.defaultLocale || i18nOptions.defaultLocale
+ }
// Check if locale exists in configured locales
- if (SUPPORTED_LOCALES.includes(locale)) {
+ if (isSupportedLocale(locale)) {
return locale
}
// Try to get fallback locale from config.fallbackLocale
const fallbackLocales = config.fallbackLocale as
- | Record<string, string[]>
+ | Record<string, Locale[]>
| undefined
if (fallbackLocales && fallbackLocales[locale]) {
const fallbacks = fallbackLocales[locale]
for (const fallback of fallbacks) {
- if (SUPPORTED_LOCALES.includes(fallback)) {
+ if (isSupportedLocale(fallback)) {
return fallback
}
}
}
// Fall back to default locale
- return config.defaultLocale || 'en'
+ return config.defaultLocale || i18nOptions.defaultLocale
}
// try to get locale from query
@@ -39,7 +49,7 @@ export default defineI18nLocaleDetector((event, config) => {
// try to get locale from cookie
const cookie = tryCookieLocale(event, {
lang: '',
- name: 'chronoframe-locale',
+ name: i18nOptions.detectBrowserLanguage.cookieKey,
}) // disable locale default value with `lang` option
if (cookie) {
return normalizeLocale(cookie.toString())
diff --git a/nuxt.config.ts b/nuxt.config.ts
index f6fbcce7..cf8a2696 100644
--- a/nuxt.config.ts
+++ b/nuxt.config.ts
@@ -1,5 +1,6 @@
import pkg from './package.json'
import type { AnalyticsConfig } from './shared/types/config'
+import i18n, { dayjsLocales } from './i18n/i18n.options'
// https://nuxt.com/docs/api/configuration/nuxt-config
export default defineNuxtConfig({
@@ -248,7 +249,7 @@ export default defineNuxtConfig({
},
dayjs: {
- locales: ['zh-cn', 'zh-hk', 'en'],
+ locales: dayjsLocales,
plugins: [
'relativeTime',
'utc',
@@ -260,38 +261,5 @@ export default defineNuxtConfig({
defaultTimezone: 'Asia/Shanghai',
},
- i18n: {
- experimental: {
- localeDetector: 'localeDetector.ts',
- },
- detectBrowserLanguage: {
- fallbackLocale: 'en',
- useCookie: false,
- cookieKey: 'chronoframe-locale',
- },
- strategy: 'no_prefix',
- defaultLocale: 'en',
- locales: [
- {
- code: 'zh-Hans',
- name: '简体中文',
- file: 'zh-Hans.json',
- language: 'zh',
- },
- {
- code: 'zh-Hant-TW',
- name: '繁体中文(台湾)',
- file: 'zh-Hant-TW.json',
- language: 'zh-TW',
- },
- {
- code: 'zh-Hant-HK',
- name: '繁体中文(香港)',
- file: 'zh-Hant-HK.json',
- language: 'zh-HK',
- },
- { code: 'en', name: 'English', file: 'en.json', language: 'en' },
- { code: 'ja', name: '日本語', file: 'ja.json', language: 'ja' },
- ],
- },
+ i18n,
})
diff --git a/server/services/settings/contants.ts b/server/services/settings/contants.ts
index ba241252..bc09776a 100644
--- a/server/services/settings/contants.ts
+++ b/server/services/settings/contants.ts
@@ -1,4 +1,5 @@
import type { SettingConfig } from '~~/shared/types/settings'
+import i18nOptions from '~~/i18n/i18n.options'
// 存储提供商的枚举值
export const STORAGE_PROVIDERS = ['local', 's3', 'openlist'] as const
@@ -189,7 +190,7 @@ export const DEFAULT_SETTINGS = [
key: 'language',
type: 'string',
defaultValue: 'en',
- enum: ['en', 'zh-CN', 'zh-TW', 'ja'] as const,
+ enum: i18nOptions.locales.map(({ language }) => language),
label: 'settings.location.language.label',
description: 'settings.location.language.description',
isPublic: true,
diff --git a/server/services/settings/settingsManager.ts b/server/services/settings/settingsManager.ts
index b25abad0..e0e19f2c 100644
--- a/server/services/settings/settingsManager.ts
+++ b/server/services/settings/settingsManager.ts
@@ -161,7 +161,7 @@ export class SettingsManager {
isPublic: config.isPublic,
isReadonly: config.isReadonly,
isSecret: config.isSecret,
- enum: config.enum ? config.enum : null,
+ enum: config.enum ? [...config.enum] : null,
})
.run()
}
diff --git a/server/services/settings/ui-config.ts b/server/services/settings/ui-config.ts
index cb5c598f..f3c110f0 100644
--- a/server/services/settings/ui-config.ts
+++ b/server/services/settings/ui-config.ts
@@ -1,3 +1,4 @@
+import i18nOptions from '~~/i18n/i18n.options'
import type { FieldUIConfig } from '~~/shared/types/settings'
/**
@@ -89,12 +90,10 @@ export const MAP_SETTINGS_UI: Record<string, FieldUIConfig> = {
export const LOCATION_SETTINGS_UI: Record<string, FieldUIConfig> = {
language: {
type: 'select',
- options: [
- { label: 'English', value: 'en' },
- { label: '简体中文 (Simplified Chinese)', value: 'zh-CN' },
- { label: '繁體中文 (Traditional Chinese)', value: 'zh-TW' },
- { label: '日本語 (Japanese)', value: 'ja' },
- ],
+ options: i18nOptions.locales.map((locale) => ({
+ label: locale.label,
+ value: locale.language,
+ })),
help: 'settings.location.language.help',
},
'mapbox.token': {