@wxn0brp/falcon-frame-lang 0.0.2 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +8 -3
- package/dist/index.d.ts +3 -0
- package/dist/index.js +58 -0
- package/dist/lang.d.ts +6 -0
- package/dist/lang.js +25 -0
- package/dist/types.d.ts +11 -0
- package/dist/types.js +1 -0
- package/package.json +46 -48
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -17,7 +17,7 @@ This package provides easy-to-use internationalization (i18n) functionality for
|
|
|
17
17
|
## Installation
|
|
18
18
|
|
|
19
19
|
```bash
|
|
20
|
-
|
|
20
|
+
npm i @wxn0brp/falcon-frame-lang @wxn0brp/falcon-frame
|
|
21
21
|
```
|
|
22
22
|
|
|
23
23
|
## Usage
|
|
@@ -26,22 +26,27 @@ yarn add github:wxn0brP/FalconFrame-lang#dist
|
|
|
26
26
|
|
|
27
27
|
```typescript
|
|
28
28
|
import { createLangRouter } from "@wxn0brp/falcon-frame-lang";
|
|
29
|
+
import { FalconFrame } from "@wxn0brp/falcon-frame";
|
|
30
|
+
|
|
31
|
+
const app = new FalconFrame();
|
|
29
32
|
|
|
30
33
|
const config = {
|
|
31
34
|
meta: {
|
|
32
35
|
"home": { title: "Home Page" },
|
|
33
36
|
"about": { title: "About Us" }
|
|
34
|
-
},
|
|
37
|
+
}, // Meta data for specific pages
|
|
35
38
|
dir: "public", // Directory containing HTML files (default: "public")
|
|
36
39
|
layout: "public/layout.html", // Layout file path (default: "public/layout.html")
|
|
37
40
|
langDir: "public/lang", // Directory containing language JSON files (default: "public/lang")
|
|
38
|
-
|
|
41
|
+
disableCache: false, // Disable caching of language data
|
|
42
|
+
getSpecific: async (name: string) => {
|
|
39
43
|
// Return specific data for the given page name
|
|
40
44
|
return {};
|
|
41
45
|
}
|
|
42
46
|
};
|
|
43
47
|
|
|
44
48
|
const langRouter = createLangRouter(config);
|
|
49
|
+
app.use("/:name", langRouter);
|
|
45
50
|
```
|
|
46
51
|
|
|
47
52
|
### Language Files
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { renderHTML } from "@wxn0brp/falcon-frame";
|
|
2
|
+
import { getLang, getLangData } from "./lang.js";
|
|
3
|
+
export function createLangRouter(cfg) {
|
|
4
|
+
const config = {
|
|
5
|
+
dir: "public",
|
|
6
|
+
layout: "public/layout.html",
|
|
7
|
+
langDir: "public/lang",
|
|
8
|
+
disableCache: false,
|
|
9
|
+
meta: undefined,
|
|
10
|
+
getSpecific: () => ({}),
|
|
11
|
+
...cfg
|
|
12
|
+
};
|
|
13
|
+
if (!config.dir.endsWith("/"))
|
|
14
|
+
config.dir += "/";
|
|
15
|
+
if (!config.langDir.endsWith("/"))
|
|
16
|
+
config.langDir += "/";
|
|
17
|
+
return async (req, res, next) => {
|
|
18
|
+
const name = req.params.name;
|
|
19
|
+
let meta = {};
|
|
20
|
+
if (config.meta) {
|
|
21
|
+
meta = config.meta[name] || config.meta["*"];
|
|
22
|
+
if (!meta)
|
|
23
|
+
return next();
|
|
24
|
+
}
|
|
25
|
+
const lang = getLang(req);
|
|
26
|
+
const langData = getLangData(req, config, lang);
|
|
27
|
+
const mapLangData = Object.keys(langData).reduce((acc, key) => {
|
|
28
|
+
const _key = "t_" + key.replaceAll("-", "_").replaceAll(".", "_").replaceAll(" ", "_");
|
|
29
|
+
acc[_key] = langData[key];
|
|
30
|
+
return acc;
|
|
31
|
+
}, {});
|
|
32
|
+
const dataObj = {
|
|
33
|
+
lang,
|
|
34
|
+
name,
|
|
35
|
+
...(mapLangData || {}),
|
|
36
|
+
...(await config.getSpecific?.(name) || {})
|
|
37
|
+
};
|
|
38
|
+
if (meta?.title)
|
|
39
|
+
dataObj.title = meta.title;
|
|
40
|
+
const html = renderHTML(config.dir + name + ".html", dataObj, [], res.FF);
|
|
41
|
+
res.ct("text/html; charset=utf-8");
|
|
42
|
+
res.end(translateHtml(html, langData, lang));
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
function translateHtml(html, langData, lang = "en") {
|
|
46
|
+
html = html.replace(/<([^>]*?)\s+translate\s*=\s*["']([^"']+)["']([^>]*?)>(.*?)<\/[^>]*?>/gs, (fullMatch, beforeTranslate, translateValue, afterTranslate) => {
|
|
47
|
+
let translatedText = langData[translateValue] || translateValue;
|
|
48
|
+
return `<${beforeTranslate} translate="${translateValue}" ${afterTranslate}>${translatedText}</${fullMatch.match(/<\/(\w+)/)[1]}>`;
|
|
49
|
+
});
|
|
50
|
+
html = html.replace(/(<[^>]*?)\s+translate-([a-z-]+)=["']([^"']+)["']([^>]*>)/gi, (_, beforeTag, attrName, attrValue, afterTag) => {
|
|
51
|
+
const translatedValue = langData[attrValue] || attrValue;
|
|
52
|
+
return `${beforeTag} translate-${attrName}="${attrValue}" ${attrName}="${translatedValue}"${afterTag}`;
|
|
53
|
+
});
|
|
54
|
+
html = html.replace(/<html\s+lang=["']([^"']+)["']>/gi, () => {
|
|
55
|
+
return `<html lang="${lang}">`;
|
|
56
|
+
});
|
|
57
|
+
return html;
|
|
58
|
+
}
|
package/dist/lang.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { AnotherCache } from "@wxn0brp/ac";
|
|
2
|
+
import { FFRequest } from "@wxn0brp/falcon-frame";
|
|
3
|
+
import { Config } from "./types.js";
|
|
4
|
+
export declare const cache: AnotherCache<any, string>;
|
|
5
|
+
export declare function getLang(req: FFRequest): string;
|
|
6
|
+
export declare function getLangData(req: FFRequest, config: Config, lang?: string): Record<string, string>;
|
package/dist/lang.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { AnotherCache } from "@wxn0brp/ac";
|
|
2
|
+
import { existsSync, readFileSync } from "fs";
|
|
3
|
+
export const cache = new AnotherCache();
|
|
4
|
+
export function getLang(req) {
|
|
5
|
+
let lang = req.query.lang || req.cookies.lang || req.headers["accept-language"];
|
|
6
|
+
if (!lang)
|
|
7
|
+
return "en";
|
|
8
|
+
lang = lang.split(",")[0].split(";")[0].split("-")[0];
|
|
9
|
+
return lang;
|
|
10
|
+
}
|
|
11
|
+
export function getLangData(req, config, lang = getLang(req)) {
|
|
12
|
+
if (!config.disableCache && cache.has(lang))
|
|
13
|
+
return cache.get(lang);
|
|
14
|
+
const path = config.langDir + lang + ".json";
|
|
15
|
+
if (existsSync(path)) {
|
|
16
|
+
try {
|
|
17
|
+
const data = JSON.parse(readFileSync(path, "utf-8"));
|
|
18
|
+
if (!config.disableCache)
|
|
19
|
+
cache.set(lang, data);
|
|
20
|
+
return data;
|
|
21
|
+
}
|
|
22
|
+
catch { }
|
|
23
|
+
}
|
|
24
|
+
return {};
|
|
25
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export interface Config {
|
|
2
|
+
meta: Record<string, {
|
|
3
|
+
title: string;
|
|
4
|
+
[key: string]: any;
|
|
5
|
+
}>;
|
|
6
|
+
dir: string;
|
|
7
|
+
layout: string;
|
|
8
|
+
langDir: string;
|
|
9
|
+
getSpecific: (name: string) => Promise<Record<string, any>> | Record<string, any>;
|
|
10
|
+
disableCache: boolean;
|
|
11
|
+
}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,51 +1,49 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
2
|
+
"name": "@wxn0brp/falcon-frame-lang",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"main": "dist/index.js",
|
|
5
|
+
"types": "dist/index.d.ts",
|
|
6
|
+
"description": "FalconFrame language middleware for multi-language support",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/wxn0brP/FalconFrame-lang.git"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/wxn0brP/FalconFrame",
|
|
12
|
+
"author": "wxn0brP",
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"type": "module",
|
|
15
|
+
"keywords": [
|
|
16
|
+
"falcon-frame",
|
|
17
|
+
"i18n",
|
|
18
|
+
"localization",
|
|
19
|
+
"translation",
|
|
20
|
+
"lang",
|
|
21
|
+
"language"
|
|
22
|
+
],
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"@types/node": "*",
|
|
25
|
+
"@wxn0brp/ac": "^0.0.3",
|
|
26
|
+
"@wxn0brp/falcon-frame": "^0.7.0",
|
|
27
|
+
"tsc-alias": "*",
|
|
28
|
+
"typescript": "*"
|
|
29
|
+
},
|
|
30
|
+
"peerDependencies": {
|
|
31
|
+
"@wxn0brp/ac": ">=0.0.3",
|
|
32
|
+
"@wxn0brp/falcon-frame": ">=0.7.0"
|
|
33
|
+
},
|
|
34
|
+
"files": [
|
|
35
|
+
"dist"
|
|
36
|
+
],
|
|
37
|
+
"exports": {
|
|
38
|
+
".": {
|
|
39
|
+
"types": "./dist/index.d.ts",
|
|
40
|
+
"import": "./dist/index.js",
|
|
41
|
+
"default": "./dist/index.js"
|
|
10
42
|
},
|
|
11
|
-
"
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
"scripts": {
|
|
16
|
-
"build": "tsc && tsc-alias"
|
|
17
|
-
},
|
|
18
|
-
"keywords": [
|
|
19
|
-
"falcon-frame",
|
|
20
|
-
"i18n",
|
|
21
|
-
"localization",
|
|
22
|
-
"translation",
|
|
23
|
-
"lang",
|
|
24
|
-
"language"
|
|
25
|
-
],
|
|
26
|
-
"devDependencies": {
|
|
27
|
-
"@types/node": "*",
|
|
28
|
-
"@wxn0brp/ac": "^0.0.2",
|
|
29
|
-
"@wxn0brp/falcon-frame": "^0.4.0",
|
|
30
|
-
"typescript": "*"
|
|
31
|
-
},
|
|
32
|
-
"peerDependencies": {
|
|
33
|
-
"@wxn0brp/ac": ">=0.0.2",
|
|
34
|
-
"@wxn0brp/falcon-frame": ">=0.4.0"
|
|
35
|
-
},
|
|
36
|
-
"files": [
|
|
37
|
-
"dist"
|
|
38
|
-
],
|
|
39
|
-
"exports": {
|
|
40
|
-
".": {
|
|
41
|
-
"types": "./dist/index.d.ts",
|
|
42
|
-
"import": "./dist/index.js",
|
|
43
|
-
"default": "./dist/index.js"
|
|
44
|
-
},
|
|
45
|
-
"./*": {
|
|
46
|
-
"types": "./dist/*.d.ts",
|
|
47
|
-
"import": "./dist/*.js",
|
|
48
|
-
"default": "./dist/*.js"
|
|
49
|
-
}
|
|
43
|
+
"./*": {
|
|
44
|
+
"types": "./dist/*.d.ts",
|
|
45
|
+
"import": "./dist/*.js",
|
|
46
|
+
"default": "./dist/*.js"
|
|
50
47
|
}
|
|
51
|
-
}
|
|
48
|
+
}
|
|
49
|
+
}
|