refactor(frontend): modularize i18n architecture

This commit is contained in:
elky
2026-06-30 17:01:39 +08:00
parent bf29129a4b
commit 9f138d09e6
118 changed files with 11940 additions and 5368 deletions
+23 -4
View File
@@ -1,5 +1,7 @@
import { ref } from 'vue'
import { TOAST_CONFIG } from '@/config/constants'
import { getI18nLocale } from '@/i18n'
import { translateLegacyText } from '@/i18n/messages'
export type ToastVariant = 'success' | 'error' | 'warning' | 'info'
@@ -11,15 +13,31 @@ export interface Toast {
duration?: number
}
export type ToastOptions = Omit<Toast, 'id' | 'variant'> & {
description?: string
variant?: ToastVariant | 'destructive'
}
const toasts = ref<Toast[]>([])
export function useToast() {
function showToast(options: Omit<Toast, 'id'>) {
function localizeToastText(value: string | undefined): string | undefined {
return value === undefined ? undefined : translateLegacyText(value, getI18nLocale())
}
function normalizeToastVariant(variant: ToastOptions['variant']): ToastVariant {
return variant === 'destructive' ? 'error' : variant || 'info'
}
function showToast(options: ToastOptions) {
const { description, ...toastOptions } = options
const toast: Toast = {
id: Date.now().toString(),
variant: 'info',
duration: 5000,
...options
...toastOptions,
variant: normalizeToastVariant(options.variant),
title: localizeToastText(options.title),
message: localizeToastText(options.message ?? description),
}
@@ -66,10 +84,11 @@ export function useToast() {
toasts,
showToast,
removeToast,
toast: showToast,
success,
error,
warning,
info,
clearAll
}
}
}