mirror of
https://github.com/fawney19/Aether.git
synced 2026-10-08 10:27:46 +08:00
refactor(frontend): modularize i18n architecture
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
import { ref } from 'vue'
|
||||
import { getI18nLocale } from '@/i18n'
|
||||
import { translateLegacyText } from '@/i18n/messages'
|
||||
|
||||
export type ConfirmVariant = 'danger' | 'destructive' | 'warning' | 'info' | 'question'
|
||||
|
||||
@@ -25,6 +27,10 @@ const state = ref<ConfirmState>({
|
||||
})
|
||||
|
||||
export function useConfirm() {
|
||||
function localizeConfirmText(value: string): string {
|
||||
return translateLegacyText(value, getI18nLocale())
|
||||
}
|
||||
|
||||
/**
|
||||
* 显示确认对话框
|
||||
* @param options 对话框选项
|
||||
@@ -34,10 +40,10 @@ export function useConfirm() {
|
||||
return new Promise((resolve) => {
|
||||
state.value = {
|
||||
isOpen: true,
|
||||
title: options.title || '确认操作',
|
||||
message: options.message,
|
||||
confirmText: options.confirmText || '确认',
|
||||
cancelText: options.cancelText || '取消',
|
||||
title: localizeConfirmText(options.title || '确认操作'),
|
||||
message: localizeConfirmText(options.message),
|
||||
confirmText: localizeConfirmText(options.confirmText || '确认'),
|
||||
cancelText: localizeConfirmText(options.cancelText || '取消'),
|
||||
variant: options.variant || 'question',
|
||||
resolve
|
||||
}
|
||||
@@ -109,4 +115,4 @@ export function useConfirm() {
|
||||
handleConfirm,
|
||||
handleCancel
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user