feat: core components support simple locale switching (#4273)
* feat: core components support simple locale switching * fix: test error * fix: test error
This commit is contained in:
@@ -28,7 +28,7 @@
|
||||
|
||||
/* 主题颜色 */
|
||||
|
||||
--primary: 211 91% 39%;
|
||||
--primary: 231 98% 65%;
|
||||
--primary-foreground: 0 0% 98%;
|
||||
|
||||
/* Used for destructive actions such as <Button variant="destructive"> */
|
||||
|
@@ -2,6 +2,7 @@ export * from './use-content-style';
|
||||
export * from './use-is-mobile';
|
||||
export * from './use-namespace';
|
||||
export * from './use-priority-value';
|
||||
export * from './use-simple-locale';
|
||||
export * from './use-sortable';
|
||||
export {
|
||||
useEmitAsProps,
|
||||
|
@@ -0,0 +1,3 @@
|
||||
# Simple i18n
|
||||
|
||||
Simple i18 implementation
|
25
packages/@core/composables/src/use-simple-locale/index.ts
Normal file
25
packages/@core/composables/src/use-simple-locale/index.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { computed, ref } from 'vue';
|
||||
|
||||
import { createSharedComposable } from '@vueuse/core';
|
||||
|
||||
import { getMessages, type Locale } from './messages';
|
||||
|
||||
export const useSimpleLocale = createSharedComposable(() => {
|
||||
const currentLocale = ref<Locale>('zh-CN');
|
||||
|
||||
const setSimpleLocale = (locale: Locale) => {
|
||||
currentLocale.value = locale;
|
||||
};
|
||||
|
||||
const $t = computed(() => {
|
||||
const localeMessages = getMessages(currentLocale.value);
|
||||
return (key: string) => {
|
||||
return localeMessages[key] || key;
|
||||
};
|
||||
});
|
||||
return {
|
||||
$t,
|
||||
currentLocale,
|
||||
setSimpleLocale,
|
||||
};
|
||||
});
|
14
packages/@core/composables/src/use-simple-locale/messages.ts
Normal file
14
packages/@core/composables/src/use-simple-locale/messages.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
export type Locale = 'en-US' | 'zh-CN';
|
||||
|
||||
export const messages: Record<Locale, Record<string, string>> = {
|
||||
'en-US': {
|
||||
cancel: 'Cancel',
|
||||
confirm: 'Confirm',
|
||||
},
|
||||
'zh-CN': {
|
||||
cancel: '取消',
|
||||
confirm: '确认',
|
||||
},
|
||||
};
|
||||
|
||||
export const getMessages = (locale: Locale) => messages[locale];
|
@@ -44,8 +44,8 @@ describe('drawerApi', () => {
|
||||
|
||||
it('should initialize with default state', () => {
|
||||
expect(drawerState.isOpen).toBe(false);
|
||||
expect(drawerState.cancelText).toBe('取消');
|
||||
expect(drawerState.confirmText).toBe('确定');
|
||||
expect(drawerState.cancelText).toBe(undefined);
|
||||
expect(drawerState.confirmText).toBe(undefined);
|
||||
});
|
||||
|
||||
it('should open the drawer', () => {
|
||||
|
@@ -28,12 +28,10 @@ export class DrawerApi {
|
||||
} = options;
|
||||
|
||||
const defaultState: DrawerState = {
|
||||
cancelText: '取消',
|
||||
closable: true,
|
||||
closeOnClickModal: true,
|
||||
closeOnPressEscape: true,
|
||||
confirmLoading: false,
|
||||
confirmText: '确定',
|
||||
footer: true,
|
||||
isOpen: false,
|
||||
loading: false,
|
||||
|
@@ -3,7 +3,11 @@ import type { DrawerProps, ExtendedDrawerApi } from './drawer';
|
||||
|
||||
import { ref, watch } from 'vue';
|
||||
|
||||
import { useIsMobile, usePriorityValue } from '@vben-core/composables';
|
||||
import {
|
||||
useIsMobile,
|
||||
usePriorityValue,
|
||||
useSimpleLocale,
|
||||
} from '@vben-core/composables';
|
||||
import { Info, X } from '@vben-core/icons';
|
||||
import {
|
||||
Sheet,
|
||||
@@ -34,7 +38,7 @@ const props = withDefaults(defineProps<Props>(), {
|
||||
});
|
||||
|
||||
const wrapperRef = ref<HTMLElement>();
|
||||
|
||||
const { $t } = useSimpleLocale();
|
||||
const { isMobile } = useIsMobile();
|
||||
const state = props.drawerApi?.useStore?.();
|
||||
|
||||
@@ -165,7 +169,7 @@ function pointerDownOutside(e: Event) {
|
||||
<slot name="footer">
|
||||
<VbenButton variant="ghost" @click="() => drawerApi?.onCancel()">
|
||||
<slot name="cancelText">
|
||||
{{ cancelText }}
|
||||
{{ cancelText || $t('cancel') }}
|
||||
</slot>
|
||||
</VbenButton>
|
||||
<VbenButton
|
||||
@@ -173,7 +177,7 @@ function pointerDownOutside(e: Event) {
|
||||
@click="() => drawerApi?.onConfirm()"
|
||||
>
|
||||
<slot name="confirmText">
|
||||
{{ confirmText }}
|
||||
{{ confirmText || $t('confirm') }}
|
||||
</slot>
|
||||
</VbenButton>
|
||||
</slot>
|
||||
|
@@ -44,8 +44,8 @@ describe('modalApi', () => {
|
||||
|
||||
it('should initialize with default state', () => {
|
||||
expect(modalState.isOpen).toBe(false);
|
||||
expect(modalState.cancelText).toBe('取消');
|
||||
expect(modalState.confirmText).toBe('确定');
|
||||
expect(modalState.cancelText).toBe(undefined);
|
||||
expect(modalState.confirmText).toBe(undefined);
|
||||
});
|
||||
|
||||
it('should open the modal', () => {
|
||||
|
@@ -28,12 +28,10 @@ export class ModalApi {
|
||||
} = options;
|
||||
|
||||
const defaultState: ModalState = {
|
||||
cancelText: '取消',
|
||||
centered: false,
|
||||
closeOnClickModal: true,
|
||||
closeOnPressEscape: true,
|
||||
confirmLoading: false,
|
||||
confirmText: '确定',
|
||||
draggable: false,
|
||||
footer: true,
|
||||
fullscreen: false,
|
||||
|
@@ -3,7 +3,11 @@ import type { ExtendedModalApi, ModalProps } from './modal';
|
||||
|
||||
import { computed, nextTick, ref, watch } from 'vue';
|
||||
|
||||
import { useIsMobile, usePriorityValue } from '@vben-core/composables';
|
||||
import {
|
||||
useIsMobile,
|
||||
usePriorityValue,
|
||||
useSimpleLocale,
|
||||
} from '@vben-core/composables';
|
||||
import { Expand, Info, Shrink } from '@vben-core/icons';
|
||||
import {
|
||||
Dialog,
|
||||
@@ -44,6 +48,7 @@ const dialogRef = ref();
|
||||
const headerRef = ref();
|
||||
const footerRef = ref();
|
||||
|
||||
const { $t } = useSimpleLocale();
|
||||
const { isMobile } = useIsMobile();
|
||||
const state = props.modalApi?.useStore?.();
|
||||
|
||||
@@ -235,7 +240,7 @@ function pointerDownOutside(e: Event) {
|
||||
<slot name="footer">
|
||||
<VbenButton variant="ghost" @click="() => modalApi?.onCancel()">
|
||||
<slot name="cancelText">
|
||||
{{ cancelText }}
|
||||
{{ cancelText || $t('cancel') }}
|
||||
</slot>
|
||||
</VbenButton>
|
||||
<VbenButton
|
||||
@@ -243,7 +248,7 @@ function pointerDownOutside(e: Event) {
|
||||
@click="() => modalApi?.onConfirm()"
|
||||
>
|
||||
<slot name="confirmText">
|
||||
{{ confirmText }}
|
||||
{{ confirmText || $t('confirm') }}
|
||||
</slot>
|
||||
</VbenButton>
|
||||
</slot>
|
||||
|
@@ -53,10 +53,7 @@ withDefaults(defineProps<Props>(), {
|
||||
:width="logoSize"
|
||||
class="relative rounded-none bg-transparent"
|
||||
/>
|
||||
<span
|
||||
v-if="!collapsed"
|
||||
class="text-primary dark:text-foreground truncate text-nowrap"
|
||||
>
|
||||
<span v-if="!collapsed" class="text-foreground truncate text-nowrap">
|
||||
{{ text }}
|
||||
</span>
|
||||
</a>
|
||||
|
Reference in New Issue
Block a user