mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-02 05:54:44 +00:00
feat(webui): add i18n support using paraglide-js
This commit is contained in:
@@ -16,6 +16,9 @@ import {
|
||||
useRouterState,
|
||||
} from '@tanstack/react-router'
|
||||
import { useEffect } from 'react'
|
||||
import { getLocale, setLocale } from '@/ui/src/paraglide/runtime.js'
|
||||
import { m } from '@/ui/src/paraglide/messages.js'
|
||||
import { useUserMetadataStore } from '@/ui/stores/user-metadata'
|
||||
|
||||
function RootComponent() {
|
||||
const user = useAuthStore((state) => state.user)
|
||||
@@ -75,7 +78,7 @@ function RootComponent() {
|
||||
<DualSidebar>
|
||||
<DualSidebarItem
|
||||
icon={<HomeIcon />}
|
||||
label="Dashboard"
|
||||
label={m.dashboard()}
|
||||
onItemClick={() => {
|
||||
if (storedTeamId) {
|
||||
navigate({
|
||||
@@ -85,7 +88,7 @@ function RootComponent() {
|
||||
}
|
||||
}}
|
||||
/>
|
||||
<DualSidebarItem icon={<NotificationFillIcon />} label="Notifications" badge={badge}>
|
||||
<DualSidebarItem icon={<NotificationFillIcon />} label={m.notifications()} badge={badge}>
|
||||
<NotificationList />
|
||||
</DualSidebarItem>
|
||||
<DualSidebarItem
|
||||
@@ -95,7 +98,7 @@ function RootComponent() {
|
||||
className={uploading > 0 ? 'text-blue-500' : ''}
|
||||
/>
|
||||
}
|
||||
label="Uploads"
|
||||
label={m.uploads()}
|
||||
>
|
||||
<UploadTasks />
|
||||
</DualSidebarItem>
|
||||
@@ -117,5 +120,27 @@ interface MyRouterContext {
|
||||
}
|
||||
|
||||
export const Route = createRootRouteWithContext<MyRouterContext>()({
|
||||
beforeLoad: async () => {
|
||||
if (typeof window !== 'undefined') {
|
||||
const pathname = window.location.pathname
|
||||
const teamIdMatch = pathname.match(/^\/teams\/([^/]+)/)
|
||||
const teamId = teamIdMatch ? teamIdMatch[1] : null
|
||||
|
||||
if (teamId) {
|
||||
const { fetchMetadata, getMetadata } = useUserMetadataStore.getState()
|
||||
try {
|
||||
await fetchMetadata(teamId)
|
||||
const savedLocale = getMetadata<string>('locale')
|
||||
if ((savedLocale === 'en' || savedLocale === 'zh') && savedLocale !== getLocale()) {
|
||||
setLocale(savedLocale, { reload: false })
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('Failed to load user locale metadata:', e)
|
||||
}
|
||||
}
|
||||
|
||||
document.documentElement.setAttribute('lang', getLocale())
|
||||
}
|
||||
},
|
||||
component: RootComponent,
|
||||
})
|
||||
|
||||
@@ -17,6 +17,16 @@ import { Input } from '@/ui/components/ui/input'
|
||||
import { Button } from '@/ui/components/ui/button'
|
||||
import { AvatarCropDialog } from '@/ui/components/settings/AvatarCropDialog'
|
||||
import { toast } from 'sonner'
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectItem,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
} from '@/ui/components/ui/select'
|
||||
import { useUserMetadataStore } from '@/ui/stores/user-metadata'
|
||||
import { getLocale, setLocale } from '@/ui/src/paraglide/runtime.js'
|
||||
import { m } from '@/ui/src/paraglide/messages.js'
|
||||
|
||||
type SettingsTab =
|
||||
| 'general'
|
||||
@@ -38,6 +48,22 @@ function TeamSettingsPage() {
|
||||
const fileInputRef = useRef<HTMLInputElement>(null)
|
||||
const [isUpdatingProfile, setIsUpdatingProfile] = useState(false)
|
||||
|
||||
const { setMetadata, getMetadata } = useUserMetadataStore()
|
||||
const currentLocale = getMetadata<string>('locale') || getLocale()
|
||||
|
||||
const handleLanguageChange = async (newLocale: string) => {
|
||||
if (newLocale === 'en' || newLocale === 'zh') {
|
||||
try {
|
||||
await setMetadata(teamId, 'locale', newLocale)
|
||||
setLocale(newLocale)
|
||||
toast.success('Language updated successfully')
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
toast.error('Failed to update language')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const {
|
||||
data: settings,
|
||||
isLoading: isSettingsLoading,
|
||||
@@ -492,6 +518,26 @@ function TeamSettingsPage() {
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<Card className="border border-slate-200 dark:border-slate-800 mt-6">
|
||||
<CardHeader>
|
||||
<CardTitle>{m.language_settings()}</CardTitle>
|
||||
<CardDescription>{m.select_preferred_language()}</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-4">
|
||||
<div className="max-w-xs">
|
||||
<Select value={currentLocale} onValueChange={handleLanguageChange}>
|
||||
<SelectTrigger>
|
||||
<SelectValue placeholder="Select Language" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="en">English</SelectItem>
|
||||
<SelectItem value="zh">简体中文 (Chinese)</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
<AvatarCropDialog
|
||||
isOpen={isCropOpen}
|
||||
onClose={() => setIsCropOpen(false)}
|
||||
|
||||
Reference in New Issue
Block a user