Skip to content

Commit

Permalink
fix: add defaultLanguage & doc: update setValidateLanguage (#1548)
Browse files Browse the repository at this point in the history
  • Loading branch information
zkylearner committed Jun 7, 2021
1 parent 6835f6d commit b277752
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 10 deletions.
4 changes: 2 additions & 2 deletions packages/core/docs/api/entry/FormValidatorRegistry.md
Expand Up @@ -13,15 +13,15 @@ order: 6
#### 绛惧悕

```ts
interface setValidationLanguage {
interface setValidateLanguage{
(language: string): void
}
```

#### 鐢ㄤ緥

```ts
import { setValidationLanguage } from '@formily/core'
import { setValidateLanguage} from '@formily/core'

setValidationLanguage('en-US')

Expand Down
4 changes: 2 additions & 2 deletions packages/next/README.zh-cn.md
Expand Up @@ -1325,13 +1325,13 @@ actions.getFieldValue('username').then((val) => console.log(val))
**绛惧悕**

```typescript
const setValidationLanguage = (lang: string) => void;
const setValidateLanguage= (lang: string) => void;
```

**鐢ㄦ硶**

```tsx
import { setValidationLanguage } from '@formily/next'
import { setValidateLanguage} from '@formily/next'
setValidationLanguage('zh')
```

Expand Down
5 changes: 2 additions & 3 deletions packages/next/src/form/index.tsx
Expand Up @@ -21,9 +21,8 @@ export const Form: React.FC<FormProps> = ({
// @ts-ignore
const lang = ConfigProvider.getContext()?.locale?.momentLocale
useMemo(() => {
if (getValidateLocaleIOSCode(lang)) {
setValidateLanguage(lang)
}
const validateLanguage = getValidateLocaleIOSCode(lang);
setValidateLanguage(validateLanguage)
}, [lang])

return (
Expand Down
8 changes: 5 additions & 3 deletions packages/validator/src/registry.ts
Expand Up @@ -19,12 +19,14 @@ const getIn = FormPath.getIn

const self: any = globalThisPolyfill

const defaultLanguage = 'en'

const getBrowserlanguage = () => {
/* istanbul ignore next */
if (!self.navigator) {
return 'en'
return defaultLanguage
}
return self.navigator.browserlanguage || self.navigator.language || 'en'
return self.navigator.browserlanguage || self.navigator.language || defaultLanguage
}

const registry = {
Expand Down Expand Up @@ -57,7 +59,7 @@ const getISOCode = (language: string) => {
export const getValidateLocaleIOSCode = getISOCode;

export const setValidateLanguage = (lang: string) => {
registry.locales.language = lang
registry.locales.language = lang || defaultLanguage
}

export const getValidateLanguage = () => registry.locales.language
Expand Down

0 comments on commit b277752

Please sign in to comment.