Installations
npm install @byhuz/i18next-browser-languagedetector
Score
73
Supply Chain
98.9
Quality
80.1
Maintenance
100
Vulnerability
100
License
Releases
Unable to fetch releases
Developer
i18next
Module System
CommonJS
Statistics
871 Stars
246 Commits
89 Forks
9 Watching
92 Branches
27 Contributors
Updated on 20 Nov 2024
Languages
JavaScript (94.47%)
TypeScript (5.53%)
Total Downloads
Cumulative downloads
Total Downloads
5,100
Last day
0%
4
Compared to previous day
Last week
300%
4
Compared to previous week
Last month
-21.1%
15
Compared to previous month
Last year
-59.5%
394
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
1
Dev Dependencies
26
Introduction
This is an i18next language detection plugin used to detect user language in the browser, with support for:
- cookie (set cookie i18next=LANGUAGE)
- sessionStorage (set key i18nextLng=LANGUAGE)
- localStorage (set key i18nextLng=LANGUAGE)
- navigator (set browser language)
- querystring (append
?lng=LANGUAGE
to URL) - htmlTag (add html language tag <html lang="LANGUAGE" ...)
- path (http://my.site.com/LANGUAGE/...)
- subdomain (http://LANGUAGE.site.com/...)
Getting started
Source can be loaded via npm, bower or downloaded from this repo.
# npm package
$ npm install i18next-browser-languagedetector
# bower
$ bower install i18next-browser-languagedetector
- If you don't use a module loader it will be added to
window.i18nextBrowserLanguageDetector
Wiring up:
1import i18next from 'i18next'; 2import LanguageDetector from 'i18next-browser-languagedetector'; 3 4i18next.use(LanguageDetector).init({ 5 supportedLngs: ['de', 'en', 'fr'], 6 ...i18nextOptions 7});
As with all modules you can either pass the constructor function (class) to the i18next.use
or to a concrete instance.
supportedLngs
is optional, but allows i18next to pick the best match from the list of detected languages. If it's not set then language
will be set to the first detected language, regardless of whether your application has translations for that language or not.
Detector Options
The default options can be found here.
1{ 2 // order and from where user language should be detected 3 order: ['querystring', 'cookie', 'localStorage', 'sessionStorage', 'navigator', 'htmlTag', 'path', 'subdomain'], 4 5 // keys or params to lookup language from 6 lookupQuerystring: 'lng', 7 lookupCookie: 'i18next', 8 lookupLocalStorage: 'i18nextLng', 9 lookupSessionStorage: 'i18nextLng', 10 lookupFromPathIndex: 0, 11 lookupFromSubdomainIndex: 0, 12 13 // cache user language on 14 caches: ['localStorage', 'cookie'], 15 excludeCacheFor: ['cimode'], // languages to not persist (cookie, localStorage) 16 17 // optional expiry and domain for set cookie 18 cookieMinutes: 10, 19 cookieDomain: 'myDomain', 20 21 // optional htmlTag with lang attribute, the default is: 22 htmlTag: document.documentElement, 23 24 // optional set cookie options, reference:[MDN Set-Cookie docs](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie) 25 cookieOptions: { path: '/', sameSite: 'strict' }, 26 27 // optional conversion function used to modify the detected language code 28 convertDetectedLanguage: 'Iso15897', 29 convertDetectedLanguage: (lng) => lng.replace('-', '_') 30}
Options can be passed in:
preferred - by setting options.detection in i18next.init:
1import i18next from 'i18next'; 2import LanguageDetector from 'i18next-browser-languagedetector'; 3 4i18next.use(LanguageDetector).init({ 5 detection: options, 6});
on construction:
1import LanguageDetector from 'i18next-browser-languagedetector'; 2const languageDetector = new LanguageDetector(null, options);
via calling init:
1import LanguageDetector from 'i18next-browser-languagedetector'; 2const languageDetector = new LanguageDetector(); 3languageDetector.init(options);
Adding own detection functionality
interface
1export default { 2 name: 'myDetectorsName', 3 4 lookup(options) { 5 // options -> are passed in options 6 return 'en'; 7 }, 8 9 cacheUserLanguage(lng, options) { 10 // options -> are passed in options 11 // lng -> current language, will be called after init and on changeLanguage 12 // store it 13 }, 14};
adding it
1import LanguageDetector from 'i18next-browser-languagedetector'; 2const languageDetector = new LanguageDetector(); 3languageDetector.addDetector(myDetector); 4 5i18next.use(languageDetector).init({ 6 detection: options, 7});
Don't forget: You have to add the name of your detector (myDetectorsName
in this case) to the order
array in your options
object. Without that, your detector won't be used. See the Detector Options section for more.
Gold Sponsors
localization as a service - locize.com
Needing a translation management? Want to edit your translations with an InContext Editor? Use the orginal provided to you by the maintainers of i18next!
With using locize you directly support the future of i18next and react-i18next.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
2 commit(s) and 2 issue activity found in the last 90 days -- score normalized to 3
Reason
Found 6/23 approved changesets -- score normalized to 2
Reason
8 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-p8p7-x288-28g6
- Warn: Project is vulnerable to: GHSA-gcx4-mw62-g8wm
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-f5x3-32g6-xq36
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 13 are checked with a SAST tool
Score
2.7
/10
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More