@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 CHANGED
@@ -17,7 +17,7 @@ This package provides easy-to-use internationalization (i18n) functionality for
17
17
  ## Installation
18
18
 
19
19
  ```bash
20
- yarn add github:wxn0brP/FalconFrame-lang#dist
20
+ npm i @wxn0brp/falcon-frame-lang
21
21
  ```
22
22
 
23
23
  ## Usage
@@ -0,0 +1,3 @@
1
+ import { RouteHandler } from "@wxn0brp/falcon-frame";
2
+ import { Config } from "./types.js";
3
+ export declare function createLangRouter(config: Config): RouteHandler;
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
+ }
@@ -0,0 +1,10 @@
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) => Record<string, any>;
10
+ }
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,51 +1,49 @@
1
1
  {
2
- "name": "@wxn0brp/falcon-frame-lang",
3
- "version": "0.0.2",
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"
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
- "homepage": "https://github.com/wxn0brP/FalconFrame",
12
- "author": "wxn0brP",
13
- "license": "MIT",
14
- "type": "module",
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
+ }