next-safe-i18n 0.0.1
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 +3 -0
- package/dist/config.d.ts +11 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +2 -0
- package/dist/i18n.d.ts +15 -0
- package/dist/i18n.d.ts.map +1 -0
- package/dist/i18n.js +73 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5 -0
- package/package.json +40 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Victor Teo
|
|
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
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA,KAAK,UAAU,CAAC,MAAM,SAAS,MAAM,EAAE,UAAU,IAAI;IACnD,aAAa,EAAE,MAAM,CAAA;IACrB,OAAO,EAAE,MAAM,EAAE,CAAA;IACjB,YAAY,EAAE;SAAG,GAAG,IAAI,MAAM,GAAG,OAAO,CAAC;YAAE,OAAO,EAAE,UAAU,CAAA;SAAE,CAAC;KAAE,CAAA;CACpE,CAAA;eAEc,UAAU"}
|
package/dist/config.js
ADDED
package/dist/i18n.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { NextRequest, NextResponse } from "next/server";
|
|
2
|
+
import I18NConfig from "./config";
|
|
3
|
+
export type I18n<Locale extends string, Dictionary> = {
|
|
4
|
+
createProxy(proxy: (request: NextRequest) => (NextResponse | undefined)): (request: NextRequest) => NextResponse;
|
|
5
|
+
fetchLocale(params: Params<Locale>): Promise<Locale>;
|
|
6
|
+
fetchLocaleAndDictionary(params: Params<Locale>): Promise<{
|
|
7
|
+
locale: Locale;
|
|
8
|
+
dictionary: Dictionary;
|
|
9
|
+
}>;
|
|
10
|
+
};
|
|
11
|
+
export type Params<Locale> = Promise<{
|
|
12
|
+
lang: Locale;
|
|
13
|
+
}>;
|
|
14
|
+
export declare function createI18n<Locale extends string, Dictionary>(config: I18NConfig<Locale, Dictionary>): I18n<Locale, Dictionary>;
|
|
15
|
+
//# sourceMappingURL=i18n.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"i18n.d.ts","sourceRoot":"","sources":["../src/i18n.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAIvD,OAAO,UAAU,MAAM,UAAU,CAAA;AAEjC,MAAM,MAAM,IAAI,CAAC,MAAM,SAAS,MAAM,EAAE,UAAU,IAAI;IACpD,WAAW,CAAC,KAAK,EAAE,CAAC,OAAO,EAAE,WAAW,KAAK,CAAC,YAAY,GAAG,SAAS,CAAC,GAAG,CAAC,OAAO,EAAE,WAAW,KAAK,YAAY,CAAA;IAChH,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,CAAC,GAAG,OAAO,CAAC,MAAM,CAAC,CAAA;IACpD,wBAAwB,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,CAAC,GAAG,OAAO,CAAC;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,UAAU,EAAE,UAAU,CAAA;KAAE,CAAC,CAAA;CACtG,CAAA;AAED,MAAM,MAAM,MAAM,CAAC,MAAM,IAAI,OAAO,CAAC;IACnC,IAAI,EAAE,MAAM,CAAA;CACb,CAAC,CAAA;AAEF,wBAAgB,UAAU,CAAC,MAAM,SAAS,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,UAAU,CAAC,MAAM,EAAE,UAAU,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,UAAU,CAAC,CA0C9H"}
|
package/dist/i18n.js
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.createI18n = createI18n;
|
|
16
|
+
const server_1 = require("next/server");
|
|
17
|
+
const navigation_1 = require("next/navigation");
|
|
18
|
+
const negotiator_1 = __importDefault(require("negotiator"));
|
|
19
|
+
const intl_localematcher_1 = require("@formatjs/intl-localematcher");
|
|
20
|
+
function createI18n(config) {
|
|
21
|
+
return {
|
|
22
|
+
createProxy(proxy = emptyProxy) {
|
|
23
|
+
return (request) => {
|
|
24
|
+
const pathname = request.nextUrl.pathname;
|
|
25
|
+
const pathnameIsMissingLocale = config.locales.every((locale) => !pathname.startsWith(`/${locale}/`) && pathname !== `/${locale}`);
|
|
26
|
+
if (pathnameIsMissingLocale) {
|
|
27
|
+
const locale = getLocale(request, config.locales, config.defaultLocale);
|
|
28
|
+
return server_1.NextResponse.redirect(new URL(`/${locale}${pathname.startsWith("/") ? "" : "/"}${pathname}`, request.url));
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
return proxy(request) || server_1.NextResponse.next({
|
|
32
|
+
request: {
|
|
33
|
+
headers: request.headers
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
},
|
|
39
|
+
fetchLocale(params) {
|
|
40
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
41
|
+
const { lang } = yield params;
|
|
42
|
+
return lang;
|
|
43
|
+
});
|
|
44
|
+
},
|
|
45
|
+
fetchLocaleAndDictionary(params) {
|
|
46
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
47
|
+
const locale = yield this.fetchLocale(params);
|
|
48
|
+
if (locale in config.dictionaries) {
|
|
49
|
+
return {
|
|
50
|
+
locale,
|
|
51
|
+
dictionary: (yield config.dictionaries[locale]).default
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
(0, navigation_1.notFound)();
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
function emptyProxy(request) {
|
|
62
|
+
return server_1.NextResponse.next({
|
|
63
|
+
request: {
|
|
64
|
+
headers: request.headers
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
function getLocale(request, locales, defaultLocale) {
|
|
69
|
+
const negotiatorHeaders = {};
|
|
70
|
+
request.headers.forEach((value, key) => (negotiatorHeaders[key] = value));
|
|
71
|
+
const languages = new negotiator_1.default({ headers: negotiatorHeaders }).languages(locales);
|
|
72
|
+
return (0, intl_localematcher_1.match)(languages, locales, defaultLocale);
|
|
73
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,OAAO,IAAI,UAAU,EAAE,MAAM,UAAU,CAAA;AACrD,OAAO,EAAE,UAAU,EAAE,KAAK,IAAI,EAAE,KAAK,MAAM,EAAE,MAAM,QAAQ,CAAA"}
|
package/dist/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "next-safe-i18n",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Safe and modern i18n for Next.js",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
"./*": "./dist/*.js"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"README.md",
|
|
12
|
+
"LICENSE",
|
|
13
|
+
"dist/**/*"
|
|
14
|
+
],
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/victorteokw/next-safe-i18n.git"
|
|
18
|
+
},
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/victorteokw/next-safe-i18n/issues"
|
|
21
|
+
},
|
|
22
|
+
"homepage": "https://github.com/victorteokw/next-safe-i18n#readme",
|
|
23
|
+
"author": "Victor Teo <yeannylam@gmail.com>",
|
|
24
|
+
"license": "MIT",
|
|
25
|
+
"scripts": {
|
|
26
|
+
"prepublish": "tsc"
|
|
27
|
+
},
|
|
28
|
+
"peerDependencies": {
|
|
29
|
+
"next": "^16",
|
|
30
|
+
"react": "^19"
|
|
31
|
+
},
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"@formatjs/intl-localematcher": "^0.8.13",
|
|
34
|
+
"negotiator": "^1.1.0"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"typescript": "^7.0.2",
|
|
38
|
+
"@types/negotiator": "^0.6.5"
|
|
39
|
+
}
|
|
40
|
+
}
|