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.

feat(locale): add locale switch for admin dashboard

Ahmet Ömerb9bbfb8015e1
diff --git a/app/app.vue b/app/app.vue
index c6967553..a49e4803 100644
--- a/app/app.vue
+++ b/app/app.vue
@@ -29,6 +29,7 @@ const appTitle = useSettingRef('app:title')
colorMode.preference = useSettingRef('app:appearance.theme').value as string
useHead({
+ htmlAttrs: { lang: localeRef },
titleTemplate: (title) =>
`${title ? title + ' | ' : ''}${appTitle.value || 'ChronoFrame'}`,
})
diff --git a/app/components/masonry/Root.vue b/app/components/masonry/Root.vue
index 8bcfeb4b..3529eb36 100644
--- a/app/components/masonry/Root.vue
+++ b/app/components/masonry/Root.vue
@@ -153,8 +153,9 @@ const photoStats = computed(() => {
const allDates = displayPhotos.value
?.map((p) => p?.dateTaken)
.filter((date): date is string => Boolean(date))
- .map((date) => dayjs(date).format('ll'))
- .sort((a, b) => (dayjs(a).isBefore(dayjs(b)) ? 1 : -1))
+ .map((date) => dayjs(date))
+ .sort((a, b) => b.valueOf() - a.valueOf())
+ .map((date) => date.format('ll'))
const dateRange =
allDates.length > 0
diff --git a/app/composables/useLocalePreference.ts b/app/composables/useLocalePreference.ts
new file mode 100644
index 00000000..76f61ebd
--- /dev/null
+++ b/app/composables/useLocalePreference.ts
@@ -0,0 +1,65 @@
+import type { DropdownMenuItem } from '@nuxt/ui'
+import i18nOptions, {
+ defaultLocale,
+ isAppLocaleCode,
+ locales,
+ type AppLocaleCode,
+} from '~~/i18n/i18n.options'
+
+export type LocalePreference = AppLocaleCode | 'auto'
+
+const ONE_YEAR_IN_SECONDS = 60 * 60 * 24 * 365
+
+/**
+ * The UI language preference, picked from the dashboard account menu.
+ * It only affects the current browser, never the instance settings.
+ *
+ * An explicit choice is stored in the `chronoframe-locale` cookie, which the
+ * i18n detection reads first on every request. `auto` clears the cookie.
+ */
+export function useLocalePreference() {
+ const { $i18n } = useNuxtApp()
+
+ const cookie = useCookie<string | null>(
+ i18nOptions.detectBrowserLanguage.cookieKey,
+ { maxAge: ONE_YEAR_IN_SECONDS, sameSite: 'lax', path: '/' },
+ )
+
+ const preference = useState<LocalePreference>('locale-preference', () =>
+ isAppLocaleCode(cookie.value) ? cookie.value : 'auto',
+ )
+
+ const resolveLocale = (value: LocalePreference): AppLocaleCode => {
+ if (value !== 'auto') {
+ return value
+ }
+ const browserLocale = $i18n.getBrowserLocale()
+ return isAppLocaleCode(browserLocale) ? browserLocale : defaultLocale
+ }
+
+ const setPreference = async (value: LocalePreference) => {
+ preference.value = value
+ cookie.value = value === 'auto' ? null : value
+ await $i18n.setLocale(resolveLocale(value))
+ }
+
+ const menuItems = computed<DropdownMenuItem[][]>(() => [
+ [
+ {
+ label: $i18n.t('ui.locale.auto'),
+ icon: 'tabler:world',
+ type: 'checkbox',
+ checked: preference.value === 'auto',
+ onSelect: () => setPreference('auto'),
+ },
+ ],
+ locales.map(({ code, label }) => ({
+ label,
+ type: 'checkbox',
+ checked: preference.value === code,
+ onSelect: () => setPreference(code),
+ })),
+ ])
+
+ return { menuItems }
+}
diff --git a/app/layouts/dashboard.vue b/app/layouts/dashboard.vue
index b17a0766..fc59b085 100644
--- a/app/layouts/dashboard.vue
+++ b/app/layouts/dashboard.vue
@@ -1,10 +1,11 @@
<script lang="ts" setup>
-import type { NavigationMenuItem } from '@nuxt/ui'
+import type { DropdownMenuItem, NavigationMenuItem } from '@nuxt/ui'
const route = useRoute()
const router = useRouter()
-const { loggedIn, user } = useUserSession()
+const { loggedIn, user, clear: clearUserSession } = useUserSession()
const settingsStore = useSettingsStore()
+const { menuItems: localeMenuItems } = useLocalePreference()
const appTitle = computed(() => {
const value = settingsStore.getSetting('app:title')
@@ -98,6 +99,36 @@ const navItems = computed<NavigationMenuItem[][]>(() => [
],
])
+const handleLogout = async () => {
+ await clearUserSession()
+ await navigateTo('/')
+}
+
+const accountMenuItems = computed<DropdownMenuItem[][]>(() => [
+ [
+ {
+ label: $t('ui.locale.label'),
+ icon: 'tabler:language',
+ children: localeMenuItems.value,
+ },
+ ],
+ [
+ {
+ label: $t('dashboard.nav.viewGallery'),
+ icon: 'tabler:photo',
+ to: '/',
+ },
+ ],
+ [
+ {
+ label: $t('ui.action.logout.tooltip'),
+ icon: 'tabler:logout',
+ color: 'error',
+ onSelect: handleLogout,
+ },
+ ],
+])
+
useHead({
title: () => $t('title.dashboard'),
titleTemplate: (title) => `${title ? `${title} | ` : ''}${appTitle.value}`,
@@ -189,19 +220,31 @@ const handleLogin = () => {
</template>
<template #footer="{ collapsed }">
- <UButton
- :avatar="{
- src: user?.avatar || '',
- alt: user?.username || user?.email || 'User Avatar',
- icon: 'tabler:user',
+ <UDropdownMenu
+ :items="accountMenuItems"
+ :content="{ align: 'center', collisionPadding: 12 }"
+ :ui="{
+ content: collapsed
+ ? 'min-w-48'
+ : 'min-w-(--reka-dropdown-menu-trigger-width)',
}"
- :label="collapsed ? undefined : user?.username || 'User'"
- size="lg"
- color="neutral"
- variant="ghost"
- class="w-full"
- :block="collapsed"
- />
+ >
+ <UButton
+ :avatar="{
+ src: user?.avatar || '',
+ alt: user?.username || user?.email || 'User Avatar',
+ icon: 'tabler:user',
+ }"
+ :label="collapsed ? undefined : user?.username || 'User'"
+ :trailing-icon="collapsed ? undefined : 'tabler:selector'"
+ size="lg"
+ color="neutral"
+ variant="ghost"
+ class="w-full data-[state=open]:bg-elevated"
+ :block="collapsed"
+ :ui="{ trailingIcon: 'text-dimmed ms-auto' }"
+ />
+ </UDropdownMenu>
</template>
</UDashboardSidebar>
diff --git a/i18n/i18n.options.ts b/i18n/i18n.options.ts
index db0434d1..4ca967fb 100644
--- a/i18n/i18n.options.ts
+++ b/i18n/i18n.options.ts
@@ -7,7 +7,7 @@ export const defaultLocale = 'en'
type DayjsLocale = NonNullable<DayjsModuleOptions['locales']>[number]
-type AppLocaleCode =
+export type AppLocaleCode =
| 'zh-Hans'
| 'zh-Hant-TW'
| 'zh-Hant-HK'
@@ -65,7 +65,12 @@ export const locales: AppLocaleObject[] = [
},
]
+export const localeCodes = locales.map(({ code }) => code)
export const localeLanguages = locales.map(({ language }) => language)
+
+export function isAppLocaleCode(value: unknown): value is AppLocaleCode {
+ return localeCodes.some((code) => code === value)
+}
export const dayjsLocales: DayjsLocale[] = [
'zh-cn',
'zh-tw',
diff --git a/i18n/locales/en.json b/i18n/locales/en.json
index a63273f9..266e79bc 100644
--- a/i18n/locales/en.json
+++ b/i18n/locales/en.json
@@ -3,6 +3,10 @@
"photo": "no photos | one photo | {count} photos"
},
"ui": {
+ "locale": {
+ "label": "Language",
+ "auto": "Auto (browser language)"
+ },
"action": {
"filter": {
"tooltip": "Filter Photos",
@@ -1094,7 +1098,8 @@
"noAccess": "Sorry, you do not have access to this page."
},
"nav": {
- "documentation": "Documentation"
+ "documentation": "Documentation",
+ "viewGallery": "View Gallery"
}
},
"settings": {
diff --git a/i18n/locales/ja.json b/i18n/locales/ja.json
index d2888f78..cf75292c 100644
--- a/i18n/locales/ja.json
+++ b/i18n/locales/ja.json
@@ -3,6 +3,10 @@
"photo": "写真がありません | 写真 {count} 枚"
},
"ui": {
+ "locale": {
+ "label": "言語",
+ "auto": "自動(ブラウザの言語)"
+ },
"action": {
"filter": {
"tooltip": "写真をフィルター",
@@ -1020,7 +1024,8 @@
"noAccess": "申し訳ありませんが、このページにアクセスする権限がありません。"
},
"nav": {
- "documentation": "ドキュメント"
+ "documentation": "ドキュメント",
+ "viewGallery": "ギャラリーを表示"
}
},
"settings": {
diff --git a/i18n/locales/ru.json b/i18n/locales/ru.json
index 90267bad..74f1aa2e 100644
--- a/i18n/locales/ru.json
+++ b/i18n/locales/ru.json
@@ -3,6 +3,10 @@
"photo": "нет фото | одно фото | {count} фото"
},
"ui": {
+ "locale": {
+ "label": "Язык",
+ "auto": "Авто (язык браузера)"
+ },
"action": {
"filter": {
"tooltip": "Фильтр фотографий",
@@ -1093,7 +1097,8 @@
"noAccess": "У вас нет доступа к этой странице."
},
"nav": {
- "documentation": "Документация"
+ "documentation": "Документация",
+ "viewGallery": "Открыть галерею"
}
},
"settings": {
diff --git a/i18n/locales/zh-Hans.json b/i18n/locales/zh-Hans.json
index e8c4ee47..acfe0fae 100644
--- a/i18n/locales/zh-Hans.json
+++ b/i18n/locales/zh-Hans.json
@@ -17,6 +17,10 @@
}
},
"ui": {
+ "locale": {
+ "label": "语言",
+ "auto": "自动(跟随浏览器)"
+ },
"livePhoto": "实况",
"action": {
"dashboard": {
@@ -1094,7 +1098,8 @@
"noAccess": "抱歉,您没有访问此页面的权限。"
},
"nav": {
- "documentation": "文档"
+ "documentation": "文档",
+ "viewGallery": "查看画廊"
}
},
"settings": {
diff --git a/i18n/locales/zh-Hant-HK.json b/i18n/locales/zh-Hant-HK.json
index a76d951e..66bcc0c3 100644
--- a/i18n/locales/zh-Hant-HK.json
+++ b/i18n/locales/zh-Hant-HK.json
@@ -17,6 +17,10 @@
}
},
"ui": {
+ "locale": {
+ "label": "語言",
+ "auto": "自動(跟隨瀏覽器)"
+ },
"livePhoto": "原況",
"action": {
"dashboard": {
@@ -1020,7 +1024,8 @@
"noAccess": "抱歉,您沒有存取此頁面的權限。"
},
"nav": {
- "documentation": "文件"
+ "documentation": "文件",
+ "viewGallery": "查看畫廊"
}
},
"settings": {
diff --git a/i18n/locales/zh-Hant-TW.json b/i18n/locales/zh-Hant-TW.json
index 5d9304bd..c59633a7 100644
--- a/i18n/locales/zh-Hant-TW.json
+++ b/i18n/locales/zh-Hant-TW.json
@@ -17,6 +17,10 @@
}
},
"ui": {
+ "locale": {
+ "label": "語言",
+ "auto": "自動(跟隨瀏覽器)"
+ },
"livePhoto": "原況",
"action": {
"dashboard": {
@@ -1020,7 +1024,8 @@
"noAccess": "抱歉,您沒有存取此頁面的權限。"
},
"nav": {
- "documentation": "文件"
+ "documentation": "文件",
+ "viewGallery": "檢視畫廊"
}
},
"settings": {