next-translate 2.0.2 β 2.0.4
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 +7 -0
- package/index.d.ts +2 -2
- package/lib/cjs/transCore.js +21 -14
- package/lib/esm/transCore.js +21 -14
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -973,6 +973,7 @@ import i18n from './i18n'
|
|
|
973
973
|
export function middleware(request: NextRequest) {
|
|
974
974
|
const locale = request.nextUrl.locale || i18n.defaultLocale
|
|
975
975
|
request.nextUrl.searchParams.set('lang', locale)
|
|
976
|
+
request.nextUrl.href = request.nextUrl.href.replace(`/${locale}`, "")
|
|
976
977
|
return NextResponse.rewrite(request.nextUrl)
|
|
977
978
|
}
|
|
978
979
|
```
|
|
@@ -1113,6 +1114,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
|
|
|
1113
1114
|
<td align="center" valign="top" width="14.28%"><a href="https://github.com/wuifdesign"><img src="https://avatars.githubusercontent.com/u/5678318?v=4?s=100" width="100px;" alt="Wuif"/><br /><sub><b>Wuif</b></sub></a><br /><a href="https://github.com/aralroca/next-translate/commits?author=wuifdesign" title="Code">π»</a></td>
|
|
1114
1115
|
</tr>
|
|
1115
1116
|
<tr>
|
|
1117
|
+
<td align="center" valign="top" width="14.28%"><a href="https://github.com/marcesengel"><img src="https://avatars.githubusercontent.com/u/6208890?v=4?s=100" width="100px;" alt="Marces Engel"/><br /><sub><b>Marces Engel</b></sub></a><br /><a href="https://github.com/aralroca/next-translate/commits?author=marcesengel" title="Code">π»</a></td>
|
|
1116
1118
|
<td align="center" valign="top" width="14.28%"><a href="https://michal.bar"><img src="https://avatars.githubusercontent.com/u/9134970?v=4?s=100" width="100px;" alt="MichaΕ Bar"/><br /><sub><b>MichaΕ Bar</b></sub></a><br /><a href="https://github.com/aralroca/next-translate/commits?author=MrPumpking" title="Code">π»</a></td>
|
|
1117
1119
|
<td align="center" valign="top" width="14.28%"><a href="https://github.com/Dragate"><img src="https://avatars.githubusercontent.com/u/28112929?v=4?s=100" width="100px;" alt="Dragate"/><br /><sub><b>Dragate</b></sub></a><br /><a href="https://github.com/aralroca/next-translate/commits?author=Dragate" title="Code">π»</a></td>
|
|
1118
1120
|
<td align="center" valign="top" width="14.28%"><a href="https://github.com/marcesengel"><img src="https://avatars.githubusercontent.com/u/6208890?v=4?s=100" width="100px;" alt="Marces Engel"/><br /><sub><b>Marces Engel</b></sub></a><br /><a href="https://github.com/aralroca/next-translate/commits?author=marcesengel" title="Code">π»</a></td>
|
|
@@ -1120,6 +1122,11 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
|
|
|
1120
1122
|
<td align="center" valign="top" width="14.28%"><a href="https://github.com/StLyn4"><img src="https://avatars.githubusercontent.com/u/73965070?v=4?s=100" width="100px;" alt="Vsevolod Volkov"/><br /><sub><b>Vsevolod Volkov</b></sub></a><br /><a href="https://github.com/aralroca/next-translate/commits?author=StLyn4" title="Code">π»</a></td>
|
|
1121
1123
|
<td align="center" valign="top" width="14.28%"><a href="https://github.com/felixonmars"><img src="https://avatars.githubusercontent.com/u/1006477?v=4?s=100" width="100px;" alt="Felix Yan"/><br /><sub><b>Felix Yan</b></sub></a><br /><a href="https://github.com/aralroca/next-translate/commits?author=felixonmars" title="Documentation">π</a></td>
|
|
1122
1124
|
</tr>
|
|
1125
|
+
<tr>
|
|
1126
|
+
<td align="center" valign="top" width="14.28%"><a href="https://github.com/alziqziq"><img src="https://avatars.githubusercontent.com/u/29282122?v=4?s=100" width="100px;" alt="Muhammad Al Ziqri"/><br /><sub><b>Muhammad Al Ziqri</b></sub></a><br /><a href="https://github.com/aralroca/next-translate/commits?author=alziqziq" title="Code">π»</a></td>
|
|
1127
|
+
<td align="center" valign="top" width="14.28%"><a href="http://teka.dev"><img src="https://avatars.githubusercontent.com/u/4443094?v=4?s=100" width="100px;" alt="Marcelo Oliveira"/><br /><sub><b>Marcelo Oliveira</b></sub></a><br /><a href="https://github.com/aralroca/next-translate/commits?author=marcelotk15" title="Code">π»</a></td>
|
|
1128
|
+
<td align="center" valign="top" width="14.28%"><a href="https://github.com/SimplyComplexable"><img src="https://avatars.githubusercontent.com/u/8563846?v=4?s=100" width="100px;" alt="Zack Sunderland"/><br /><sub><b>Zack Sunderland</b></sub></a><br /><a href="https://github.com/aralroca/next-translate/commits?author=SimplyComplexable" title="Code">π»</a></td>
|
|
1129
|
+
</tr>
|
|
1123
1130
|
</tbody>
|
|
1124
1131
|
</table>
|
|
1125
1132
|
|
package/index.d.ts
CHANGED
|
@@ -3,10 +3,10 @@ import type { NextConfig } from 'next';
|
|
|
3
3
|
export interface TranslationQuery {
|
|
4
4
|
[name: string]: any;
|
|
5
5
|
}
|
|
6
|
-
export type Translate = <T = string>(i18nKey: string | TemplateStringsArray, query?: TranslationQuery | null, options?: {
|
|
6
|
+
export type Translate = <T extends unknown = string>(i18nKey: string | TemplateStringsArray, query?: TranslationQuery | null, options?: {
|
|
7
7
|
returnObjects?: boolean;
|
|
8
8
|
fallback?: string | string[];
|
|
9
|
-
default?: string;
|
|
9
|
+
default?: T | string;
|
|
10
10
|
ns?: string;
|
|
11
11
|
}) => T;
|
|
12
12
|
export interface I18n {
|
package/lib/cjs/transCore.js
CHANGED
|
@@ -25,6 +25,20 @@ function splitNsKey(key, nsSeparator) {
|
|
|
25
25
|
function transCore(_a) {
|
|
26
26
|
var config = _a.config, allNamespaces = _a.allNamespaces, pluralRules = _a.pluralRules, lang = _a.lang;
|
|
27
27
|
var _b = config.logger, logger = _b === void 0 ? missingKeyLogger : _b, _c = config.allowEmptyStrings, allowEmptyStrings = _c === void 0 ? true : _c;
|
|
28
|
+
var interpolateUnknown = function (value, query) {
|
|
29
|
+
if (Array.isArray(value)) {
|
|
30
|
+
return value.map(function (val) { return interpolateUnknown(val, query); });
|
|
31
|
+
}
|
|
32
|
+
if (value instanceof Object) {
|
|
33
|
+
return objectInterpolation({
|
|
34
|
+
obj: value,
|
|
35
|
+
query: query,
|
|
36
|
+
config: config,
|
|
37
|
+
lang: lang,
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
return interpolation({ text: value, query: query, config: config, lang: lang });
|
|
41
|
+
};
|
|
28
42
|
var t = function (key, query, options) {
|
|
29
43
|
var _a;
|
|
30
44
|
if (key === void 0) { key = ''; }
|
|
@@ -33,7 +47,10 @@ function transCore(_a) {
|
|
|
33
47
|
var _d = splitNsKey(k, nsSeparator), i18nKey = _d.i18nKey, _e = _d.namespace, namespace = _e === void 0 ? (_a = options === null || options === void 0 ? void 0 : options.ns) !== null && _a !== void 0 ? _a : config.defaultNS : _e;
|
|
34
48
|
var dic = (namespace && allNamespaces[namespace]) || {};
|
|
35
49
|
var keyWithPlural = plural(pluralRules, dic, i18nKey, config, query);
|
|
36
|
-
var
|
|
50
|
+
var dicValue = getDicValue(dic, keyWithPlural, config, options);
|
|
51
|
+
var value = typeof dicValue === 'object'
|
|
52
|
+
? JSON.parse(JSON.stringify(dicValue))
|
|
53
|
+
: dicValue;
|
|
37
54
|
var empty = typeof value === 'undefined' ||
|
|
38
55
|
(typeof value === 'object' && !Object.keys(value).length) ||
|
|
39
56
|
(value === '' && !allowEmptyStrings);
|
|
@@ -52,21 +69,13 @@ function transCore(_a) {
|
|
|
52
69
|
return t(firstFallback, query, __assign(__assign({}, options), { fallback: restFallbacks }));
|
|
53
70
|
}
|
|
54
71
|
}
|
|
55
|
-
if (empty && (options === null || options === void 0 ? void 0 : options.default) && (fallbacks === null || fallbacks === void 0 ? void 0 : fallbacks.length)
|
|
56
|
-
return
|
|
72
|
+
if (empty && (options === null || options === void 0 ? void 0 : options.default) && !(fallbacks === null || fallbacks === void 0 ? void 0 : fallbacks.length)) {
|
|
73
|
+
return interpolateUnknown(options.default, query);
|
|
57
74
|
}
|
|
58
75
|
if (empty) {
|
|
59
76
|
return k;
|
|
60
77
|
}
|
|
61
|
-
|
|
62
|
-
return objectInterpolation({
|
|
63
|
-
obj: value,
|
|
64
|
-
query: query,
|
|
65
|
-
config: config,
|
|
66
|
-
lang: lang,
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
return interpolation({ text: value, query: query, config: config, lang: lang });
|
|
78
|
+
return interpolateUnknown(value, query);
|
|
70
79
|
};
|
|
71
80
|
return t;
|
|
72
81
|
}
|
|
@@ -121,8 +130,6 @@ function interpolation(_a) {
|
|
|
121
130
|
var _b = config.interpolation || {}, _c = _b.format, format = _c === void 0 ? null : _c, _d = _b.prefix, prefix = _d === void 0 ? '{{' : _d, _e = _b.suffix, suffix = _e === void 0 ? '}}' : _e;
|
|
122
131
|
var regexEnd = suffix === '' ? '' : "(?:[\\s,]+([\\w-]*))?\\s*".concat(escapeRegex(suffix));
|
|
123
132
|
return Object.keys(query).reduce(function (all, varKey) {
|
|
124
|
-
if (typeof all !== 'string')
|
|
125
|
-
return all;
|
|
126
133
|
var regex = new RegExp("".concat(escapeRegex(prefix), "\\s*").concat(varKey).concat(regexEnd), 'gm');
|
|
127
134
|
return all.replace(regex, function (_match, $1) {
|
|
128
135
|
return $1 && format
|
package/lib/esm/transCore.js
CHANGED
|
@@ -23,6 +23,20 @@ function splitNsKey(key, nsSeparator) {
|
|
|
23
23
|
export default function transCore(_a) {
|
|
24
24
|
var config = _a.config, allNamespaces = _a.allNamespaces, pluralRules = _a.pluralRules, lang = _a.lang;
|
|
25
25
|
var _b = config.logger, logger = _b === void 0 ? missingKeyLogger : _b, _c = config.allowEmptyStrings, allowEmptyStrings = _c === void 0 ? true : _c;
|
|
26
|
+
var interpolateUnknown = function (value, query) {
|
|
27
|
+
if (Array.isArray(value)) {
|
|
28
|
+
return value.map(function (val) { return interpolateUnknown(val, query); });
|
|
29
|
+
}
|
|
30
|
+
if (value instanceof Object) {
|
|
31
|
+
return objectInterpolation({
|
|
32
|
+
obj: value,
|
|
33
|
+
query: query,
|
|
34
|
+
config: config,
|
|
35
|
+
lang: lang,
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
return interpolation({ text: value, query: query, config: config, lang: lang });
|
|
39
|
+
};
|
|
26
40
|
var t = function (key, query, options) {
|
|
27
41
|
var _a;
|
|
28
42
|
if (key === void 0) { key = ''; }
|
|
@@ -31,7 +45,10 @@ export default function transCore(_a) {
|
|
|
31
45
|
var _d = splitNsKey(k, nsSeparator), i18nKey = _d.i18nKey, _e = _d.namespace, namespace = _e === void 0 ? (_a = options === null || options === void 0 ? void 0 : options.ns) !== null && _a !== void 0 ? _a : config.defaultNS : _e;
|
|
32
46
|
var dic = (namespace && allNamespaces[namespace]) || {};
|
|
33
47
|
var keyWithPlural = plural(pluralRules, dic, i18nKey, config, query);
|
|
34
|
-
var
|
|
48
|
+
var dicValue = getDicValue(dic, keyWithPlural, config, options);
|
|
49
|
+
var value = typeof dicValue === 'object'
|
|
50
|
+
? JSON.parse(JSON.stringify(dicValue))
|
|
51
|
+
: dicValue;
|
|
35
52
|
var empty = typeof value === 'undefined' ||
|
|
36
53
|
(typeof value === 'object' && !Object.keys(value).length) ||
|
|
37
54
|
(value === '' && !allowEmptyStrings);
|
|
@@ -50,21 +67,13 @@ export default function transCore(_a) {
|
|
|
50
67
|
return t(firstFallback, query, __assign(__assign({}, options), { fallback: restFallbacks }));
|
|
51
68
|
}
|
|
52
69
|
}
|
|
53
|
-
if (empty && (options === null || options === void 0 ? void 0 : options.default) && (fallbacks === null || fallbacks === void 0 ? void 0 : fallbacks.length)
|
|
54
|
-
return
|
|
70
|
+
if (empty && (options === null || options === void 0 ? void 0 : options.default) && !(fallbacks === null || fallbacks === void 0 ? void 0 : fallbacks.length)) {
|
|
71
|
+
return interpolateUnknown(options.default, query);
|
|
55
72
|
}
|
|
56
73
|
if (empty) {
|
|
57
74
|
return k;
|
|
58
75
|
}
|
|
59
|
-
|
|
60
|
-
return objectInterpolation({
|
|
61
|
-
obj: value,
|
|
62
|
-
query: query,
|
|
63
|
-
config: config,
|
|
64
|
-
lang: lang,
|
|
65
|
-
});
|
|
66
|
-
}
|
|
67
|
-
return interpolation({ text: value, query: query, config: config, lang: lang });
|
|
76
|
+
return interpolateUnknown(value, query);
|
|
68
77
|
};
|
|
69
78
|
return t;
|
|
70
79
|
}
|
|
@@ -118,8 +127,6 @@ function interpolation(_a) {
|
|
|
118
127
|
var _b = config.interpolation || {}, _c = _b.format, format = _c === void 0 ? null : _c, _d = _b.prefix, prefix = _d === void 0 ? '{{' : _d, _e = _b.suffix, suffix = _e === void 0 ? '}}' : _e;
|
|
119
128
|
var regexEnd = suffix === '' ? '' : "(?:[\\s,]+([\\w-]*))?\\s*".concat(escapeRegex(suffix));
|
|
120
129
|
return Object.keys(query).reduce(function (all, varKey) {
|
|
121
|
-
if (typeof all !== 'string')
|
|
122
|
-
return all;
|
|
123
130
|
var regex = new RegExp("".concat(escapeRegex(prefix), "\\s*").concat(varKey).concat(regexEnd), 'gm');
|
|
124
131
|
return all.replace(regex, function (_match, $1) {
|
|
125
132
|
return $1 && format
|