Skip to content

Commit 1138c0c

Browse files
committed
fix: support string value
1 parent df6f276 commit 1138c0c

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

lib/types/settings.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,5 @@ export interface SettingsVueI18nLocaleDirObject {
3838
* You need to use the locale capture as a named capture `?<locale>`, so it’s be able to capture from the path of the locale resources.
3939
* If you omit it, it will be captured from the resource path with the same regular expression pattern as `vue-cli-plugin-i18n`.
4040
*/
41-
localePattern?: RegExp
41+
localePattern?: string | RegExp
4242
}

lib/utils/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import { getCwd } from './get-cwd'
2929
interface LocaleFiles {
3030
files: string[]
3131
localeKey: LocaleKeyType
32-
localePattern?: RegExp
32+
localePattern?: string | RegExp
3333
}
3434
const UNEXPECTED_ERROR_LOCATION = { line: 1, column: 0 }
3535
/**

lib/utils/locale-messages.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,14 @@ export abstract class LocaleMessage {
5656
fullpath: string
5757
locales?: string[]
5858
localeKey: LocaleKeyType
59-
localePattern?: RegExp
59+
localePattern?: string | RegExp
6060
}) {
6161
this.fullpath = fullpath
6262
/** @type {LocaleKeyType} Specifies how to determine the locale for localization messages. */
6363
this.localeKey = localeKey
6464
/** @type {string} The localization messages file name. */
6565
this.file = fullpath.replace(/^.*(\\|\/|:)/, '')
66-
this.localePattern = localePattern || DEFAULT_LOCALE_CAPTURE_REGEX
66+
this.localePattern = this.getLocalePatternWithRegex(localePattern)
6767

6868
this._locales = locales
6969
}
@@ -73,6 +73,20 @@ export abstract class LocaleMessage {
7373
*/
7474
abstract getMessagesInternal(): I18nLocaleMessageDictionary
7575

76+
/**
77+
* Get locale pattern with regular expression
78+
*/
79+
getLocalePatternWithRegex(localePattern?: string | RegExp): RegExp {
80+
// prettier-ignore
81+
return localePattern != null
82+
? typeof localePattern === 'string'
83+
? new RegExp(localePattern, 'i')
84+
: Object.prototype.toString.call(localePattern) === '[object RegExp]'
85+
? localePattern
86+
: DEFAULT_LOCALE_CAPTURE_REGEX
87+
: DEFAULT_LOCALE_CAPTURE_REGEX
88+
}
89+
7690
/**
7791
* @returns {object} The localization messages object.
7892
*/
@@ -178,7 +192,7 @@ export class FileLocaleMessage extends LocaleMessage {
178192
* @param {string} arg.fullpath Absolute path.
179193
* @param {string[]} [arg.locales] The locales.
180194
* @param {LocaleKeyType} arg.localeKey Specifies how to determine the locale for localization messages.
181-
* @param {RegExp} args.localePattern Specifies how to determin the regular expression pattern for how to get the locale.
195+
* @param {string | RegExp} args.localePattern Specifies how to determin the regular expression pattern for how to get the locale.
182196
*/
183197
constructor({
184198
fullpath,
@@ -189,7 +203,7 @@ export class FileLocaleMessage extends LocaleMessage {
189203
fullpath: string
190204
locales?: string[]
191205
localeKey: LocaleKeyType
192-
localePattern?: RegExp
206+
localePattern?: string | RegExp
193207
}) {
194208
super({
195209
fullpath,

0 commit comments

Comments
 (0)