next-language-selector 0.1.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/README.md +16 -0
- package/dist/index.cjs +113 -0
- package/dist/index.d.mts +26 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.mjs +85 -0
- package/package.json +56 -0
package/README.md
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# next-language-selector
|
|
2
|
+
|
|
3
|
+
A lightweight, configurable language selector for Next.js (App Router & Pages Router).
|
|
4
|
+
Efficiently manages the `NEXT_LOCALE` cookie and works seamlessly with any i18n solution.
|
|
5
|
+
|
|
6
|
+
## Key Features
|
|
7
|
+
- 🚀 **Next.js Native**: Built specifically for the Next.js ecosystem.
|
|
8
|
+
- 🪶 **Zero Dependencies**: Keeps your bundle size minimal.
|
|
9
|
+
- 🎨 **Fully Customizable**: Use built-in styles or your own UI (Shadcn, Radix, etc.).
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
pnpm add next-language-selector
|
|
15
|
+
# or
|
|
16
|
+
npm install next-language-selector
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
LanguageSelector: () => LanguageSelector,
|
|
24
|
+
setLocaleCookie: () => setLocaleCookie
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(index_exports);
|
|
27
|
+
|
|
28
|
+
// src/selector.tsx
|
|
29
|
+
var import_react = require("react");
|
|
30
|
+
|
|
31
|
+
// src/utils.ts
|
|
32
|
+
var setLocaleCookie = (locale, cookieName = "NEXT_LOCALE") => {
|
|
33
|
+
if (typeof document === "undefined") return;
|
|
34
|
+
document.cookie = `${cookieName}=${locale}; max-age=31536000; path=/`;
|
|
35
|
+
window.location.reload();
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
// src/selector.tsx
|
|
39
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
40
|
+
var LanguageSelector = ({
|
|
41
|
+
locales,
|
|
42
|
+
defaultLocale,
|
|
43
|
+
cookieName = "NEXT_LOCALE",
|
|
44
|
+
isDropdown = true,
|
|
45
|
+
renderCustom,
|
|
46
|
+
className,
|
|
47
|
+
activeColor = "red"
|
|
48
|
+
}) => {
|
|
49
|
+
const [current, setCurrent] = (0, import_react.useState)(defaultLocale);
|
|
50
|
+
(0, import_react.useEffect)(() => {
|
|
51
|
+
const saved = document.cookie.split("; ").find((row) => row.startsWith(`${cookieName}=`))?.split("=")[1];
|
|
52
|
+
if (saved) setCurrent(saved);
|
|
53
|
+
}, [cookieName]);
|
|
54
|
+
const handleSelect = (code) => {
|
|
55
|
+
setCurrent(code);
|
|
56
|
+
setLocaleCookie(code, cookieName);
|
|
57
|
+
};
|
|
58
|
+
if (renderCustom) {
|
|
59
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children: renderCustom({
|
|
60
|
+
locales,
|
|
61
|
+
currentLocale: current,
|
|
62
|
+
onChange: handleSelect
|
|
63
|
+
}) });
|
|
64
|
+
}
|
|
65
|
+
if (!isDropdown) {
|
|
66
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
67
|
+
"div",
|
|
68
|
+
{
|
|
69
|
+
style: { display: "flex", gap: "8px", alignItems: "center" },
|
|
70
|
+
className,
|
|
71
|
+
children: locales.map((l) => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
|
|
72
|
+
"button",
|
|
73
|
+
{
|
|
74
|
+
onClick: () => handleSelect(l.code),
|
|
75
|
+
style: {
|
|
76
|
+
cursor: "pointer",
|
|
77
|
+
background: "none",
|
|
78
|
+
border: "none",
|
|
79
|
+
borderBottom: current === l.code ? `1px solid ${activeColor}` : "1px solid transparent",
|
|
80
|
+
color: current === l.code ? "inherit" : "gray",
|
|
81
|
+
fontSize: "12px",
|
|
82
|
+
padding: "2px 4px"
|
|
83
|
+
},
|
|
84
|
+
children: [
|
|
85
|
+
l.flag,
|
|
86
|
+
" ",
|
|
87
|
+
l.name
|
|
88
|
+
]
|
|
89
|
+
},
|
|
90
|
+
l.code
|
|
91
|
+
))
|
|
92
|
+
}
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
96
|
+
"select",
|
|
97
|
+
{
|
|
98
|
+
value: current,
|
|
99
|
+
onChange: (e) => handleSelect(e.target.value),
|
|
100
|
+
className,
|
|
101
|
+
children: locales.map((l) => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("option", { value: l.code, children: [
|
|
102
|
+
l.flag,
|
|
103
|
+
" ",
|
|
104
|
+
l.name
|
|
105
|
+
] }, l.code))
|
|
106
|
+
}
|
|
107
|
+
);
|
|
108
|
+
};
|
|
109
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
110
|
+
0 && (module.exports = {
|
|
111
|
+
LanguageSelector,
|
|
112
|
+
setLocaleCookie
|
|
113
|
+
});
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import React, { ReactNode } from 'react';
|
|
2
|
+
|
|
3
|
+
interface LocaleConfig {
|
|
4
|
+
name: string;
|
|
5
|
+
code: string;
|
|
6
|
+
flag?: string;
|
|
7
|
+
}
|
|
8
|
+
interface LanguageSelectorProps {
|
|
9
|
+
locales: LocaleConfig[];
|
|
10
|
+
defaultLocale: string;
|
|
11
|
+
isDropdown: boolean;
|
|
12
|
+
cookieName?: string;
|
|
13
|
+
activeColor?: string;
|
|
14
|
+
className?: string;
|
|
15
|
+
renderCustom?: (props: {
|
|
16
|
+
locales: LocaleConfig[];
|
|
17
|
+
currentLocale: string;
|
|
18
|
+
onChange: (code: string) => void;
|
|
19
|
+
}) => ReactNode;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
declare const LanguageSelector: ({ locales, defaultLocale, cookieName, isDropdown, renderCustom, className, activeColor, }: LanguageSelectorProps) => React.JSX.Element;
|
|
23
|
+
|
|
24
|
+
declare const setLocaleCookie: (locale: string, cookieName?: string) => void;
|
|
25
|
+
|
|
26
|
+
export { LanguageSelector, type LanguageSelectorProps, type LocaleConfig, setLocaleCookie };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import React, { ReactNode } from 'react';
|
|
2
|
+
|
|
3
|
+
interface LocaleConfig {
|
|
4
|
+
name: string;
|
|
5
|
+
code: string;
|
|
6
|
+
flag?: string;
|
|
7
|
+
}
|
|
8
|
+
interface LanguageSelectorProps {
|
|
9
|
+
locales: LocaleConfig[];
|
|
10
|
+
defaultLocale: string;
|
|
11
|
+
isDropdown: boolean;
|
|
12
|
+
cookieName?: string;
|
|
13
|
+
activeColor?: string;
|
|
14
|
+
className?: string;
|
|
15
|
+
renderCustom?: (props: {
|
|
16
|
+
locales: LocaleConfig[];
|
|
17
|
+
currentLocale: string;
|
|
18
|
+
onChange: (code: string) => void;
|
|
19
|
+
}) => ReactNode;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
declare const LanguageSelector: ({ locales, defaultLocale, cookieName, isDropdown, renderCustom, className, activeColor, }: LanguageSelectorProps) => React.JSX.Element;
|
|
23
|
+
|
|
24
|
+
declare const setLocaleCookie: (locale: string, cookieName?: string) => void;
|
|
25
|
+
|
|
26
|
+
export { LanguageSelector, type LanguageSelectorProps, type LocaleConfig, setLocaleCookie };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// src/selector.tsx
|
|
2
|
+
import { useEffect, useState } from "react";
|
|
3
|
+
|
|
4
|
+
// src/utils.ts
|
|
5
|
+
var setLocaleCookie = (locale, cookieName = "NEXT_LOCALE") => {
|
|
6
|
+
if (typeof document === "undefined") return;
|
|
7
|
+
document.cookie = `${cookieName}=${locale}; max-age=31536000; path=/`;
|
|
8
|
+
window.location.reload();
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
// src/selector.tsx
|
|
12
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
13
|
+
var LanguageSelector = ({
|
|
14
|
+
locales,
|
|
15
|
+
defaultLocale,
|
|
16
|
+
cookieName = "NEXT_LOCALE",
|
|
17
|
+
isDropdown = true,
|
|
18
|
+
renderCustom,
|
|
19
|
+
className,
|
|
20
|
+
activeColor = "red"
|
|
21
|
+
}) => {
|
|
22
|
+
const [current, setCurrent] = useState(defaultLocale);
|
|
23
|
+
useEffect(() => {
|
|
24
|
+
const saved = document.cookie.split("; ").find((row) => row.startsWith(`${cookieName}=`))?.split("=")[1];
|
|
25
|
+
if (saved) setCurrent(saved);
|
|
26
|
+
}, [cookieName]);
|
|
27
|
+
const handleSelect = (code) => {
|
|
28
|
+
setCurrent(code);
|
|
29
|
+
setLocaleCookie(code, cookieName);
|
|
30
|
+
};
|
|
31
|
+
if (renderCustom) {
|
|
32
|
+
return /* @__PURE__ */ jsx(Fragment, { children: renderCustom({
|
|
33
|
+
locales,
|
|
34
|
+
currentLocale: current,
|
|
35
|
+
onChange: handleSelect
|
|
36
|
+
}) });
|
|
37
|
+
}
|
|
38
|
+
if (!isDropdown) {
|
|
39
|
+
return /* @__PURE__ */ jsx(
|
|
40
|
+
"div",
|
|
41
|
+
{
|
|
42
|
+
style: { display: "flex", gap: "8px", alignItems: "center" },
|
|
43
|
+
className,
|
|
44
|
+
children: locales.map((l) => /* @__PURE__ */ jsxs(
|
|
45
|
+
"button",
|
|
46
|
+
{
|
|
47
|
+
onClick: () => handleSelect(l.code),
|
|
48
|
+
style: {
|
|
49
|
+
cursor: "pointer",
|
|
50
|
+
background: "none",
|
|
51
|
+
border: "none",
|
|
52
|
+
borderBottom: current === l.code ? `1px solid ${activeColor}` : "1px solid transparent",
|
|
53
|
+
color: current === l.code ? "inherit" : "gray",
|
|
54
|
+
fontSize: "12px",
|
|
55
|
+
padding: "2px 4px"
|
|
56
|
+
},
|
|
57
|
+
children: [
|
|
58
|
+
l.flag,
|
|
59
|
+
" ",
|
|
60
|
+
l.name
|
|
61
|
+
]
|
|
62
|
+
},
|
|
63
|
+
l.code
|
|
64
|
+
))
|
|
65
|
+
}
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
return /* @__PURE__ */ jsx(
|
|
69
|
+
"select",
|
|
70
|
+
{
|
|
71
|
+
value: current,
|
|
72
|
+
onChange: (e) => handleSelect(e.target.value),
|
|
73
|
+
className,
|
|
74
|
+
children: locales.map((l) => /* @__PURE__ */ jsxs("option", { value: l.code, children: [
|
|
75
|
+
l.flag,
|
|
76
|
+
" ",
|
|
77
|
+
l.name
|
|
78
|
+
] }, l.code))
|
|
79
|
+
}
|
|
80
|
+
);
|
|
81
|
+
};
|
|
82
|
+
export {
|
|
83
|
+
LanguageSelector,
|
|
84
|
+
setLocaleCookie
|
|
85
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "next-language-selector",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Configurable language selector for Next.js",
|
|
5
|
+
"main": "./dist/index.cjs",
|
|
6
|
+
"module": "./dist/index.mjs",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": {
|
|
11
|
+
"types": "./dist/index.d.mts",
|
|
12
|
+
"default": "./dist/index.mjs"
|
|
13
|
+
},
|
|
14
|
+
"require": {
|
|
15
|
+
"types": "./dist/index.d.cts",
|
|
16
|
+
"default": "./dist/index.cjs"
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "tsup",
|
|
22
|
+
"dev": "tsup --watch"
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"dist"
|
|
26
|
+
],
|
|
27
|
+
"peerDependencies": {
|
|
28
|
+
"next": "^13.0.0 || ^14.0.0 || ^15.0.0 || ^16.0.0",
|
|
29
|
+
"react": "^18.0.0 || ^19.0.0"
|
|
30
|
+
},
|
|
31
|
+
"keywords": [
|
|
32
|
+
"next.js",
|
|
33
|
+
"i18n",
|
|
34
|
+
"language-selector",
|
|
35
|
+
"internationalization",
|
|
36
|
+
"react",
|
|
37
|
+
"cookie-based-i18n",
|
|
38
|
+
"app-router"
|
|
39
|
+
],
|
|
40
|
+
"author": "Kirilinsky (https://github.com/kirilinsky)",
|
|
41
|
+
"license": "MIT",
|
|
42
|
+
"homepage": "https://github.com/kirilinsky/next-language-selector#readme",
|
|
43
|
+
"bugs": {
|
|
44
|
+
"url": "https://github.com/kirilinsky/next-language-selector/issues"
|
|
45
|
+
},
|
|
46
|
+
"repository": {
|
|
47
|
+
"type": "git",
|
|
48
|
+
"url": "git+https://github.com/kirilinsky/next-language-selector.git"
|
|
49
|
+
},
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"@types/react": "^19.2.14",
|
|
52
|
+
"tslib": "^2.8.1",
|
|
53
|
+
"tsup": "^8.5.1",
|
|
54
|
+
"typescript": "^5.9.3"
|
|
55
|
+
}
|
|
56
|
+
}
|