修复组件多语言bug

This commit is contained in:
ray_wuhao 2023-03-10 16:51:24 +08:00
parent ffef1ee509
commit 94acacf9b8
5 changed files with 34 additions and 32 deletions

View File

@ -15,6 +15,10 @@
## 3.1.2 ## 3.1.2
### Fixes
- 修复 DatePicker 组件国际化部分失效问题
### Feats ### Feats
- 修改 demo 页面展示 - 修改 demo 页面展示

View File

@ -9,7 +9,7 @@ import {
NGlobalStyle, NGlobalStyle,
} from 'naive-ui' } from 'naive-ui'
import { useSetting } from '@/store' import { useSetting } from '@/store'
import { naiveLocales } from '@/language/index'
const GlobalProvider = defineComponent({ const GlobalProvider = defineComponent({
name: 'GlobalProvider', name: 'GlobalProvider',
setup() { setup() {
@ -21,7 +21,11 @@ const GlobalProvider = defineComponent({
const modelThemeValue = computed(() => const modelThemeValue = computed(() =>
settingStore.themeValue ? darkTheme : null, settingStore.themeValue ? darkTheme : null,
) )
const modelLocal = computed(() => settingStore.naiveLocal) const localePackage = computed(() => {
const key = settingStore.localeLanguage
return naiveLocales(key)
})
const { message, notification, dialog, loadingBar } = createDiscreteApi( const { message, notification, dialog, loadingBar } = createDiscreteApi(
['message', 'dialog', 'notification', 'loadingBar'], ['message', 'dialog', 'notification', 'loadingBar'],
@ -40,7 +44,7 @@ const GlobalProvider = defineComponent({
return { return {
modelPrimaryColorOverride, modelPrimaryColorOverride,
modelThemeValue, modelThemeValue,
modelLocal, localePackage,
} }
}, },
render() { render() {
@ -48,8 +52,8 @@ const GlobalProvider = defineComponent({
<NConfigProvider <NConfigProvider
themeOverrides={this.modelPrimaryColorOverride} themeOverrides={this.modelPrimaryColorOverride}
theme={this.modelThemeValue} theme={this.modelThemeValue}
locale={this.modelLocal.local} locale={this.localePackage.locale}
dateLocal={this.modelLocal.dateLocal} dateLocale={this.localePackage.dateLocal}
> >
<NLoadingBarProvider> <NLoadingBarProvider>
<NMessageProvider> <NMessageProvider>

View File

@ -10,7 +10,6 @@
*/ */
import { zhCN, dateZhCN } from 'naive-ui' // 导入 `naive ui` 中文包 import { zhCN, dateZhCN } from 'naive-ui' // 导入 `naive ui` 中文包
import { enUS, dateEnUS } from 'naive-ui' // 导入 `naive ui` 英文包
/** 语言包语种添加后, 需要在此文件配置语言包 */ /** 语言包语种添加后, 需要在此文件配置语言包 */
export const localOptions = [ export const localOptions = [
@ -30,24 +29,25 @@ export const localOptions = [
* @returns * @returns
* *
* @remark . , (https://www.naiveui.com/zh-CN/dark/docs/i18n) * @remark . , (https://www.naiveui.com/zh-CN/dark/docs/i18n)
* @remark naive ui
*/ */
export const naiveLocales = (key: string) => { export const naiveLocales = (key: string) => {
switch (key) { switch (key) {
case 'zh-CN': case 'zh-CN':
return { return {
local: zhCN, locale: zhCN,
dateLocal: dateZhCN, dateLocal: dateZhCN,
} }
case 'en-US': case 'en-US':
return { return {
local: enUS, locale: null,
dateLocal: dateEnUS, dateLocal: null,
} }
default: default:
return { return {
local: zhCN, locale: zhCN,
dateLocal: dateZhCN, dateLocal: dateZhCN,
} }
} }

View File

@ -1,4 +1,4 @@
import { naiveLocales, getDefaultNaiveLocal } from '@/language/index' import { getDefaultLocal } from '@/language/index'
import { setCache } from '@use-utils/cache' import { setCache } from '@use-utils/cache'
export const useSetting = defineStore( export const useSetting = defineStore(
@ -14,16 +14,16 @@ export const useSetting = defineStore(
themeValue: false, // `true` 为黑夜主题, `false` 为白色主题 themeValue: false, // `true` 为黑夜主题, `false` 为白色主题
reloadRouteSwitch: true, // 刷新路由开关 reloadRouteSwitch: true, // 刷新路由开关
menuTagSwitch: true, // 多标签页开关 menuTagSwitch: true, // 多标签页开关
naiveLocal: getDefaultNaiveLocal(), // `naive ui` 语言包
spinSwitch: false, // 全屏加载 spinSwitch: false, // 全屏加载
breadcrumbSwitch: true, // 面包屑开关 breadcrumbSwitch: true, // 面包屑开关
localeLanguage: getDefaultLocal(),
}) })
const { locale } = useI18n() const { locale } = useI18n()
const updateLocale = (key: string) => { const updateLocale = (key: string) => {
// TODO: 修改语言 // TODO: 修改语言
locale.value = key locale.value = key
settingState.naiveLocal = naiveLocales(key) settingState.localeLanguage = key
setCache('localeLanguage', key, 'localStorage') setCache('localeLanguage', key, 'localStorage')
} }

View File

@ -169,7 +169,17 @@ const TableView = defineComponent({
</NLi> </NLi>
<NLi> RayCollapseGird 使</NLi> <NLi> RayCollapseGird 使</NLi>
</NUl> </NUl>
<NH2> RayCollapseGird 使</NH2> <NH2> RayCollapseGird 使 RayTable </NH2>
<NP>
使 columns action
(v-model:columns)
</NP>
<NP></NP>
<NP></NP>
<NP></NP>
<NP> excel </NP>
<NP></NP>
<NP></NP>
<RayCollapseGrid <RayCollapseGrid
bordered={false} bordered={false}
collapsedRows={this.gridCollapsedRows} collapsedRows={this.gridCollapsedRows}
@ -219,26 +229,17 @@ const TableView = defineComponent({
)} )}
data={this.tableData} data={this.tableData}
columns={this.baseColumns} columns={this.baseColumns}
action={false}
pagination={{ pagination={{
pageSize: 10, pageSize: 10,
}} }}
loading={this.tableLoading} loading={this.tableLoading}
rightClickMenu={this.tableMenuOptions}
onMenuSelect={this.handleMenuSelect.bind(this)}
> >
{{ {{
tableFooter: () => '表格的底部内容区域,有时候你可能会用上', tableFooter: () => '表格的底部内容区域,有时候你可能会用上',
}} }}
</RayTable> </RayTable>
<NH2>RayTable </NH2>
<NP>
使 columns action
(v-model:columns)
</NP>
<NP></NP>
<NP></NP>
<NP></NP>
<NP> excel </NP>
<NP></NP>
<RayTable <RayTable
title={h(NInput, { title={h(NInput, {
placeholder: '请输入检索条件', placeholder: '请输入检索条件',
@ -247,13 +248,6 @@ const TableView = defineComponent({
data={this.tableData} data={this.tableData}
v-model:columns={this.actionColumns} v-model:columns={this.actionColumns}
bordered={false} bordered={false}
/>
<NH2></NH2>
<RayTable
title="右键菜单表格"
action={false}
data={this.tableData}
columns={this.baseColumns}
rightClickMenu={this.tableMenuOptions} rightClickMenu={this.tableMenuOptions}
onMenuSelect={this.handleMenuSelect.bind(this)} onMenuSelect={this.handleMenuSelect.bind(this)}
/> />