Skip to content

Commit 82b4589

Browse files
authored
feat: add defaultLocale to replace default locale (#40)
1 parent 1c4e56f commit 82b4589

File tree

6 files changed

+64
-0
lines changed

6 files changed

+64
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ eg. `{ lazyComponent: true }`
5858

5959
Whether to automatically import styles.
6060

61+
### defaultLocale
62+
63+
- Type: `string`
64+
65+
Replace default locale, you can find locale list [here](https://github.com/youzan/vant/tree/main/packages/vant/src/locale/lang).
66+
6167
### excludeExports
6268

6369
- Type: `array`

playground/nuxt.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export default defineNuxtConfig({
55
modules: [Vant],
66
vant: {
77
lazyload: true,
8+
defaultLocale: 'en-US',
89
imports: ['Locale', 'useCurrentLang']
910
}
1011
})

src/core/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export * from './components'
22
export * from './imports'
33
export * from './lazyload'
4+
export * from './localePlugn'
45
export * from './options'
56
export * from './styles'
67
export * from './transformPlugin'

src/core/localePlugn.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { createUnplugin } from 'unplugin'
2+
import MagicString from 'magic-string'
3+
import type { NuxtOptions } from '@nuxt/schema'
4+
import { libraryName } from '../config'
5+
6+
interface LocalePluginOptions {
7+
sourcemap?: NuxtOptions['sourcemap']['client']
8+
locale: string
9+
}
10+
11+
export const localePlugin = createUnplugin((options: LocalePluginOptions) => {
12+
return {
13+
name: `${libraryName}:locale`,
14+
enforce: 'pre',
15+
transformInclude (id) {
16+
const regExp = new RegExp(`${libraryName}/es/locale/index`)
17+
return !!id.match(regExp)
18+
},
19+
transform (code, id) {
20+
const s = new MagicString(code)
21+
22+
s.replaceAll('zh-CN', options.locale)
23+
24+
if (s.hasChanged()) {
25+
return {
26+
code: s.toString(),
27+
map: options.sourcemap
28+
? s.generateMap({ source: id, includeContent: true })
29+
: undefined
30+
}
31+
}
32+
}
33+
}
34+
})

src/module.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
resolveLazyload,
77
resolveOptions,
88
resolveStyles,
9+
localePlugin,
910
transformPlugin
1011
} from './core/index'
1112
import type { Options } from './types'
@@ -36,6 +37,13 @@ export default defineNuxtModule<Partial<Options>>({
3637
transformStyles: name => resolveStyles(options, name)
3738
})
3839
)
40+
41+
if (options.defaultLocale && options.defaultLocale !== 'zh-CN') {
42+
config.plugins.push(localePlugin.vite({
43+
sourcemap: nuxt.options.sourcemap[mode],
44+
locale: options.defaultLocale
45+
}))
46+
}
3947
})
4048

4149
nuxt.hook('webpack:config', (configs) => {
@@ -51,6 +59,13 @@ export default defineNuxtModule<Partial<Options>>({
5159
transformStyles: name => resolveStyles(options, name)
5260
})
5361
)
62+
63+
if (options.defaultLocale && options.defaultLocale !== 'zh-CN') {
64+
config.plugins.push(localePlugin.webpack({
65+
sourcemap: nuxt.options.sourcemap[mode],
66+
locale: options.defaultLocale
67+
}))
68+
}
5469
})
5570
})
5671
}

src/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ export interface Options extends TransformOptions {
2727
* @default true
2828
*/
2929
importStyle: boolean
30+
/**
31+
* Replace default locale, you can find locale list [here](https://github.com/youzan/vant/tree/main/packages/vant/src/locale/lang).
32+
*
33+
* @default 'zh-CN'
34+
* @example 'en-US'
35+
*/
36+
defaultLocale?: string
3037
/**
3138
* Exclude exports from Vant that are not component content.
3239
*

0 commit comments

Comments
 (0)