@wxn0brp/falcon-frame-lang 0.0.2
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 +21 -0
- package/README.md +100 -0
- package/package.json +51 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 wxn0brP
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
# @wxn0brp/falcon-frame-lang
|
|
2
|
+
|
|
3
|
+
A localization middleware for the FalconFrame framework that enables multi-language support for web applications.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
This package provides easy-to-use internationalization (i18n) functionality for FalconFrame applications. It allows developers to create multi-language websites by automatically translating content based on user preferences and language files.
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
- **Automatic Language Detection**: Detects user"s language preference from cookies or the `Accept-Language` header
|
|
12
|
+
- **HTML Translation**: Translates HTML content by finding and replacing translation keys
|
|
13
|
+
- **Flexible Configuration**: Customizable directory paths and translation patterns
|
|
14
|
+
- **Caching**: Includes caching mechanism to improve performance
|
|
15
|
+
- **Attribute-based Translations**: Supports translation via HTML attributes as well as content
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
yarn add github:wxn0brP/FalconFrame-lang#dist
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
### Basic Setup
|
|
26
|
+
|
|
27
|
+
```typescript
|
|
28
|
+
import { createLangRouter } from "@wxn0brp/falcon-frame-lang";
|
|
29
|
+
|
|
30
|
+
const config = {
|
|
31
|
+
meta: {
|
|
32
|
+
"home": { title: "Home Page" },
|
|
33
|
+
"about": { title: "About Us" }
|
|
34
|
+
},
|
|
35
|
+
dir: "public", // Directory containing HTML files (default: "public")
|
|
36
|
+
layout: "public/layout.html", // Layout file path (default: "public/layout.html")
|
|
37
|
+
langDir: "public/lang", // Directory containing language JSON files (default: "public/lang")
|
|
38
|
+
getSpecific: (name: string) => {
|
|
39
|
+
// Return specific data for the given page name
|
|
40
|
+
return {};
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const langRouter = createLangRouter(config);
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### Language Files
|
|
48
|
+
|
|
49
|
+
Create language files in the `langDir` (by default `public/lang/`) with the format `{langCode}.json`:
|
|
50
|
+
|
|
51
|
+
```json
|
|
52
|
+
{
|
|
53
|
+
"about us": "About Us",
|
|
54
|
+
"contact": "Contact",
|
|
55
|
+
"image_alt_text": "Image Alt Text",
|
|
56
|
+
"welcome": "Welcome to our website"
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### Translating HTML
|
|
61
|
+
|
|
62
|
+
The middleware translates HTML in three ways:
|
|
63
|
+
|
|
64
|
+
1. **Content-based translation**: Text content inside HTML tags
|
|
65
|
+
```html
|
|
66
|
+
<h1 translate="about us"></h1> <!-- Will be translated to "About Us" -->
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
2. **Attribute-based translation**: Using `translate-*` attributes
|
|
70
|
+
```html
|
|
71
|
+
<input type="text" translate-placeholder="contact us" /> <!-- Will add translated placeholder attribute -->
|
|
72
|
+
<img src="image.jpg" translate-alt="image_alt_text"> <!-- Will translate the alt attribute -->
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
3. **Translate attribute**: Using the `translate` attribute for direct element translation
|
|
76
|
+
```html
|
|
77
|
+
<h1 translate="welcome"></h1> <!-- Will translate the content to "Welcome to our website" -->
|
|
78
|
+
<button translate="contact"></button> <!-- Will translate the content to "Contact" -->
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### Language Detection
|
|
82
|
+
|
|
83
|
+
The package detects the user"s preferred language in the following order:
|
|
84
|
+
1. `lang` query parameter
|
|
85
|
+
2. `lang` cookie value
|
|
86
|
+
3. `Accept-Language` header from the request
|
|
87
|
+
4. Defaults to "en" if no language is detected
|
|
88
|
+
|
|
89
|
+
## Dependencies
|
|
90
|
+
|
|
91
|
+
- `@wxn0brp/ac`: For caching functionality
|
|
92
|
+
- `@wxn0brp/falcon-frame`: The main FalconFrame framework
|
|
93
|
+
|
|
94
|
+
## Performance & Caching
|
|
95
|
+
|
|
96
|
+
The middleware uses an efficient caching mechanism to improve performance. Language files are loaded once and cached in memory to prevent repeated file system operations. This results in faster response times for subsequent requests requiring the same language translations.
|
|
97
|
+
|
|
98
|
+
## License
|
|
99
|
+
|
|
100
|
+
MIT License.
|
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
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"
|
|
10
|
+
},
|
|
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
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|