Skip to content

Commit

Permalink
feat(projects): import i18n [引入i18n]
Browse files Browse the repository at this point in the history
  • Loading branch information
honghuangdc committed Oct 11, 2022
1 parent 1b45b71 commit b632b7f
Show file tree
Hide file tree
Showing 10 changed files with 164 additions and 4 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"ua-parser-js": "^1.0.2",
"vditor": "^3.8.17",
"vue": "3.2.40",
"vue-i18n": "^9.2.2",
"vue-router": "^4.1.5",
"vuedraggable": "^4.1.0",
"wangeditor": "^4.7.15",
Expand Down
53 changes: 53 additions & 0 deletions pnpm-lock.yaml

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

17 changes: 13 additions & 4 deletions src/layouts/common/GlobalLogo/index.vue
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
<template>
<router-link :to="routeHomePath" class="flex-center w-full nowrap-hidden">
<system-logo class="text-32px text-primary" />
<h2 v-show="showTitle" class="pl-8px text-16px font-bold text-primary transition duration-300 ease-in-out">
{{ title }}
<h2
v-show="showTitle"
class="pl-8px text-16px font-bold text-primary transition duration-300 ease-in-out"
@click="toggleLocal"
>
{{ t('message.system.title') }}
</h2>
</router-link>
</template>

<script setup lang="ts">
import { routePath } from '@/router';
import { useAppInfo } from '@/composables';
import { t, setLocale } from '@/locales';
defineOptions({ name: 'GlobalLogo' });
Expand All @@ -20,8 +24,13 @@ interface Props {
defineProps<Props>();
const { title } = useAppInfo();
const routeHomePath = routePath('root');
let flag = true;
function toggleLocal() {
flag = !flag;
setLocale(flag ? 'en' : 'zh-CN');
}
</script>

<style scoped></style>
22 changes: 22 additions & 0 deletions src/locales/i18n.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { App } from 'vue';
import { createI18n } from 'vue-i18n';
import messages from './lang';
import type { LocaleKey } from './lang';

const i18n = createI18n({
locale: 'zh-CN',
fallbackLocale: 'en',
messages
});

export function setupI18n(app: App) {
app.use(i18n);
}

export function t(key: string) {
return i18n.global.t(key);
}

export function setLocale(locale: LocaleKey) {
i18n.global.locale = locale;
}
1 change: 1 addition & 0 deletions src/locales/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './i18n';
21 changes: 21 additions & 0 deletions src/locales/lang/en.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { LocaleMessages } from 'vue-i18n';

const locale: LocaleMessages<I18nType.Schema> = {
message: {
system: {
title: 'SoybeanAdmin'
},
routes: {
dashboard: {
dashboard: 'Dashboard',
analysis: 'Analysis',
workbench: 'Workbench'
},
about: {
about: 'About'
}
}
}
};

export default locale;
11 changes: 11 additions & 0 deletions src/locales/lang/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import zhCN from './zh-cn';
import en from './en';

const locales = {
'zh-CN': zhCN,
en
};

export type LocaleKey = keyof typeof locales;

export default locales;
21 changes: 21 additions & 0 deletions src/locales/lang/zh-cn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { LocaleMessages } from 'vue-i18n';

const locale: LocaleMessages<I18nType.Schema> = {
message: {
system: {
title: 'Soybean管理系统'
},
routes: {
dashboard: {
dashboard: '仪表盘',
analysis: '分析页',
workbench: '工作台'
},
about: {
about: '关于'
}
}
}
};

export default locale;
3 changes: 3 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { setupDirectives } from './directives';
import { setupRouter } from './router';
import { setupAssets } from './plugins';
import { setupStore } from './store';
import { setupI18n } from './locales';

async function setupApp() {
// import assets: js、css
Expand All @@ -20,6 +21,8 @@ async function setupApp() {
// vue router
await setupRouter(app);

setupI18n(app);

// mount app
app.mount('#app');
}
Expand Down
18 changes: 18 additions & 0 deletions src/typings/system.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,3 +329,21 @@ declare namespace Message {
tagProps?: import('naive-ui').TagProps;
}
}

declare namespace I18nType {
interface Schema {
system: {
title: string;
};
routes: {
dashboard: {
dashboard: string;
analysis: string;
workbench: string;
};
about: {
about: string;
};
};
}
}

1 comment on commit b632b7f

@vercel
Copy link

@vercel vercel bot commented on b632b7f Oct 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.