From ac720ce6d0272d58fc0c899b2a6aa30f5b77217c Mon Sep 17 00:00:00 2001 From: Matt Date: Tue, 22 Feb 2022 15:03:32 +0000 Subject: [PATCH] (feat): Support "de-de" if user has "de" in their language list In the case the user has only a 2 letter string in their browser languages list, but we support only full locales, we will pick the first listed code that matches the first 2 letters. For example `de`, [`en-us`, 'de-de', 'de-as'] -> 'de-de' --- .github/workflows/ci.yml | 23 +++++++++++++++++++++++ package.json | 5 +++-- src/index.js | 2 +- test/index.test.js | 20 ++++++++++++++++++++ 4 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 .github/workflows/ci.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..e41a899 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,23 @@ +# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node +# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions +name: CI + +on: [push, pull_request] + +jobs: + test: + runs-on: ubuntu-latest + + strategy: + matrix: + node-version: [12.x, 14.x, 16.x] + # See supported Node.js release schedule at https://nodejs.org/en/about/releases/ + steps: + - uses: actions/checkout@v2 + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v2 + with: + node-version: ${{ matrix.node-version }} + cache: 'npm' + - run: yarn install --frozen-lockfile + - run: yarn test diff --git a/package.json b/package.json index 9bd3bd0..e348d00 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "browser-lang", - "version": "0.1.0", + "version": "0.2.0", "license": "MIT", "description": "", "repository": "github:wiziple/browser-lang", @@ -17,7 +17,8 @@ "main": "./dist/index.js", "scripts": { "clean": "rimraf dist", - "test": "jest test --watch", + "test": "jest test", + "test:watch": "jest test --watch", "build": "babel src --out-dir dist", "format": "prettier --write {src,test}/**/*.{js,jsx}" }, diff --git a/src/index.js b/src/index.js index 0dce96d..e20d372 100644 --- a/src/index.js +++ b/src/index.js @@ -57,7 +57,7 @@ function getPreferredLanguage(options) { // en == en_US const matchCodeOnly = languages.filter(lang => - startsWith(browserLanguage, lang) + startsWith(browserLanguage, lang) || startsWith(lang, browserLanguage) ) return matchCodeOnly[0] || fallback } diff --git a/test/index.test.js b/test/index.test.js index fa652b9..aea4c96 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -81,4 +81,24 @@ describe("browserLang", () => { } expect(browserLang(options)).toBe("zh") }) + + it('should return "de-de" when navigator.languages is ["de"] and "de-de" is one item in the list.', () => { + mockNavigator({ + languages: ["de"], + }) + const options = { + languages: ["en-us", "de-de"], + } + expect(browserLang(options)).toBe("de-de") + }) + + it('should return "de-de" when navigator.languages is ["de"] and "de-de" is first item in the list.', () => { + mockNavigator({ + languages: ["de"], + }) + const options = { + languages: ["en-us", "de-de", "de-as"], + } + expect(browserLang(options)).toBe("de-de") + }) })