Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
PipecraftNet committed Aug 17, 2023
1 parent f5ec672 commit 8903073
Show file tree
Hide file tree
Showing 12 changed files with 3,607 additions and 1 deletion.
9 changes: 9 additions & 0 deletions .editorconfig
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
18 changes: 18 additions & 0 deletions .github/workflows/npm-publish.yml
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 }}
26 changes: 26 additions & 0 deletions .gitignore
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
4 changes: 4 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules/**
LICENSE
pnpm-lock.yaml
tsconfig.tsbuildinfo
13 changes: 13 additions & 0 deletions .prettierrc.cjs
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,
}
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2023 UTags
Copyright (c) 2023 Pipecraft

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
22 changes: 22 additions & 0 deletions README.md
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)
61 changes: 61 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
type I18nMessageMap = Record<string, string>

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)
let messagesDefault: I18nMessageMap | undefined
let messagesLocal: I18nMessageMap | 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
}

// zh-HK will match zh if zh-HK is not provided
if (langs.has(language2) && !messagesLocal) {
messagesLocal = value
}

if (langs.has("en")) {
messagesDefault = value
}

if (langs.has("en-us") && !messagesDefault) {
messagesDefault = value
}
}

if (!messagesLocal) {
messagesLocal = {}
}

if (!messagesDefault || messagesDefault === messagesLocal) {
messagesDefault = {}
}

return function (key: string, ...parameters: string[] | number[]) {
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
}
}
58 changes: 58 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"name": "browser-extension-i18n",
"version": "0.0.5",
"description": "I18N module for developing browser extensions and userscripts",
"type": "module",
"main": "./lib/index.js",
"exports": {
".": "./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
}
}
}
Loading

0 comments on commit 8903073

Please sign in to comment.