├── .gitignore ├── index.js ├── license ├── package.json ├── readme.md ├── sindres-is-cool.png └── yarn.lock /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | module.exports = function() { 4 | return (navigator.languages && navigator.languages[0]) || 5 | navigator.language || 6 | navigator.userLanguage 7 | } 8 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Bu Kinoshita (https://bukinoshita.io) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "detect-browser-language", 3 | "version": "0.0.2", 4 | "description": "Detect browser language", 5 | "main": "index.js", 6 | "repository": "https://github.com/bukinoshita/detect-browser-language.git", 7 | "author": "Bu Kinoshita ", 8 | "license": "MIT", 9 | "keywords": [ 10 | "browser", 11 | "language", 12 | "intl", 13 | "detect", 14 | "detection", 15 | "translate", 16 | "translation" 17 | ] 18 | } 19 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # detect-browser-language 2 | 3 | > Detect browser language 4 | 5 | 6 | ## Install 7 | 8 | ```bash 9 | $ yarn add detect-browser-language 10 | ``` 11 | 12 | 13 | ## Browser support 14 | 15 | - Chrome 16 | - Firefox 17 | - IE 18 | - Edge 19 | - Opera 20 | - Safari 21 | 22 | 23 | ## Usage 24 | 25 | ```js 26 | // without SSR (server-side rendering) — React example 27 | import React from 'react' 28 | import detectBrowserLanguage from 'detect-browser-language' 29 | 30 | export default () => 31 |

{detectBrowserLanguage()}

32 | ``` 33 | 34 | ```js 35 | // with SSR (server-side rendering) — React example 36 | import React, { Component } from 'react' 37 | 38 | export default class Page Component { 39 | componentDidMount() { 40 | const detectBrowserLanguage = require('detect-browser-language') 41 | this.setState({ userLanguage: detectBrowserLanguage() }) 42 | } 43 | 44 | render() { 45 | const { userLanguage } = this.state 46 | 47 | return

{userLanguage}

48 | } 49 | } 50 | ``` 51 | 52 | _**PS:** Works with other libraries as well._ 53 | 54 | 55 | ## Why 56 | 57 | If you have a website that has `intl` implemented use this library to get user preferred language instead trying to guess the language based on their geolocation. I might be in France but that does't mean I speak/read french. 58 | 59 | 60 | 61 | 62 | ## License 63 | 64 | MIT © [Bu Kinoshita](https://bukinoshita.io) 65 | -------------------------------------------------------------------------------- /sindres-is-cool.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bukinoshita/detect-browser-language/c36e198b7a6f8c99e5ac839e31bf85c4c51d42b6/sindres-is-cool.png -------------------------------------------------------------------------------- /yarn.lock: -------------------------------------------------------------------------------- 1 | # THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. 2 | # yarn lockfile v1 3 | 4 | 5 | --------------------------------------------------------------------------------