-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f5ec672
commit 69ddfef
Showing
12 changed files
with
3,612 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
root = true | ||
|
||
[*] | ||
indent_style = space | ||
indent_size = 2 | ||
end_of_line = lf | ||
charset = utf-8 | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
name: NPM Publish | ||
on: | ||
push: | ||
tags: | ||
- "*.*.*" | ||
jobs: | ||
publish: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v1 | ||
- uses: actions/setup-node@v1 | ||
with: | ||
node-version: 14 | ||
- run: npm install | ||
- run: npm test | ||
- uses: JS-DevTools/npm-publish@v1 | ||
with: | ||
token: ${{ secrets.NPM_TOKEN }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. | ||
|
||
# dependencies | ||
/node_modules | ||
|
||
|
||
# misc | ||
.DS_Store | ||
._* | ||
|
||
# debug | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
.pnpm-debug.log* | ||
|
||
# local env files | ||
.env* | ||
|
||
out/ | ||
build/ | ||
dist/ | ||
|
||
# typescript | ||
.tsbuildinfo | ||
tsconfig.tsbuildinfo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
node_modules/** | ||
LICENSE | ||
pnpm-lock.yaml | ||
tsconfig.tsbuildinfo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/** | ||
* @type {import('prettier').Options} | ||
*/ | ||
module.exports = { | ||
printWidth: 80, | ||
tabWidth: 2, | ||
useTabs: false, | ||
semi: false, | ||
singleQuote: false, | ||
trailingComma: "es5", | ||
bracketSpacing: true, | ||
bracketSameLine: true, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,24 @@ | ||
# browser-extension-i18n | ||
|
||
I18N module for developing browser extensions and userscripts | ||
|
||
## Usage | ||
|
||
```bash | ||
npm i browser-extension-i18n | ||
# or | ||
pnpm add browser-extension-i18n | ||
# or | ||
yarn add browser-extension-i18n | ||
``` | ||
|
||
## License | ||
|
||
Copyright (c) 2023 [Pipecraft](https://www.pipecraft.net). Licensed under the [MIT License](LICENSE). | ||
|
||
## >\_ | ||
|
||
[![Pipecraft](https://img.shields.io/badge/site-pipecraft-brightgreen)](https://www.pipecraft.net) | ||
[![UTags](https://img.shields.io/badge/site-UTags-brightgreen)](https://utags.pipecraft.net) | ||
[![DTO](https://img.shields.io/badge/site-DTO-brightgreen)](https://dto.pipecraft.net) | ||
[![BestXTools](https://img.shields.io/badge/site-bestxtools-brightgreen)](https://www.bestxtools.com) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
type I18nMessageMap = Record<string, string> | ||
|
||
let messagesDefault: I18nMessageMap | undefined | ||
|
||
let messagesLocal: I18nMessageMap | ||
|
||
export function initI18n( | ||
messageMaps: Record<string, I18nMessageMap>, | ||
language?: string | ||
) { | ||
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing | ||
language = (language || navigator.language).toLowerCase() | ||
const language2 = language.slice(0, 2) | ||
messagesDefault = undefined | ||
|
||
for (const entry of Object.entries(messageMaps)) { | ||
const langs = new Set( | ||
entry[0] | ||
.toLowerCase() | ||
.split(",") | ||
.map((v) => v.trim()) | ||
) | ||
const value = entry[1] | ||
if (langs.has(language)) { | ||
messagesLocal = value | ||
} | ||
|
||
if (langs.has(language2) && !messagesLocal) { | ||
messagesLocal = value | ||
} | ||
|
||
if (langs.has("en")) { | ||
messagesDefault = value | ||
} | ||
|
||
if (langs.has("en-us") && !messagesDefault) { | ||
messagesDefault = value | ||
} | ||
} | ||
|
||
if (!messagesDefault || messagesDefault === messagesLocal) { | ||
messagesDefault = {} | ||
} | ||
} | ||
|
||
export function i18n(key: string, ...parameters: string[] | number[]) { | ||
if (!messagesLocal) { | ||
messagesLocal = {} | ||
} | ||
|
||
if (!messagesDefault) { | ||
messagesDefault = {} | ||
} | ||
|
||
let text: string = messagesLocal[key] || messagesDefault[key] || key | ||
if (parameters && parameters.length > 0 && text !== key) { | ||
// eslint-disable-next-line unicorn/no-for-loop | ||
for (let i = 0; i < parameters.length; i++) { | ||
text = text.replaceAll( | ||
new RegExp(`\\{${i + 1}\\}`, "g"), | ||
String(parameters[i]) | ||
) | ||
} | ||
} | ||
|
||
return text | ||
} | ||
|
||
export const i = i18n |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
{ | ||
"name": "browser-extension-i18n", | ||
"version": "0.0.2", | ||
"description": "I18N module for developing browser extensions and userscripts", | ||
"type": "module", | ||
"main": "./lib/index.js", | ||
"scripts": { | ||
"p": "prettier --write .", | ||
"lint": "prettier --write . && xo --fix && tsc --noemit", | ||
"test": "echo \"Error: no test specified\" && exit 0" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/utags/browser-extension-i18n.git" | ||
}, | ||
"keywords": [ | ||
"extensions", | ||
"userscripts", | ||
"utilities", | ||
"i18n" | ||
], | ||
"author": "Pipecraft", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/utags/browser-extension-i18n/issues" | ||
}, | ||
"homepage": "https://github.com/utags/browser-extension-i18n#readme", | ||
"devDependencies": { | ||
"@types/chrome": "^0.0.243", | ||
"prettier": "^3.0.2", | ||
"typescript": "^5.1.6", | ||
"xo": "^0.56.0" | ||
}, | ||
"files": [ | ||
"lib/", | ||
"LICENSE", | ||
"README.md" | ||
], | ||
"engines": { | ||
"node": "^14.13.1 || >=16.0.0" | ||
}, | ||
"xo": { | ||
"space": 2, | ||
"prettier": true, | ||
"globals": [ | ||
"window", | ||
"top", | ||
"document" | ||
], | ||
"rules": { | ||
"prefer-destructuring": 0, | ||
"capitalized-comments": 0 | ||
} | ||
} | ||
} |
Oops, something went wrong.