-
Notifications
You must be signed in to change notification settings - Fork 225
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: detect language from html lang attributes and navigator.languages
- Loading branch information
wangcheng
committed
Mar 27, 2019
1 parent
f7b4c22
commit 9ffffc0
Showing
7 changed files
with
130 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
packages/griffith/src/contexts/Language/LanguageProvider.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import React from 'react' | ||
import PropTypes from 'prop-types' | ||
import LanguageContext from './LanguageContext' | ||
import guessLanguage from './guessLanguage' | ||
import SUPPORTED_LANGUAGES from './supportedLanguages' | ||
|
||
const VALID_LANGUAGES = Object.keys(SUPPORTED_LANGUAGES) | ||
|
||
const isValid = language => VALID_LANGUAGES.includes(language) | ||
|
||
export default class LanguageProvider extends React.Component { | ||
static propTypes = { | ||
language: PropTypes.oneOf(VALID_LANGUAGES), | ||
} | ||
|
||
state = { | ||
language: isValid(this.props.language) | ||
? this.props.language | ||
: SUPPORTED_LANGUAGES.EN, | ||
} | ||
|
||
componentDidMount() { | ||
if (!isValid(this.props.language)) { | ||
// detect language from html lang attributes and navigator.languages | ||
const htmlLanguage = document.documentElement.getAttribute('lang') | ||
const navigatorLanguages = navigator.languages || [navigator.language] | ||
const languageList = [htmlLanguage, ...navigatorLanguages] | ||
this.setState({language: guessLanguage(languageList)}) | ||
} | ||
} | ||
|
||
render() { | ||
return ( | ||
<LanguageContext.Provider value={this.state.language}> | ||
{this.props.children} | ||
</LanguageContext.Provider> | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import SUPPORTED_LANGUAGES from './supportedLanguages' | ||
|
||
const KEY_WORDS = [ | ||
['en', SUPPORTED_LANGUAGES.EN], | ||
['ja', SUPPORTED_LANGUAGES.JA], | ||
|
||
['hant', SUPPORTED_LANGUAGES.ZH_HANT], | ||
['hans', SUPPORTED_LANGUAGES.ZH_HANS], | ||
|
||
['hk', SUPPORTED_LANGUAGES.ZH_HANT], | ||
['tw', SUPPORTED_LANGUAGES.ZH_HANT], | ||
|
||
['cn', SUPPORTED_LANGUAGES.ZH_HANS], | ||
['sg', SUPPORTED_LANGUAGES.ZH_HANS], | ||
|
||
['zh', SUPPORTED_LANGUAGES.ZH_HANS], // prefer Simplified Chinese to Traditional if not specified | ||
] | ||
|
||
function matchKeyword(langCode) { | ||
const found = KEY_WORDS.find(([keyword]) => langCode.includes(keyword)) | ||
return found ? found[1] : null | ||
} | ||
|
||
function guessLanguage(languageList) { | ||
const list = languageList.filter(Boolean).map(s => s.toLocaleLowerCase()) | ||
for (let i = 0; i < list.length; i = i + 1) { | ||
const matchedSupportedLanguage = matchKeyword(list[i]) | ||
if (matchedSupportedLanguage) { | ||
return matchedSupportedLanguage | ||
} | ||
} | ||
return SUPPORTED_LANGUAGES.EN | ||
} | ||
|
||
export default guessLanguage |
37 changes: 37 additions & 0 deletions
37
packages/griffith/src/contexts/Language/guessLanguage.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import guessLanguage from './guessLanguage' | ||
import SUPPORTED_LANGUAGES from './supportedLanguages' | ||
|
||
describe('guessLanguage', () => { | ||
it('should return first suppored language in the list', () => { | ||
expect(guessLanguage(['zh-tw', 'en'])).toBe(SUPPORTED_LANGUAGES.ZH_HANT) | ||
expect(guessLanguage(['fr', 'en'])).toBe(SUPPORTED_LANGUAGES.EN) | ||
}) | ||
|
||
it('should return default language if no match found', () => { | ||
expect(guessLanguage(['es', 'de'])).toBe(SUPPORTED_LANGUAGES.EN) | ||
}) | ||
|
||
it('should return default language if provided empty list', () => { | ||
expect(guessLanguage([])).toBe(SUPPORTED_LANGUAGES.EN) | ||
}) | ||
|
||
it('handle various Chinese language codes', () => { | ||
expect(guessLanguage(['zh-CN'])).toBe(SUPPORTED_LANGUAGES.ZH_HANS) | ||
expect(guessLanguage(['zh-SG'])).toBe(SUPPORTED_LANGUAGES.ZH_HANS) | ||
|
||
expect(guessLanguage(['zh-TW'])).toBe(SUPPORTED_LANGUAGES.ZH_HANT) | ||
expect(guessLanguage(['zh-HK'])).toBe(SUPPORTED_LANGUAGES.ZH_HANT) | ||
|
||
expect(guessLanguage(['zh-cmn-Hans'])).toBe(SUPPORTED_LANGUAGES.ZH_HANS) | ||
expect(guessLanguage(['cmn-Hans-CN'])).toBe(SUPPORTED_LANGUAGES.ZH_HANS) | ||
expect(guessLanguage(['zh-cmn-Hans-HK'])).toBe(SUPPORTED_LANGUAGES.ZH_HANS) | ||
|
||
expect(guessLanguage(['zh-cmn-Hant'])).toBe(SUPPORTED_LANGUAGES.ZH_HANT) | ||
expect(guessLanguage(['zh-cmn-Hant'])).toBe(SUPPORTED_LANGUAGES.ZH_HANT) | ||
expect(guessLanguage(['zh-cmn-Hant-CN'])).toBe(SUPPORTED_LANGUAGES.ZH_HANT) | ||
}) | ||
|
||
it('prefer Simplified Chinese to Traditional if not specified', () => { | ||
expect(guessLanguage(['zh'])).toBe(SUPPORTED_LANGUAGES.ZH_HANS) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export {default as LanguageContext} from './LanguageContext' | ||
export {default as LanguageProvider} from './LanguageProvider' |
8 changes: 8 additions & 0 deletions
8
packages/griffith/src/contexts/Language/supportedLanguages.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const SUPPORTED_LANGUAGES = { | ||
EN: 'en', | ||
JA: 'hans', | ||
ZH_HANS: 'zh-Hans', | ||
ZH_HANT: 'zh-Hant', | ||
} | ||
|
||
export default SUPPORTED_LANGUAGES |