@wxn0brp/falcon-frame-lang 0.0.2 → 0.0.3
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/README.md +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.js +53 -0
- package/dist/lang.d.ts +6 -0
- package/dist/lang.js +24 -0
- package/dist/types.d.ts +10 -0
- package/dist/types.js +1 -0
- package/package.json +46 -48
package/README.md
CHANGED
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { renderHTML } from "@wxn0brp/falcon-frame";
|
|
2
|
+
import { getLang, getLangData } from "./lang.js";
|
|
3
|
+
export function createLangRouter(config) {
|
|
4
|
+
config = {
|
|
5
|
+
dir: "public",
|
|
6
|
+
layout: "public/layout.html",
|
|
7
|
+
langDir: "public/lang",
|
|
8
|
+
...config
|
|
9
|
+
};
|
|
10
|
+
if (!config.dir.endsWith("/"))
|
|
11
|
+
config.dir += "/";
|
|
12
|
+
if (!config.langDir.endsWith("/"))
|
|
13
|
+
config.langDir += "/";
|
|
14
|
+
return (req, res, next) => {
|
|
15
|
+
const name = req.params.name;
|
|
16
|
+
const meta = config.meta[name] || config.meta["*"];
|
|
17
|
+
if (!meta)
|
|
18
|
+
return next();
|
|
19
|
+
const lang = getLang(req);
|
|
20
|
+
const langData = getLangData(req, config, lang);
|
|
21
|
+
const mapLangData = Object.keys(langData).reduce((acc, key) => {
|
|
22
|
+
const _key = "t_" + key.replaceAll("-", "_").replaceAll(".", "_").replaceAll(" ", "_");
|
|
23
|
+
acc[_key] = langData[key];
|
|
24
|
+
return acc;
|
|
25
|
+
}, {});
|
|
26
|
+
const dataObj = {
|
|
27
|
+
title: meta.title || name,
|
|
28
|
+
lang,
|
|
29
|
+
name,
|
|
30
|
+
...(mapLangData || {}),
|
|
31
|
+
...(config.getSpecific?.(name) || {})
|
|
32
|
+
};
|
|
33
|
+
const main = renderHTML(config.dir + name + ".html", dataObj);
|
|
34
|
+
let html = renderHTML(config.layout, { body: main, ...dataObj });
|
|
35
|
+
html = translateHtml(html, langData, lang);
|
|
36
|
+
res.ct("text/html; charset=utf-8");
|
|
37
|
+
res.end(html);
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
function translateHtml(html, langData, lang = "en") {
|
|
41
|
+
html = html.replace(/<([^>]*?)\s+translate\s*=\s*["']([^"']+)["']([^>]*?)>(.*?)<\/[^>]*?>/gs, (fullMatch, beforeTranslate, translateValue, afterTranslate) => {
|
|
42
|
+
let translatedText = langData[translateValue] || translateValue;
|
|
43
|
+
return `<${beforeTranslate} translate="${translateValue}" ${afterTranslate}>${translatedText}</${fullMatch.match(/<\/(\w+)/)[1]}>`;
|
|
44
|
+
});
|
|
45
|
+
html = html.replace(/(<[^>]*?)\s+translate-([a-z-]+)=["']([^"']+)["']([^>]*>)/gi, (_, beforeTag, attrName, attrValue, afterTag) => {
|
|
46
|
+
const translatedValue = langData[attrValue] || attrValue;
|
|
47
|
+
return `${beforeTag} translate-${attrName}="${attrValue}" ${attrName}="${translatedValue}"${afterTag}`;
|
|
48
|
+
});
|
|
49
|
+
html = html.replace(/<html\s+lang=["']([^"']+)["']>/gi, () => {
|
|
50
|
+
return `<html lang="${lang}">`;
|
|
51
|
+
});
|
|
52
|
+
return html;
|
|
53
|
+
}
|
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,24 @@
|
|
|
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 (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
|
+
cache.set(lang, data);
|
|
19
|
+
return data;
|
|
20
|
+
}
|
|
21
|
+
catch { }
|
|
22
|
+
}
|
|
23
|
+
return {};
|
|
24
|
+
}
|
package/dist/types.d.ts
ADDED
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.0.3",
|
|
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.5.3",
|
|
27
|
+
"tsc-alias": "*",
|
|
28
|
+
"typescript": "*"
|
|
29
|
+
},
|
|
30
|
+
"peerDependencies": {
|
|
31
|
+
"@wxn0brp/ac": ">=0.0.3",
|
|
32
|
+
"@wxn0brp/falcon-frame": ">=0.5.3"
|
|
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
|
+
}
|