Skip to content

Commit

Permalink
feat: core components support simple locale switching (#4273)
Browse files Browse the repository at this point in the history
* feat: core components support simple locale switching

* fix: test error

* fix: test error
  • Loading branch information
anncwb committed Aug 29, 2024
1 parent 98da067 commit a77cc00
Show file tree
Hide file tree
Showing 18 changed files with 78 additions and 32 deletions.
2 changes: 0 additions & 2 deletions docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@
"@vben-core/shadcn-ui": "workspace:*",
"@vben/common-ui": "workspace:*",
"@vben/styles": "workspace:*",
"@vueuse/core": "^11.0.3",
"lucide-vue-next": "^0.436.0",
"markdown-it": "^14.1.0",
"medium-zoom": "^1.1.0",
"radix-vue": "^1.9.5"
},
"devDependencies": {
"@nolebase/vitepress-plugin-git-changelog": "^2.4.0",
"@types/markdown-it": "^14.1.2",
"@vben/vite-config": "workspace:*",
"@vite-pwa/vitepress": "^0.5.0",
"vitepress": "^1.3.4",
Expand Down
4 changes: 2 additions & 2 deletions docs/src/en/guide/in-depth/theme.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ You can check the list below to understand all the available variables.

/* Theme Colors */

--primary: 211 91% 39%;
--primary: 231 98% 65%;
--primary-foreground: 0 0% 98%;

/* Used for destructive actions such as <Button variant="destructive"> */
Expand Down Expand Up @@ -351,7 +351,7 @@ type BuiltinThemeType =

/* Theme Colors */

--primary: 211 91% 39%;
--primary: 231 98% 65%;
--primary-foreground: 0 0% 98%;

/* Used for destructive actions such as <Button variant="destructive"> */
Expand Down
4 changes: 2 additions & 2 deletions docs/src/guide/in-depth/theme.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ css 变量内的颜色,必须使用 `hsl` 格式,如 `0 0% 100%`,不需要

/* 主题颜色 */

--primary: 211 91% 39%;
--primary: 231 98% 65%;
--primary-foreground: 0 0% 98%;

/* Used for destructive actions such as <Button variant="destructive"> */
Expand Down Expand Up @@ -351,7 +351,7 @@ type BuiltinThemeType =

/* 主题颜色 */

--primary: 211 91% 39%;
--primary: 231 98% 65%;
--primary-foreground: 0 0% 98%;

/* Used for destructive actions such as <Button variant="destructive"> */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"> */
Expand Down
1 change: 1 addition & 0 deletions packages/@core/composables/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions packages/@core/composables/src/use-simple-locale/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Simple i18n

Simple i18 implementation
25 changes: 25 additions & 0 deletions packages/@core/composables/src/use-simple-locale/index.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 14 additions & 0 deletions packages/@core/composables/src/use-simple-locale/messages.ts
Original file line number Diff line number Diff line change
@@ -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];
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
2 changes: 0 additions & 2 deletions packages/@core/ui-kit/popup-ui/src/drawer/drawer-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 8 additions & 4 deletions packages/@core/ui-kit/popup-ui/src/drawer/drawer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -34,7 +38,7 @@ const props = withDefaults(defineProps<Props>(), {
});
const wrapperRef = ref<HTMLElement>();
const { $t } = useSimpleLocale();
const { isMobile } = useIsMobile();
const state = props.drawerApi?.useStore?.();
Expand Down Expand Up @@ -165,15 +169,15 @@ function pointerDownOutside(e: Event) {
<slot name="footer">
<VbenButton variant="ghost" @click="() => drawerApi?.onCancel()">
<slot name="cancelText">
{{ cancelText }}
{{ cancelText || $t('cancel') }}
</slot>
</VbenButton>
<VbenButton
:loading="confirmLoading"
@click="() => drawerApi?.onConfirm()"
>
<slot name="confirmText">
{{ confirmText }}
{{ confirmText || $t('confirm') }}
</slot>
</VbenButton>
</slot>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
2 changes: 0 additions & 2 deletions packages/@core/ui-kit/popup-ui/src/modal/modal-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
11 changes: 8 additions & 3 deletions packages/@core/ui-kit/popup-ui/src/modal/modal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -44,6 +48,7 @@ const dialogRef = ref();
const headerRef = ref();
const footerRef = ref();
const { $t } = useSimpleLocale();
const { isMobile } = useIsMobile();
const state = props.modalApi?.useStore?.();
Expand Down Expand Up @@ -235,15 +240,15 @@ function pointerDownOutside(e: Event) {
<slot name="footer">
<VbenButton variant="ghost" @click="() => modalApi?.onCancel()">
<slot name="cancelText">
{{ cancelText }}
{{ cancelText || $t('cancel') }}
</slot>
</VbenButton>
<VbenButton
:loading="confirmLoading"
@click="() => modalApi?.onConfirm()"
>
<slot name="confirmText">
{{ confirmText }}
{{ confirmText || $t('confirm') }}
</slot>
</VbenButton>
</slot>
Expand Down
5 changes: 1 addition & 4 deletions packages/@core/ui-kit/shadcn-ui/src/components/logo/logo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
1 change: 1 addition & 0 deletions packages/locales/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
},
"dependencies": {
"@intlify/core-base": "^9.14.0",
"@vben-core/composables": "workspace:*",
"vue": "^3.4.38",
"vue-i18n": "^9.14.0"
}
Expand Down
5 changes: 5 additions & 0 deletions packages/locales/src/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import type {
import { type App, unref } from 'vue';
import { createI18n } from 'vue-i18n';

import { useSimpleLocale } from '@vben-core/composables';

const i18n = createI18n({
globalInjection: true,
legacy: false,
Expand All @@ -19,6 +21,8 @@ const i18n = createI18n({

const modules = import.meta.glob('./langs/*.json');

const { setSimpleLocale } = useSimpleLocale();

const localesMap = loadLocalesMap(modules);

let loadMessages: LoadMessageFn;
Expand Down Expand Up @@ -75,6 +79,7 @@ async function loadLocaleMessages(lang: SupportedLanguagesType) {
if (unref(i18n.global.locale) === lang) {
return setI18nLanguage(lang);
}
setSimpleLocale(lang);

const message = await localesMap[lang]?.();

Expand Down
9 changes: 3 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a77cc00

Please sign in to comment.