-
Notifications
You must be signed in to change notification settings - Fork 225
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Try to detect language from html lang attributes and navigator.languages #38
Open
wangcheng
wants to merge
9
commits into
zhihu:master
Choose a base branch
from
wangcheng:locale
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
559dc41
refactor: rename LocaleContext to LanguageContext
ed97803
feat: detect language from html lang attributes and navigator.languages
19b8a5d
docs: update docs for language
c6af752
build: example use source field
wangcheng a4d4c46
chore: remove @zhihu/babel-preset
wangcheng 521dac7
Merge branch 'master' into build
wangcheng c8ff066
Merge branch 'master' into build
wangcheng 3a371cd
Merge pull request #76 from wangcheng678/build
wangcheng eb702b1
Merge branch 'master' into locale
wangcheng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
6 changes: 3 additions & 3 deletions
6
packages/griffith/src/components/TranslatedText/TranslatedText.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 |
---|---|---|
@@ -1,11 +1,11 @@ | ||
import React from 'react' | ||
import {LocaleContext} from '../../contexts/Locale' | ||
import {LanguageContext} from '../../contexts/Language' | ||
import strings from './strings' | ||
|
||
const TranslatedText = ({name}) => ( | ||
<LocaleContext.Consumer> | ||
<LanguageContext.Consumer> | ||
lixiaoyan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{locale => strings[locale][name]} | ||
</LocaleContext.Consumer> | ||
</LanguageContext.Consumer> | ||
) | ||
|
||
export default TranslatedText |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import React from 'react' | ||
|
||
const LanguageContext = React.createContext('en') | ||
LanguageContext.displayName = 'LanguageContext' | ||
|
||
export default LanguageContext |
47 changes: 47 additions & 0 deletions
47
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,47 @@ | ||
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) | ||
|
||
function getLanguage(props) { | ||
const {language} = props | ||
if (isValid(language)) { | ||
return language | ||
} else if (typeof document !== 'undefined') { | ||
// 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] | ||
return guessLanguage(languageList) | ||
} | ||
|
||
return SUPPORTED_LANGUAGES.EN | ||
} | ||
|
||
export default class LanguageProvider extends React.PureComponent { | ||
static propTypes = { | ||
language: PropTypes.oneOf(VALID_LANGUAGES), | ||
} | ||
|
||
state = { | ||
language: SUPPORTED_LANGUAGES.EN, | ||
} | ||
|
||
static getDerivedStateFromProps = (props, state) => { | ||
const language = getLanguage(props) | ||
return language === state.language ? null : {language} | ||
} | ||
|
||
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 |
---|---|---|
@@ -0,0 +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 |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
是不是保留
locale
比较好,language
可能有歧义。There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
看了下 Web 的 Intl API 也用的
locale
这个词。https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RelativeTimeFormat#Syntax
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
locale 和 language 还是不一样的。我们看起来只区分了语言,并没有区分「区域」,比如美国和英国词汇不同,数字格式不同之类的。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我们这个属性可以填的值兼容了 locale 的写法,如果本意就是 locale 感觉写 locale 也没什么问题。美国英国的不同 locale 表现成同一种外观而已。