Skip to content

Commit

Permalink
feat: support no-option
Browse files Browse the repository at this point in the history
  • Loading branch information
hemengke1997 committed Oct 20, 2023
1 parent f656868 commit 01f76b4
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 27 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ pnpm add vite-plugin-i18n-detector -D


## 配置项
| 参数 | 类型 | 默认值 | 描述 |
| ------------- | ---------------- | --------------------------------------- | ---------------------- |
| localesPaths | `string[]` | undefined | 存放语言资源的目录地址 |
| pathMatcher | `string` | undefined | 资源文件匹配规则 |
| parserPlugins | `ParserPlugin[]` | `[jsonParser, json5Parser, yamlParser]` | 资源文件解析插件 |
| root | `string` | `process.cwd()` | 项目根目录 |
| 参数 | 类型 | 默认值 | 描述 |
| ------------- | ---------------- | --------------- | ---------------------- |
| localesPaths | `string[]` | undefined | 存放语言资源的目录地址 |
| pathMatcher | `string` | undefined | 资源文件匹配规则 |
| parserPlugins | `ParserPlugin[]` | `[]` | 资源文件解析插件 |
| root | `string` | `process.cwd()` | 项目根目录 |

## 配置参考

Expand Down
16 changes: 1 addition & 15 deletions playground/spa/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,6 @@ import { i18nDetector } from 'vite-plugin-i18n-detector'
// https://vitejs.dev/config/
export default defineConfig({
base: '/vite-plugin-i18n-detector/',
plugins: [
react(),
i18nDetector({
localesPaths: [path.join(__dirname, './src/locales')],
pathMatcher: '{locale}/{namespace}.{ext}',
parserPlugins: [
{
ext: 'properties', // just for example
parse() {
// how to parse properties file, it's up to you
},
},
],
}),
],
plugins: [react(), i18nDetector()],
clearScreen: false,
})
20 changes: 15 additions & 5 deletions src/plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,34 @@ import { debug } from './utils/debugger'
import { initWatcher } from './utils/file-watcher'
import { hmr } from './utils/hmr'
import { type ParserConstructor } from './parsers/Parser'
import { initOptions } from './utils/init-options'

export type ParserPlugin = ParserConstructor | undefined

export interface I18nDetectorOptions {
/**
* @description locales directory paths
* @default
* ```js
* [path.resolve(root, './src/locales')]
* ```
* @example
* [path.resolve(__dirname, './src/locales')]
* ['./src/locales']
*/
localesPaths: string[]
localesPaths?: string[]
/**
* @description rule of matching locale file
* @default
* ```js
* '{locale}/{namespaces}.{ext}'
* ```
* @example
* `{locale}/{namespaces}.{ext}`
* `{locale}/{namespace}.json`
* `{namespaces}/{locale}`
* `something/{locale}/{namespace}`
*/
pathMatcher: string
pathMatcher?: string
/**
* @description
* parser plugins
Expand All @@ -48,11 +56,13 @@ export interface I18nDetectorOptions {
root?: string
}

export async function i18nDetector(options: I18nDetectorOptions) {
export async function i18nDetector(opts?: I18nDetectorOptions) {
const options = initOptions(opts)

debug('i18nDetector options:', options)

const localeDetector = new LocaleDetector({
root: options.root || process.cwd(),
root: options.root,
localesPaths: options.localesPaths,
pathMatcher: options.pathMatcher,
parserPlugins: options.parserPlugins,
Expand Down
2 changes: 1 addition & 1 deletion src/plugin/locale-detector/LocaleDetector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { debug } from '../utils/debugger'
import { logger } from '../utils/logger'
import { Parser } from '../parsers/Parser'

export interface Config extends I18nDetectorOptions {
export interface Config extends Required<I18nDetectorOptions> {
root: string
}

Expand Down
2 changes: 2 additions & 0 deletions src/plugin/parsers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ import { json5Parser } from './json5'
import { yamlParser } from './yaml'

export const DefaultEnabledParsers = [jsonParser, json5Parser, yamlParser]

export const DefaultParserPlugins = DefaultEnabledParsers.map((t) => t.parser)
25 changes: 25 additions & 0 deletions src/plugin/utils/init-options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import path from 'node:path'
import { type I18nDetectorOptions } from '..'

export const DEFAULT_OPTIONS: Required<I18nDetectorOptions> = {
localesPaths: [path.resolve(process.cwd(), './src/locales')],
pathMatcher: '{locale}/{namespaces}.{ext}',
parserPlugins: [],
root: process.cwd(),
}

export function initOptions(options?: I18nDetectorOptions) {
if (!options) return DEFAULT_OPTIONS
if (options.root) {
if (!options.localesPaths?.length) {
options.localesPaths = DEFAULT_OPTIONS.localesPaths.map((p) => path.resolve(options.root!, p))
}
} else {
options.root = DEFAULT_OPTIONS.root
}

return {
...DEFAULT_OPTIONS,
options,
} as Required<I18nDetectorOptions>
}

0 comments on commit 01f76b4

Please sign in to comment.