crelte 0.5.14 → 0.5.15
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/dist/server/Request.d.ts +2 -2
- package/dist/server/Request.js +2 -2
- package/dist/static/index.d.ts +1 -1
- package/dist/translations/index.d.ts +2 -2
- package/dist/translations/index.d.ts.map +1 -1
- package/dist/translations/translationsPlugin.d.ts +4 -1
- package/dist/translations/translationsPlugin.d.ts.map +1 -1
- package/dist/translations/translationsPlugin.js +32 -2
- package/package.json +1 -1
- package/src/server/Request.ts +2 -2
- package/src/static/index.ts +1 -1
- package/src/translations/index.ts +2 -0
- package/src/translations/translationsPlugin.ts +46 -3
package/dist/server/Request.d.ts
CHANGED
|
@@ -24,8 +24,8 @@ export default class ServerRequest extends Request {
|
|
|
24
24
|
*
|
|
25
25
|
* @example
|
|
26
26
|
* ```js
|
|
27
|
-
* router.get('/blog/:slug', async
|
|
28
|
-
* return Response.json({ slug:
|
|
27
|
+
* router.get('/blog/:slug', async csr => {
|
|
28
|
+
* return Response.json({ slug: csr.req.getParam('slug') });
|
|
29
29
|
* });
|
|
30
30
|
* ```
|
|
31
31
|
*/
|
package/dist/server/Request.js
CHANGED
|
@@ -27,8 +27,8 @@ export default class ServerRequest extends Request {
|
|
|
27
27
|
*
|
|
28
28
|
* @example
|
|
29
29
|
* ```js
|
|
30
|
-
* router.get('/blog/:slug', async
|
|
31
|
-
* return Response.json({ slug:
|
|
30
|
+
* router.get('/blog/:slug', async csr => {
|
|
31
|
+
* return Response.json({ slug: csr.req.getParam('slug') });
|
|
32
32
|
* });
|
|
33
33
|
* ```
|
|
34
34
|
*/
|
package/dist/static/index.d.ts
CHANGED
|
@@ -8,7 +8,7 @@ import { createFileLoader } from './loader/fileLoader/fileLoader.js';
|
|
|
8
8
|
import { createGlobalLoader } from './loader/GlobalLoader.js';
|
|
9
9
|
import { type TranslationsLoader, type LoaderCreator } from './loader/index.js';
|
|
10
10
|
import loadTranslations from './loadTranslations.js';
|
|
11
|
-
import { createTranslations, getTranslationsPlugin, type TranslateFunction, type TranslateStore, type Translations, TranslationsPlugin, type TranslationsPluginOptions } from './translationsPlugin.js';
|
|
11
|
+
import { createTranslations, getTranslationsPlugin, type TranslateFunction, type TranslationVarValue, type TranslateStore, type Translations, TranslationsPlugin, type TranslationsPluginOptions } from './translationsPlugin.js';
|
|
12
12
|
/**
|
|
13
13
|
* Creates a translate store for the given namespace.
|
|
14
14
|
*
|
|
@@ -24,5 +24,5 @@ import { createTranslations, getTranslationsPlugin, type TranslateFunction, type
|
|
|
24
24
|
* ```
|
|
25
25
|
*/
|
|
26
26
|
declare function getTranslations(namespace?: string): TranslateStore;
|
|
27
|
-
export { createTranslations, TranslationsPluginOptions, loadTranslations, getTranslations, getTranslationsPlugin, Translations, TranslationsPlugin, TranslationsLoader, LoaderCreator, TranslateStore, TranslateFunction, createFileLoader, createGlobalLoader, };
|
|
27
|
+
export { createTranslations, TranslationsPluginOptions, loadTranslations, getTranslations, getTranslationsPlugin, Translations, TranslationsPlugin, TranslationsLoader, LoaderCreator, TranslateStore, TranslateFunction, TranslationVarValue, createFileLoader, createGlobalLoader, };
|
|
28
28
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/translations/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,gBAAgB,EAAE,MAAM,mCAAmC,CAAC;AACrE,OAAO,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAC;AAC9D,OAAO,EAAE,KAAK,kBAAkB,EAAE,KAAK,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAChF,OAAO,gBAAgB,MAAM,uBAAuB,CAAC;AACrD,OAAO,EACN,kBAAkB,EAClB,qBAAqB,EACrB,KAAK,iBAAiB,EACtB,KAAK,cAAc,EACnB,KAAK,YAAY,EACjB,kBAAkB,EAClB,KAAK,yBAAyB,EAC9B,MAAM,yBAAyB,CAAC;AAEjC;;;;;;;;;;;;;GAaG;AACH,iBAAS,eAAe,CAAC,SAAS,GAAE,MAAiB,GAAG,cAAc,CAGrE;AAED,OAAO,EACN,kBAAkB,EAClB,yBAAyB,EACzB,gBAAgB,EAChB,eAAe,EACf,qBAAqB,EACrB,YAAY,EACZ,kBAAkB,EAClB,kBAAkB,EAClB,aAAa,EACb,cAAc,EACd,iBAAiB,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/translations/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,gBAAgB,EAAE,MAAM,mCAAmC,CAAC;AACrE,OAAO,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAC;AAC9D,OAAO,EAAE,KAAK,kBAAkB,EAAE,KAAK,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAChF,OAAO,gBAAgB,MAAM,uBAAuB,CAAC;AACrD,OAAO,EACN,kBAAkB,EAClB,qBAAqB,EACrB,KAAK,iBAAiB,EACtB,KAAK,mBAAmB,EACxB,KAAK,cAAc,EACnB,KAAK,YAAY,EACjB,kBAAkB,EAClB,KAAK,yBAAyB,EAC9B,MAAM,yBAAyB,CAAC;AAEjC;;;;;;;;;;;;;GAaG;AACH,iBAAS,eAAe,CAAC,SAAS,GAAE,MAAiB,GAAG,cAAc,CAGrE;AAED,OAAO,EACN,kBAAkB,EAClB,yBAAyB,EACzB,gBAAgB,EAChB,eAAe,EACf,qBAAqB,EACrB,YAAY,EACZ,kBAAkB,EAClB,kBAAkB,EAClB,aAAa,EACb,cAAc,EACd,iBAAiB,EACjB,mBAAmB,EAEnB,gBAAgB,EAChB,kBAAkB,GAClB,CAAC"}
|
|
@@ -2,7 +2,8 @@ import { LoaderCreator } from './loader/index.js';
|
|
|
2
2
|
import { Plugin, PluginCreator } from '../plugins/Plugins.js';
|
|
3
3
|
import { Crelte, CrelteRequest } from '../crelte.js';
|
|
4
4
|
import Readable from '../std/stores/Readable.js';
|
|
5
|
-
export type TranslateFunction = (key: string) => string;
|
|
5
|
+
export type TranslateFunction = (key: string, vars?: Record<string, TranslationVarValue>) => string;
|
|
6
|
+
export type TranslationVarValue = string | number;
|
|
6
7
|
export type TranslateStore = Readable<TranslateFunction>;
|
|
7
8
|
export type Translations = Record<string, string>;
|
|
8
9
|
export type TranslationsPluginOptions = {
|
|
@@ -29,6 +30,8 @@ export declare class TranslationsPlugin implements Plugin {
|
|
|
29
30
|
get(lang: string, namespace: string): Translations | null;
|
|
30
31
|
/** @hidden */
|
|
31
32
|
z_createTranslateStore(namespace: string): TranslateStore;
|
|
33
|
+
/** @hidden */
|
|
34
|
+
z_resolveVariables(text: string, vars?: Record<string, TranslationVarValue>): string;
|
|
32
35
|
}
|
|
33
36
|
/**
|
|
34
37
|
* Create the TranslationsPlugin
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"translationsPlugin.d.ts","sourceRoot":"","sources":["../../src/translations/translationsPlugin.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAsB,MAAM,mBAAmB,CAAC;AACtE,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAC9D,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AACrD,OAAO,QAAQ,MAAM,2BAA2B,CAAC;AAGjD,MAAM,MAAM,iBAAiB,GAAG,CAC/B,GAAG,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"translationsPlugin.d.ts","sourceRoot":"","sources":["../../src/translations/translationsPlugin.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAsB,MAAM,mBAAmB,CAAC;AACtE,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAC9D,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AACrD,OAAO,QAAQ,MAAM,2BAA2B,CAAC;AAGjD,MAA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BAAqB,CACpC,MAAM,GAAE,MAAoB,GAC1B,kBAAkB,CAEpB"}
|
|
@@ -60,18 +60,48 @@ export class TranslationsPlugin {
|
|
|
60
60
|
/** @hidden */
|
|
61
61
|
z_createTranslateStore(namespace) {
|
|
62
62
|
const store = derived(this.crelte.router.site, site => {
|
|
63
|
-
return (key) => {
|
|
63
|
+
return (key, vars) => {
|
|
64
64
|
const lang = site?.language ?? this.firstLang;
|
|
65
65
|
if (!lang)
|
|
66
66
|
throw new Error('no lang');
|
|
67
67
|
const data = this.get(lang, namespace);
|
|
68
68
|
if (!data)
|
|
69
69
|
console.error(`namespace '${namespace}' not loaded`);
|
|
70
|
-
|
|
70
|
+
const text = data?.[key] || key;
|
|
71
|
+
if (!vars)
|
|
72
|
+
return text;
|
|
73
|
+
return this.z_resolveVariables(text, vars);
|
|
71
74
|
};
|
|
72
75
|
});
|
|
73
76
|
return new Readable(store);
|
|
74
77
|
}
|
|
78
|
+
/** @hidden */
|
|
79
|
+
z_resolveVariables(text, vars = {}) {
|
|
80
|
+
let result = '';
|
|
81
|
+
let index = 0;
|
|
82
|
+
while (index < text.length) {
|
|
83
|
+
const openIndex = text.indexOf('{', index);
|
|
84
|
+
if (openIndex === -1) {
|
|
85
|
+
result += text.slice(index);
|
|
86
|
+
break;
|
|
87
|
+
}
|
|
88
|
+
const closeIndex = text.indexOf('}', openIndex + 1);
|
|
89
|
+
if (closeIndex === -1) {
|
|
90
|
+
result += text.slice(index);
|
|
91
|
+
break;
|
|
92
|
+
}
|
|
93
|
+
result += text.slice(index, openIndex);
|
|
94
|
+
const key = text.slice(openIndex + 1, closeIndex);
|
|
95
|
+
if (key in vars) {
|
|
96
|
+
result += vars[key];
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
result += `{${key}}`;
|
|
100
|
+
}
|
|
101
|
+
index = closeIndex + 1;
|
|
102
|
+
}
|
|
103
|
+
return result;
|
|
104
|
+
}
|
|
75
105
|
}
|
|
76
106
|
/**
|
|
77
107
|
* Create the TranslationsPlugin
|
package/package.json
CHANGED
package/src/server/Request.ts
CHANGED
|
@@ -33,8 +33,8 @@ export default class ServerRequest extends Request {
|
|
|
33
33
|
*
|
|
34
34
|
* @example
|
|
35
35
|
* ```js
|
|
36
|
-
* router.get('/blog/:slug', async
|
|
37
|
-
* return Response.json({ slug:
|
|
36
|
+
* router.get('/blog/:slug', async csr => {
|
|
37
|
+
* return Response.json({ slug: csr.req.getParam('slug') });
|
|
38
38
|
* });
|
|
39
39
|
* ```
|
|
40
40
|
*/
|
package/src/static/index.ts
CHANGED
|
@@ -13,6 +13,7 @@ import {
|
|
|
13
13
|
createTranslations,
|
|
14
14
|
getTranslationsPlugin,
|
|
15
15
|
type TranslateFunction,
|
|
16
|
+
type TranslationVarValue,
|
|
16
17
|
type TranslateStore,
|
|
17
18
|
type Translations,
|
|
18
19
|
TranslationsPlugin,
|
|
@@ -50,6 +51,7 @@ export {
|
|
|
50
51
|
LoaderCreator,
|
|
51
52
|
TranslateStore,
|
|
52
53
|
TranslateFunction,
|
|
54
|
+
TranslationVarValue,
|
|
53
55
|
// loaders
|
|
54
56
|
createFileLoader,
|
|
55
57
|
createGlobalLoader,
|
|
@@ -7,8 +7,9 @@ import { getCrelte } from '../index.js';
|
|
|
7
7
|
|
|
8
8
|
export type TranslateFunction = (
|
|
9
9
|
key: string,
|
|
10
|
-
|
|
10
|
+
vars?: Record<string, TranslationVarValue>,
|
|
11
11
|
) => string;
|
|
12
|
+
export type TranslationVarValue = string | number;
|
|
12
13
|
export type TranslateStore = Readable<TranslateFunction>;
|
|
13
14
|
export type Translations = Record<string, string>;
|
|
14
15
|
export type TranslationsPluginOptions = {
|
|
@@ -89,18 +90,60 @@ export class TranslationsPlugin implements Plugin {
|
|
|
89
90
|
/** @hidden */
|
|
90
91
|
z_createTranslateStore(namespace: string): TranslateStore {
|
|
91
92
|
const store = derived(this.crelte.router.site, site => {
|
|
92
|
-
return (
|
|
93
|
+
return (
|
|
94
|
+
key: string,
|
|
95
|
+
vars?: Record<string, TranslationVarValue>,
|
|
96
|
+
) => {
|
|
93
97
|
const lang = site?.language ?? this.firstLang;
|
|
94
98
|
if (!lang) throw new Error('no lang');
|
|
95
99
|
|
|
96
100
|
const data = this.get(lang, namespace);
|
|
97
101
|
if (!data) console.error(`namespace '${namespace}' not loaded`);
|
|
98
|
-
|
|
102
|
+
|
|
103
|
+
const text = data?.[key] || key;
|
|
104
|
+
if (!vars) return text;
|
|
105
|
+
return this.z_resolveVariables(text, vars);
|
|
99
106
|
};
|
|
100
107
|
});
|
|
101
108
|
|
|
102
109
|
return new Readable(store);
|
|
103
110
|
}
|
|
111
|
+
|
|
112
|
+
/** @hidden */
|
|
113
|
+
z_resolveVariables(
|
|
114
|
+
text: string,
|
|
115
|
+
vars: Record<string, TranslationVarValue> = {},
|
|
116
|
+
): string {
|
|
117
|
+
let result = '';
|
|
118
|
+
let index = 0;
|
|
119
|
+
|
|
120
|
+
while (index < text.length) {
|
|
121
|
+
const openIndex = text.indexOf('{', index);
|
|
122
|
+
if (openIndex === -1) {
|
|
123
|
+
result += text.slice(index);
|
|
124
|
+
break;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const closeIndex = text.indexOf('}', openIndex + 1);
|
|
128
|
+
if (closeIndex === -1) {
|
|
129
|
+
result += text.slice(index);
|
|
130
|
+
break;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
result += text.slice(index, openIndex);
|
|
134
|
+
|
|
135
|
+
const key = text.slice(openIndex + 1, closeIndex);
|
|
136
|
+
if (key in vars) {
|
|
137
|
+
result += vars[key];
|
|
138
|
+
} else {
|
|
139
|
+
result += `{${key}}`;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
index = closeIndex + 1;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return result;
|
|
146
|
+
}
|
|
104
147
|
}
|
|
105
148
|
|
|
106
149
|
/**
|