Skip to content

Commit 0e15223

Browse files
committed
Initial commit
0 parents  commit 0e15223

11 files changed

Lines changed: 258 additions & 0 deletions

File tree

.eslintrc.json

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"parser": "@typescript-eslint/parser",
3+
"env": {
4+
"browser": true,
5+
"node": true
6+
},
7+
"plugins": [
8+
"prettier",
9+
"@typescript-eslint",
10+
"jest",
11+
"import"
12+
],
13+
"extends": [
14+
"prettier",
15+
"plugin:prettier/recommended",
16+
"plugin:@typescript-eslint/recommended",
17+
"plugin:import/typescript"
18+
],
19+
"parserOptions": {
20+
"sourceType": "module",
21+
"ecmaVersion": 2018
22+
},
23+
"rules": {
24+
"import/namespace": "off",
25+
"import/order": [
26+
"error"
27+
],
28+
"no-unused-vars": "off",
29+
"@typescript-eslint/no-unused-vars": "error",
30+
"@typescript-eslint/member-delimiter-style": "off",
31+
"@typescript-eslint/explicit-function-return-type": "off"
32+
},
33+
"overrides": [
34+
{
35+
// enable the rule specifically for TypeScript files
36+
"files": [
37+
"*.ts",
38+
"*.tsx"
39+
],
40+
"rules": {
41+
"@typescript-eslint/explicit-function-return-type": [
42+
"error"
43+
]
44+
}
45+
}
46+
]
47+
}

.githooks/pre-commit

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
3+
set -e
4+
set -o pipefail
5+
6+
npm run lint

.github/workflows/main.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: Publish Package
2+
on:
3+
push:
4+
tags:
5+
- 'v*'
6+
jobs:
7+
build:
8+
runs-on: ubuntu-latest
9+
steps:
10+
- uses: actions/checkout@v2
11+
- uses: actions/create-release@v1
12+
env:
13+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
14+
with:
15+
tag_name: ${{ github.ref }}
16+
release_name: Release ${{ github.ref }}
17+
draft: false
18+
prerelease: false
19+
- uses: actions/setup-node@v1
20+
with:
21+
node-version: '12.x'
22+
registry-url: 'https://registry.npmjs.org'
23+
- run: npm install
24+
- run: npm publish
25+
env:
26+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Basic
2+
.DS_Store
3+
4+
# IDE
5+
/.idea
6+
/.vscode
7+
8+
# Project
9+
/package-lock.json
10+
/node_modules
11+
/dist

.prettierrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"singleQuote": true,
3+
"semi": false
4+
}

jestconfig.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"transform": {
3+
"^.+\\.(t|j)sx?$": "ts-jest"
4+
},
5+
"testRegex": "(/__tests?__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
6+
"moduleFileExtensions": ["ts", "tsx", "js", "jsx", "json", "node"]
7+
}

license.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright About Bits GmbH
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

package.json

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
{
2+
"name": "@aboutbits/internationalization",
3+
"version": "0.1.0",
4+
"description": "Internationalization",
5+
"keywords": [
6+
"internationalization",
7+
"language",
8+
"languages",
9+
"browser detection"
10+
],
11+
"author": "AboutBits",
12+
"license": "MIT",
13+
"private": false,
14+
"main": "dist/index.js",
15+
"typings": "dist/index.d.ts",
16+
"scripts": {
17+
"build": "tsc",
18+
"lint": "eslint --ext js,ts,tsx src",
19+
"lint:fix": "npm run lint -- --fix",
20+
"test": "jest --config jestconfig.json",
21+
"prepare": "npm run build",
22+
"prepublishOnly": "npm test && npm run lint",
23+
"version": "npm run lint:fix && git add -A src",
24+
"postversion": "git push && git push --tags"
25+
},
26+
"repository": {
27+
"type": "git",
28+
"url": "git+https://github.com/aboutbits/internationalization.git"
29+
},
30+
"files": [
31+
"readme.md",
32+
"dist/**/*"
33+
],
34+
"publishConfig": {
35+
"access": "public"
36+
},
37+
"bugs": {
38+
"url": "https://github.com/aboutbits/internationalization/issues"
39+
},
40+
"homepage": "https://github.com/aboutbits/internationalization#readme",
41+
"devDependencies": {
42+
"@types/jest": "^26.0.9",
43+
"@typescript-eslint/eslint-plugin": "^3.8.0",
44+
"@typescript-eslint/parser": "^3.8.0",
45+
"eslint": "^7.6.0",
46+
"eslint-config-prettier": "^6.11.0",
47+
"eslint-plugin-import": "^2.22.0",
48+
"eslint-plugin-jest": "^23.20.0",
49+
"eslint-plugin-prettier": "^3.1.4",
50+
"jest": "^26.2.2",
51+
"prettier": "^2.0.5",
52+
"ts-jest": "^26.1.4",
53+
"typescript": "^3.9.7"
54+
}
55+
}

readme.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
Internationalization
2+
====================
3+
4+
This package includes utilities for working with different languages in the browser.
5+
6+
## Table of content
7+
8+
- [Usage](#usage)
9+
- [Build & Publish](#build--publish)
10+
- [Information](#information)
11+
12+
## Usage
13+
14+
First, you have to install the package:
15+
16+
```bash
17+
npm install @aboutbits/internationalization
18+
```
19+
20+
Second, you can setup the tool with all supported languages and the fallback language and then detect the given browser language.
21+
22+
```js
23+
import Internationalization from '@aboutbits/internationalization'
24+
25+
let supportedLanguages = ['en', 'de', 'it']
26+
let fallbackLanguage = 'en'
27+
28+
let i18n = new Internationalization(supportedLanguages, fallbackLanguage)
29+
30+
let language = i18n.detectBrowserLanguage()
31+
32+
console.log(language)
33+
```
34+
35+
## Build & Publish
36+
37+
To build and publish the package, simply commit all changes and push them to master. Then run one of the following commands locally:
38+
39+
```bash
40+
npm version patch
41+
npm version minor
42+
npm version major
43+
```
44+
45+
## Information
46+
47+
About Bits is a company based in South Tyrol, Italy. You can find more information about us on [our website](https://aboutbits.it).
48+
49+
### Support
50+
51+
For support, please contact [info@aboutbits.it](mailto:info@aboutbits.it).
52+
53+
### Credits
54+
55+
- [Alex Lanz](https://github.com/alexlanz)
56+
- [Martin Malfertheiner](https://github.com/mmalfertheiner)
57+
- [All Contributors](../../contributors)
58+
59+
### License
60+
61+
The MIT License (MIT). Please see the [license file](license.md) for more information.

src/index.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Internationalization<T extends string> {
2+
supportedLanguages: T[]
3+
fallbackLanguage: T
4+
5+
constructor(supportedLanguages: T[], fallbackLanguage: T) {
6+
this.supportedLanguages = supportedLanguages
7+
this.fallbackLanguage = fallbackLanguage
8+
}
9+
10+
detectBrowserLanguage(): T {
11+
const browserLanguage =
12+
(navigator.languages && navigator.languages[0]) || navigator.language
13+
14+
return (
15+
this.supportedLanguages.find((lang) => lang === browserLanguage) ||
16+
this.fallbackLanguage
17+
)
18+
}
19+
}
20+
21+
export { Internationalization as default }

0 commit comments

Comments
 (0)