forked from wiziple/browser-lang
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
1,974 additions
and
2,058 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,10 @@ | ||
{ | ||
"presets": ["es2015"] | ||
} | ||
"presets": [ | ||
[ | ||
"@babel/preset-env", | ||
{ | ||
"targets": "> 0.25%, ie >= 9" | ||
} | ||
] | ||
] | ||
} |
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,7 @@ | ||
{ | ||
"endOfLine": "lf", | ||
"semi": false, | ||
"singleQuote": false, | ||
"tabWidth": 2, | ||
"trailingComma": "es5" | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,65 @@ | ||
import startsWith from 'lodash/startsWith'; | ||
function startsWith(string, target, position) { | ||
const { length } = string | ||
position = position == null ? 0 : position | ||
if (position < 0) { | ||
position = 0 | ||
} else if (position > length) { | ||
position = length | ||
} | ||
target = `${target}` | ||
return string.slice(position, position + target.length) == target | ||
} | ||
|
||
function getBrowserLang() { | ||
if (typeof window === 'undefined') { | ||
return null; | ||
if (typeof window === "undefined") { | ||
return null | ||
} | ||
|
||
const lang = (window.navigator.languages && window.navigator.languages[0]) | ||
|| window.navigator.language | ||
|| window.navigator.browserLanguage | ||
|| window.navigator.userLanguage | ||
|| window.navigator.systemLanguage | ||
|| null; | ||
const lang = | ||
(window.navigator.languages && window.navigator.languages[0]) || | ||
window.navigator.language || | ||
window.navigator.browserLanguage || | ||
window.navigator.userLanguage || | ||
window.navigator.systemLanguage || | ||
null | ||
|
||
return lang; | ||
return lang | ||
} | ||
|
||
function normalizeCode(code) { | ||
return code.toLowerCase().replace(/-/, '_'); | ||
return code.toLowerCase().replace(/-/, "_") | ||
} | ||
|
||
function getPreferredLanguage(options) { | ||
if (!options) { | ||
return getBrowserLang(); | ||
return getBrowserLang() | ||
} | ||
|
||
const { languages, fallback } = options; | ||
const { languages, fallback } = options | ||
if (!options.languages) { | ||
return fallback; | ||
return fallback | ||
} | ||
|
||
// some browsers report language as en-US instead of en_US | ||
const browserLanguage = normalizeCode(getBrowserLang()); | ||
const browserLanguage = normalizeCode(getBrowserLang()) | ||
|
||
if (!browserLanguage) { | ||
return fallback; | ||
return fallback | ||
} | ||
|
||
const match = languages.filter(lang => normalizeCode(lang) === browserLanguage); | ||
const match = languages.filter( | ||
lang => normalizeCode(lang) === browserLanguage | ||
) | ||
|
||
if (match.length > 0) { | ||
return match[0] || fallback; | ||
return match[0] || fallback | ||
} | ||
|
||
// en == en_US | ||
const matchCodeOnly = languages.filter(lang => startsWith(browserLanguage, lang)); | ||
return matchCodeOnly[0] || fallback; | ||
const matchCodeOnly = languages.filter(lang => | ||
startsWith(browserLanguage, lang) | ||
) | ||
return matchCodeOnly[0] || fallback | ||
} | ||
|
||
export default getPreferredLanguage; | ||
export default getPreferredLanguage |
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,84 +1,84 @@ | ||
import browserLang from '../src'; | ||
import browserLang from "../src" | ||
|
||
const mockNavigator = (obj) => { | ||
const mockNavigator = obj => { | ||
// eslint-disable-next-line no-restricted-syntax | ||
for (const key in obj) { | ||
if (Object.prototype.hasOwnProperty.call(obj, key)) { | ||
Object.defineProperty(window.navigator, key, { | ||
value: obj[key], | ||
configurable: true, | ||
}); | ||
}) | ||
} | ||
} | ||
}; | ||
} | ||
|
||
const initNavigator = () => { | ||
mockNavigator({ | ||
languages: undefined, | ||
language: undefined, | ||
browserLanguage: undefined, | ||
userLanguage: undefined, | ||
}); | ||
}; | ||
}) | ||
} | ||
|
||
describe('browserLang', () => { | ||
initNavigator(); | ||
describe("browserLang", () => { | ||
initNavigator() | ||
|
||
it('should return null when navigator.languages is undefined.', () => { | ||
expect(browserLang()).toBe(null); | ||
}); | ||
it("should return null when navigator.languages is undefined.", () => { | ||
expect(browserLang()).toBe(null) | ||
}) | ||
|
||
it('should return "ko_KR" when navigator.languages is defined as ["ko_KR", "ko"]', () => { | ||
mockNavigator({ | ||
languages: ['ko_KR', 'ko'], | ||
}); | ||
expect(browserLang()).toBe('ko_KR'); | ||
}); | ||
languages: ["ko_KR", "ko"], | ||
}) | ||
expect(browserLang()).toBe("ko_KR") | ||
}) | ||
|
||
it('should return "ko_KR" when navigator.browserLanguage is defined as "ko_KR"', () => { | ||
mockNavigator({ | ||
browserLanguage: 'ko_KR', | ||
}); | ||
expect(browserLang()).toBe('ko_KR'); | ||
}); | ||
browserLanguage: "ko_KR", | ||
}) | ||
expect(browserLang()).toBe("ko_KR") | ||
}) | ||
|
||
it('should return fallback when navigator.languages is undefined.', () => { | ||
const fallback = 'ko_KR'; | ||
it("should return fallback when navigator.languages is undefined.", () => { | ||
const fallback = "ko_KR" | ||
const options = { | ||
fallback, | ||
}; | ||
expect(browserLang(options)).toBe(fallback); | ||
}); | ||
} | ||
expect(browserLang(options)).toBe(fallback) | ||
}) | ||
|
||
it('should return fallback when there is no available language on the list.', () => { | ||
const fallback = 'ko_KR'; | ||
it("should return fallback when there is no available language on the list.", () => { | ||
const fallback = "ko_KR" | ||
mockNavigator({ | ||
languages: ['de'], | ||
}); | ||
languages: ["de"], | ||
}) | ||
const options = { | ||
languages: ['fr', 'ko', 'en'], | ||
languages: ["fr", "ko", "en"], | ||
fallback, | ||
}; | ||
expect(browserLang(options)).toBe(fallback); | ||
}); | ||
} | ||
expect(browserLang(options)).toBe(fallback) | ||
}) | ||
|
||
it('should return "zh_TW" when navigator.languages is ["zh_TW"] and "zh_TW" is on the list.', () => { | ||
mockNavigator({ | ||
languages: ['zh_TW'], | ||
}); | ||
languages: ["zh_TW"], | ||
}) | ||
const options = { | ||
languages: ['zh', 'zh_TW', 'en'], | ||
}; | ||
expect(browserLang(options)).toBe('zh_TW'); | ||
}); | ||
languages: ["zh", "zh_TW", "en"], | ||
} | ||
expect(browserLang(options)).toBe("zh_TW") | ||
}) | ||
|
||
it('should return "zh" when navigator.languages is ["zh_TW"] and only "zh" is on the list.', () => { | ||
mockNavigator({ | ||
languages: ['zh_TW'], | ||
}); | ||
languages: ["zh_TW"], | ||
}) | ||
const options = { | ||
languages: ['zh', 'en'], | ||
}; | ||
expect(browserLang(options)).toBe('zh'); | ||
}); | ||
}); | ||
languages: ["zh", "en"], | ||
} | ||
expect(browserLang(options)).toBe("zh") | ||
}) | ||
}) |
Oops, something went wrong.