feat(webui): add i18n support using paraglide-js

This commit is contained in:
yiling
2026-06-26 10:39:32 +08:00
parent 81b0083672
commit 48f4c83cc8
10 changed files with 179 additions and 24 deletions
+11
View File
@@ -0,0 +1,11 @@
{
"$schema": "https://inlang.com/schema/inlang-message-format",
"dashboard": "Dashboard",
"notifications": "Notifications",
"uploads": "Uploads",
"language": "Language",
"personal_info": "Personal Info",
"language_settings": "Language Settings",
"select_preferred_language": "Select your preferred display language.",
"settings": "Settings"
}
+11
View File
@@ -0,0 +1,11 @@
{
"$schema": "https://inlang.com/schema/inlang-message-format",
"dashboard": "控制台",
"notifications": "通知中心",
"uploads": "上传管理",
"language": "语言",
"personal_info": "个人信息",
"language_settings": "语言设置",
"select_preferred_language": "选择您首选的显示语言。",
"settings": "设置"
}
+6
View File
@@ -85,7 +85,13 @@
"@shumai/dtos": "workspace:*",
"ulid": "3.0.2"
},
"scripts": {
"i18n:compile": "paraglide-js compile --project ./project.inlang --outdir ./src/paraglide --emit-ts-declarations",
"i18n:watch": "paraglide-js compile --project ./project.inlang --outdir ./src/paraglide --watch --emit-ts-declarations"
},
"devDependencies": {
"typescript": "6.0.3",
"@inlang/paraglide-js": "^2.20.0",
"@tailwindcss/postcss": "4.3.0",
"@tailwindcss/vite": "4.3.0",
"@tanstack/router-cli": "1.167.13",
@@ -0,0 +1,12 @@
{
"$schema": "https://inlang.com/schema/project-settings",
"baseLocale": "en",
"locales": ["en", "zh"],
"modules": [
"https://cdn.jsdelivr.net/npm/@inlang/plugin-message-format@4/dist/index.js",
"https://cdn.jsdelivr.net/npm/@inlang/plugin-m-function-matcher@2/dist/index.js"
],
"plugin.inlang.messageFormat": {
"pathPattern": "./messages/{locale}.json"
}
}
+28 -3
View File
@@ -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)}