diff --git a/packages/validator/src/__tests__/registry.spec.ts b/packages/validator/src/__tests__/registry.spec.ts index f85d18a64e7..4ecfe0a1088 100644 --- a/packages/validator/src/__tests__/registry.spec.ts +++ b/packages/validator/src/__tests__/registry.spec.ts @@ -1,4 +1,10 @@ -import { getValidateLocaleIOSCode } from '../' +import { + getValidateLocaleIOSCode, + getLocaleByPath, + getValidateLocale, + setValidateLanguage, +} from '../' +import locale from '../locale' test('getValidateLocaleIOSCode', () => { expect(getValidateLocaleIOSCode('zh-CN')).toEqual('zh-CN') @@ -8,3 +14,13 @@ test('getValidateLocaleIOSCode', () => { expect(getValidateLocaleIOSCode('en')).toEqual('en') expect(getValidateLocaleIOSCode('TW')).toEqual('zh-TW') }) + +test('getLocaleByPath', () => { + expect(getLocaleByPath('pattern', 'vi')).toEqual(locale.en.pattern) + expect(getLocaleByPath('pattern')).toEqual(locale.en.pattern) +}) + +test('getValidateLocale', () => { + setValidateLanguage('vi') + expect(getValidateLocale('pattern')).toEqual(locale.en.pattern) +}) diff --git a/packages/validator/src/registry.ts b/packages/validator/src/registry.ts index bf1b97bc8d6..2d455f4030f 100644 --- a/packages/validator/src/registry.ts +++ b/packages/validator/src/registry.ts @@ -69,12 +69,15 @@ export const setValidateLanguage = (lang: string) => { export const getValidateLanguage = () => registry.locales.language +export const getLocaleByPath = (path: string, lang: string = registry.locales.language) => getIn( + registry.locales.messages, + `${getISOCode(lang)}.${path}` +) + + export const getValidateLocale = (path: string) => { - const message = getIn( - registry.locales.messages, - `${getISOCode(registry.locales.language)}.${path}` - ) - return message || getValidateLocale('pattern') + const message = getLocaleByPath(path); + return message || getLocaleByPath('pattern') || getLocaleByPath('pattern', defaultLanguage) } export const getValidateMessageTemplateEngine = () => registry.template