@@ -56,14 +56,14 @@ export abstract class LocaleMessage {
56
56
fullpath : string
57
57
locales ?: string [ ]
58
58
localeKey : LocaleKeyType
59
- localePattern ?: RegExp
59
+ localePattern ?: string | RegExp
60
60
} ) {
61
61
this . fullpath = fullpath
62
62
/** @type {LocaleKeyType } Specifies how to determine the locale for localization messages. */
63
63
this . localeKey = localeKey
64
64
/** @type {string } The localization messages file name. */
65
65
this . file = fullpath . replace ( / ^ .* ( \\ | \/ | : ) / , '' )
66
- this . localePattern = localePattern || DEFAULT_LOCALE_CAPTURE_REGEX
66
+ this . localePattern = this . getLocalePatternWithRegex ( localePattern )
67
67
68
68
this . _locales = locales
69
69
}
@@ -73,6 +73,20 @@ export abstract class LocaleMessage {
73
73
*/
74
74
abstract getMessagesInternal ( ) : I18nLocaleMessageDictionary
75
75
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
+
76
90
/**
77
91
* @returns {object } The localization messages object.
78
92
*/
@@ -178,7 +192,7 @@ export class FileLocaleMessage extends LocaleMessage {
178
192
* @param {string } arg.fullpath Absolute path.
179
193
* @param {string[] } [arg.locales] The locales.
180
194
* @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.
182
196
*/
183
197
constructor ( {
184
198
fullpath,
@@ -189,7 +203,7 @@ export class FileLocaleMessage extends LocaleMessage {
189
203
fullpath : string
190
204
locales ?: string [ ]
191
205
localeKey : LocaleKeyType
192
- localePattern ?: RegExp
206
+ localePattern ?: string | RegExp
193
207
} ) {
194
208
super ( {
195
209
fullpath,
0 commit comments