Skip to content

Commit

Permalink
feat: add defaultLocale to replace default locale (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
tolking authored Aug 25, 2024
1 parent 1c4e56f commit 82b4589
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ eg. `{ lazyComponent: true }`

Whether to automatically import styles.

### defaultLocale

- Type: `string`

Replace default locale, you can find locale list [here](https://github.com/youzan/vant/tree/main/packages/vant/src/locale/lang).

### excludeExports

- Type: `array`
Expand Down
1 change: 1 addition & 0 deletions playground/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export default defineNuxtConfig({
modules: [Vant],
vant: {
lazyload: true,
defaultLocale: 'en-US',
imports: ['Locale', 'useCurrentLang']
}
})
1 change: 1 addition & 0 deletions src/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from './components'
export * from './imports'
export * from './lazyload'
export * from './localePlugn'
export * from './options'
export * from './styles'
export * from './transformPlugin'
34 changes: 34 additions & 0 deletions src/core/localePlugn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { createUnplugin } from 'unplugin'
import MagicString from 'magic-string'
import type { NuxtOptions } from '@nuxt/schema'
import { libraryName } from '../config'

interface LocalePluginOptions {
sourcemap?: NuxtOptions['sourcemap']['client']
locale: string
}

export const localePlugin = createUnplugin((options: LocalePluginOptions) => {
return {
name: `${libraryName}:locale`,
enforce: 'pre',
transformInclude (id) {
const regExp = new RegExp(`${libraryName}/es/locale/index`)
return !!id.match(regExp)
},
transform (code, id) {
const s = new MagicString(code)

s.replaceAll('zh-CN', options.locale)

if (s.hasChanged()) {
return {
code: s.toString(),
map: options.sourcemap
? s.generateMap({ source: id, includeContent: true })
: undefined
}
}
}
}
})
15 changes: 15 additions & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
resolveLazyload,
resolveOptions,
resolveStyles,
localePlugin,
transformPlugin
} from './core/index'
import type { Options } from './types'
Expand Down Expand Up @@ -36,6 +37,13 @@ export default defineNuxtModule<Partial<Options>>({
transformStyles: name => resolveStyles(options, name)
})
)

if (options.defaultLocale && options.defaultLocale !== 'zh-CN') {
config.plugins.push(localePlugin.vite({
sourcemap: nuxt.options.sourcemap[mode],
locale: options.defaultLocale
}))
}
})

nuxt.hook('webpack:config', (configs) => {
Expand All @@ -51,6 +59,13 @@ export default defineNuxtModule<Partial<Options>>({
transformStyles: name => resolveStyles(options, name)
})
)

if (options.defaultLocale && options.defaultLocale !== 'zh-CN') {
config.plugins.push(localePlugin.webpack({
sourcemap: nuxt.options.sourcemap[mode],
locale: options.defaultLocale
}))
}
})
})
}
Expand Down
7 changes: 7 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ export interface Options extends TransformOptions {
* @default true
*/
importStyle: boolean
/**
* Replace default locale, you can find locale list [here](https://github.com/youzan/vant/tree/main/packages/vant/src/locale/lang).
*
* @default 'zh-CN'
* @example 'en-US'
*/
defaultLocale?: string
/**
* Exclude exports from Vant that are not component content.
*
Expand Down

0 comments on commit 82b4589

Please sign in to comment.