- Notifications
You must be signed in to change notification settings - Fork9
Language detector that works universally (browser + server) - Meant to be used with a universal framework, such as Next.js
License
UnlyEd/universal-language-detector
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Language detector that works universally (browser + server)
- On the server, will rely on "cookies > accept-language header"
- On the browser, will rely on "cookies > navigator settings"
Meant to be used with a universal framework, such as Next.js
Note that this lib helps resolving thelanguage (fr,en,es, etc.),not the locale (fr-FR,en-US, etc.)
It is not out of scope though, PR are welcome to support universal locale detection.
Live demo with the Next.js example
yarn install @unly/universal-language-detectorUse:
import universalLanguageDetect from '@unly/universal-language-detector';ORimport { universalLanguageDetect } from '@unly/universal-language-detector';Seeour example featuring the Next.js framework
Extensive API documentation can be found in thesource code documentation
Only the most useful API methods are documented here, the other aren't meant to be used, even though they may be
Detects the language used, universally.
Parameters:
supportedLanguages: string[];fallbackLanguage: string;acceptLanguageHeader?: string | undefined;serverCookies?: object | undefined;errorHandler?:ErrorHandler | undefined;
Example:
constlang=universalLanguageDetect({supportedLanguages:SUPPORTED_LANGUAGES,// Whitelist of supported languages, will be used to filter out languages that aren't supportedfallbackLanguage:FALLBACK_LANG,// Fallback language in case the user's language cannot be resolvedacceptLanguageHeader:get(req,'headers.accept-language'),// Optional - Accept-language header will be used when resolving the language on the server sideserverCookies:cookies,// Optional - Cookie "i18next" takes precedence over navigator configuration (ex: "i18next: fr"), will only be used on the server sideerrorHandler:(error)=>{// Optional - Use you own logger here, Sentry, etc.console.log('Custom error handler:');console.error(error);},});
We gladly accept PRs, but please open an issue first so we can discuss it beforehand.
yarn start # Shortcut - Runs linter + build + tests in concurrent mode (watch mode)OR run each process separately for finer controlyarn lintyarn buildyarn testyarn test # Run all tests, interactive and watch modeyarn test:onceyarn test:coverageWe use Semantic Versioning for this project:https://semver.org/. (vMAJOR.MINOR.PATCH:v1.0.1)
- Major version: Must be changed when Breaking Changes are made (public API isn't backward compatible).
- A function has been renamed/removed from the public API
- Something has changed that will cause the app to behave differently with the same configuration
- Minor version: Must be changed when a new feature is added or updated (without breaking change nor behavioral change)
- Patch version: Must be changed when any change is made that isn't either Major nor Minor. (Misc, doc, etc.)
yarn releaseAndPublish # Shortcut - Will prompt for bump version, commit, create git tag, push commit/tag and publish to NPMyarn release # Will prompt for bump version, commit, create git tag, push commit/tagnpm publish # Will publish to NPMDon't forget we are using SemVer, please follow our SemVer rules.
Pro hint: usebeta tag if you're in a work-in-progress (or unsure) to avoid releasing WIP versions that looks legit
Our API change (including breaking changes and "how to migrate") are documented in the Changelog.
Seechangelog
MIT
This project was generated usinghttps://github.com/UnlyEd/boilerplate-generator/tree/master/templates/typescript-OSS
This project is being maintained by:
- [Unly] Ambroise Dhenain (Vadorequest)(active)
Unly is a socially responsible company, fighting inequality and facilitating access to higher education.Unly is committed to making education more inclusive, through responsible funding for students.
We provide technological solutions to help students find the necessary funding for their studies.
We proudly participate in many TechForGood initiatives. To support and learn more about our actions to make education accessible, visit :
- https://twitter.com/UnlyEd
- https://www.facebook.com/UnlyEd/
- https://www.linkedin.com/company/unly
- Interested to work with us?
Tech tips and tricks from our CTO on ourMedium page!
#TECHFORGOOD #EDUCATIONFORALL
About
Language detector that works universally (browser + server) - Meant to be used with a universal framework, such as Next.js
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors2
Uh oh!
There was an error while loading.Please reload this page.