Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
PipecraftNet committed Aug 16, 2023
1 parent f5ec672 commit aeec6a8
Show file tree
Hide file tree
Showing 11 changed files with 3,570 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 }}
22 changes: 22 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# 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/
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules/**
LICENSE
pnpm-lock.yaml
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)
63 changes: 63 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
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()
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("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
55 changes: 55 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"name": "browser-extension-i18n",
"version": "0.0.1",
"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
}
}
}
Loading

0 comments on commit aeec6a8

Please sign in to comment.