@wxn0brp/falcon-frame-lang 0.0.3 → 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 +1 -1
- package/dist/index.js +18 -13
- package/dist/lang.js +3 -2
- package/dist/types.d.ts +5 -4
- package/package.json +3 -3
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
|
-
npm i @wxn0brp/falcon-frame-lang
|
|
20
|
+
npm i @wxn0brp/falcon-frame-lang @wxn0brp/falcon-frame
|
|
21
21
|
```
|
|
22
22
|
|
|
23
23
|
## Usage
|
|
@@ -26,22 +26,27 @@ npm i @wxn0brp/falcon-frame-lang
|
|
|
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
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,21 +1,27 @@
|
|
|
1
1
|
import { renderHTML } from "@wxn0brp/falcon-frame";
|
|
2
2
|
import { getLang, getLangData } from "./lang.js";
|
|
3
|
-
export function createLangRouter(
|
|
4
|
-
config = {
|
|
3
|
+
export function createLangRouter(cfg) {
|
|
4
|
+
const config = {
|
|
5
5
|
dir: "public",
|
|
6
6
|
layout: "public/layout.html",
|
|
7
7
|
langDir: "public/lang",
|
|
8
|
-
|
|
8
|
+
disableCache: false,
|
|
9
|
+
meta: undefined,
|
|
10
|
+
getSpecific: () => ({}),
|
|
11
|
+
...cfg
|
|
9
12
|
};
|
|
10
13
|
if (!config.dir.endsWith("/"))
|
|
11
14
|
config.dir += "/";
|
|
12
15
|
if (!config.langDir.endsWith("/"))
|
|
13
16
|
config.langDir += "/";
|
|
14
|
-
return (req, res, next) => {
|
|
17
|
+
return async (req, res, next) => {
|
|
15
18
|
const name = req.params.name;
|
|
16
|
-
|
|
17
|
-
if (
|
|
18
|
-
|
|
19
|
+
let meta = {};
|
|
20
|
+
if (config.meta) {
|
|
21
|
+
meta = config.meta[name] || config.meta["*"];
|
|
22
|
+
if (!meta)
|
|
23
|
+
return next();
|
|
24
|
+
}
|
|
19
25
|
const lang = getLang(req);
|
|
20
26
|
const langData = getLangData(req, config, lang);
|
|
21
27
|
const mapLangData = Object.keys(langData).reduce((acc, key) => {
|
|
@@ -24,17 +30,16 @@ export function createLangRouter(config) {
|
|
|
24
30
|
return acc;
|
|
25
31
|
}, {});
|
|
26
32
|
const dataObj = {
|
|
27
|
-
title: meta.title || name,
|
|
28
33
|
lang,
|
|
29
34
|
name,
|
|
30
35
|
...(mapLangData || {}),
|
|
31
|
-
...(config.getSpecific?.(name) || {})
|
|
36
|
+
...(await config.getSpecific?.(name) || {})
|
|
32
37
|
};
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
html =
|
|
38
|
+
if (meta?.title)
|
|
39
|
+
dataObj.title = meta.title;
|
|
40
|
+
const html = renderHTML(config.dir + name + ".html", dataObj, [], res.FF);
|
|
36
41
|
res.ct("text/html; charset=utf-8");
|
|
37
|
-
res.end(html);
|
|
42
|
+
res.end(translateHtml(html, langData, lang));
|
|
38
43
|
};
|
|
39
44
|
}
|
|
40
45
|
function translateHtml(html, langData, lang = "en") {
|
package/dist/lang.js
CHANGED
|
@@ -9,13 +9,14 @@ export function getLang(req) {
|
|
|
9
9
|
return lang;
|
|
10
10
|
}
|
|
11
11
|
export function getLangData(req, config, lang = getLang(req)) {
|
|
12
|
-
if (cache.has(lang))
|
|
12
|
+
if (!config.disableCache && cache.has(lang))
|
|
13
13
|
return cache.get(lang);
|
|
14
14
|
const path = config.langDir + lang + ".json";
|
|
15
15
|
if (existsSync(path)) {
|
|
16
16
|
try {
|
|
17
17
|
const data = JSON.parse(readFileSync(path, "utf-8"));
|
|
18
|
-
|
|
18
|
+
if (!config.disableCache)
|
|
19
|
+
cache.set(lang, data);
|
|
19
20
|
return data;
|
|
20
21
|
}
|
|
21
22
|
catch { }
|
package/dist/types.d.ts
CHANGED
|
@@ -3,8 +3,9 @@ export interface Config {
|
|
|
3
3
|
title: string;
|
|
4
4
|
[key: string]: any;
|
|
5
5
|
}>;
|
|
6
|
-
dir
|
|
7
|
-
layout
|
|
8
|
-
langDir
|
|
9
|
-
getSpecific
|
|
6
|
+
dir: string;
|
|
7
|
+
layout: string;
|
|
8
|
+
langDir: string;
|
|
9
|
+
getSpecific: (name: string) => Promise<Record<string, any>> | Record<string, any>;
|
|
10
|
+
disableCache: boolean;
|
|
10
11
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wxn0brp/falcon-frame-lang",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"description": "FalconFrame language middleware for multi-language support",
|
|
@@ -23,13 +23,13 @@
|
|
|
23
23
|
"devDependencies": {
|
|
24
24
|
"@types/node": "*",
|
|
25
25
|
"@wxn0brp/ac": "^0.0.3",
|
|
26
|
-
"@wxn0brp/falcon-frame": "^0.
|
|
26
|
+
"@wxn0brp/falcon-frame": "^0.7.0",
|
|
27
27
|
"tsc-alias": "*",
|
|
28
28
|
"typescript": "*"
|
|
29
29
|
},
|
|
30
30
|
"peerDependencies": {
|
|
31
31
|
"@wxn0brp/ac": ">=0.0.3",
|
|
32
|
-
"@wxn0brp/falcon-frame": ">=0.
|
|
32
|
+
"@wxn0brp/falcon-frame": ">=0.7.0"
|
|
33
33
|
},
|
|
34
34
|
"files": [
|
|
35
35
|
"dist"
|