@iquji-community/web-i18n 0.0.0-stage → 0.2.0
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 +15 -2
- package/README.zh-CN.md +16 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.js +88 -0
- package/package.json +38 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Iquji contributors
|
|
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
CHANGED
|
@@ -1,3 +1,16 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @iquji-community/web-i18n
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[简体中文](README.zh-CN.md)
|
|
4
|
+
|
|
5
|
+
English and Chinese locale selection, message dictionaries and string interpolation.
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install react@19.2.4 react-dom@19.2.4 @iquji-community/web-i18n
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
import { createTranslator } from "@iquji-community/web-i18n";
|
|
13
|
+
|
|
14
|
+
const translate = createTranslator({ greeting: "Hello, {name}!" });
|
|
15
|
+
translate("greeting", { name: "World" });
|
|
16
|
+
```
|
package/README.zh-CN.md
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# @iquji-community/web-i18n
|
|
2
|
+
|
|
3
|
+
[English](README.md)
|
|
4
|
+
|
|
5
|
+
支持中英文切换、词典和文本插值。
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install react@19.2.4 react-dom@19.2.4 @iquji-community/web-i18n
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
import { createTranslator } from "@iquji-community/web-i18n";
|
|
13
|
+
|
|
14
|
+
const translate = createTranslator({ greeting: "{name},你好!" });
|
|
15
|
+
translate("greeting", { name: "世界" });
|
|
16
|
+
```
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { type ReactNode } from "react";
|
|
2
|
+
export declare const IQUJI_LOCALES: readonly ["zh-CN", "en-US"];
|
|
3
|
+
export type Locale = (typeof IQUJI_LOCALES)[number];
|
|
4
|
+
export type MessageCatalog<MessageKey extends string = string> = Readonly<Record<MessageKey, string>>;
|
|
5
|
+
export type MessageValues = Readonly<Record<string, string | number>>;
|
|
6
|
+
export declare function parseLocale(value: unknown): Locale | undefined;
|
|
7
|
+
export declare function createTranslator<MessageKey extends string>(catalog: MessageCatalog<MessageKey>): (key: MessageKey, values?: MessageValues) => Readonly<Record<MessageKey, string>>[MessageKey];
|
|
8
|
+
type LocaleContextValue = {
|
|
9
|
+
locale: Locale;
|
|
10
|
+
setLocale: (locale: Locale) => void;
|
|
11
|
+
translate: (key: string, values?: MessageValues) => string;
|
|
12
|
+
};
|
|
13
|
+
export type IqujiLocaleProviderProps<MessageKey extends string = string> = {
|
|
14
|
+
children: ReactNode;
|
|
15
|
+
catalogs: Readonly<Record<Locale, MessageCatalog<MessageKey>>>;
|
|
16
|
+
storageKey?: string;
|
|
17
|
+
defaultLocale?: Locale;
|
|
18
|
+
};
|
|
19
|
+
export declare function IqujiLocaleProvider<MessageKey extends string = string>({ catalogs, children, storageKey, defaultLocale, }: IqujiLocaleProviderProps<MessageKey>): import("react").FunctionComponentElement<import("react").ProviderProps<LocaleContextValue | undefined>>;
|
|
20
|
+
export type IqujiLocaleContextValue<MessageKey extends string = string> = Omit<LocaleContextValue, "translate"> & {
|
|
21
|
+
translate: (key: MessageKey, values?: MessageValues) => string;
|
|
22
|
+
};
|
|
23
|
+
export declare function useIqujiLocale<MessageKey extends string = string>(): IqujiLocaleContextValue<MessageKey>;
|
|
24
|
+
export {};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import {
|
|
3
|
+
createContext,
|
|
4
|
+
createElement,
|
|
5
|
+
useCallback,
|
|
6
|
+
useContext,
|
|
7
|
+
useEffect,
|
|
8
|
+
useMemo,
|
|
9
|
+
useState
|
|
10
|
+
} from "react";
|
|
11
|
+
var IQUJI_LOCALES = ["zh-CN", "en-US"];
|
|
12
|
+
function parseLocale(value) {
|
|
13
|
+
if (typeof value !== "string") {
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
const normalized = value.toLowerCase();
|
|
17
|
+
if (normalized === "zh-cn" || normalized.startsWith("zh")) {
|
|
18
|
+
return "zh-CN";
|
|
19
|
+
}
|
|
20
|
+
if (normalized === "en-us" || normalized.startsWith("en")) {
|
|
21
|
+
return "en-US";
|
|
22
|
+
}
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
function createTranslator(catalog) {
|
|
26
|
+
return (key, values = {}) => Object.entries(values).reduce((message, [name, value]) => message.replaceAll(`{${name}}`, String(value)), catalog[key] ?? key);
|
|
27
|
+
}
|
|
28
|
+
var LocaleContext = createContext(undefined);
|
|
29
|
+
function browserLocale() {
|
|
30
|
+
if (typeof navigator === "undefined") {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
for (const language of navigator.languages) {
|
|
34
|
+
const locale = parseLocale(language);
|
|
35
|
+
if (locale) {
|
|
36
|
+
return locale;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return parseLocale(navigator.language);
|
|
40
|
+
}
|
|
41
|
+
function IqujiLocaleProvider({
|
|
42
|
+
catalogs,
|
|
43
|
+
children,
|
|
44
|
+
storageKey,
|
|
45
|
+
defaultLocale = "zh-CN"
|
|
46
|
+
}) {
|
|
47
|
+
const [locale, setLocaleState] = useState(() => {
|
|
48
|
+
if (typeof window === "undefined") {
|
|
49
|
+
return defaultLocale;
|
|
50
|
+
}
|
|
51
|
+
try {
|
|
52
|
+
return (storageKey ? parseLocale(window.localStorage.getItem(storageKey)) : undefined) ?? browserLocale() ?? defaultLocale;
|
|
53
|
+
} catch {
|
|
54
|
+
return browserLocale() ?? defaultLocale;
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
useEffect(() => {
|
|
58
|
+
document.documentElement.lang = locale;
|
|
59
|
+
}, [locale]);
|
|
60
|
+
const setLocale = useCallback((nextLocale) => {
|
|
61
|
+
setLocaleState(nextLocale);
|
|
62
|
+
try {
|
|
63
|
+
if (storageKey)
|
|
64
|
+
window.localStorage.setItem(storageKey, nextLocale);
|
|
65
|
+
} catch {}
|
|
66
|
+
}, [storageKey]);
|
|
67
|
+
const translate = useMemo(() => createTranslator(catalogs[locale]), [catalogs, locale]);
|
|
68
|
+
const value = useMemo(() => ({
|
|
69
|
+
locale,
|
|
70
|
+
setLocale,
|
|
71
|
+
translate: (key, values) => translate(key, values)
|
|
72
|
+
}), [locale, setLocale, translate]);
|
|
73
|
+
return createElement(LocaleContext.Provider, { value }, children);
|
|
74
|
+
}
|
|
75
|
+
function useIqujiLocale() {
|
|
76
|
+
const context = useContext(LocaleContext);
|
|
77
|
+
if (!context) {
|
|
78
|
+
throw new Error("useIqujiLocale must be used within IqujiLocaleProvider");
|
|
79
|
+
}
|
|
80
|
+
return context;
|
|
81
|
+
}
|
|
82
|
+
export {
|
|
83
|
+
useIqujiLocale,
|
|
84
|
+
parseLocale,
|
|
85
|
+
createTranslator,
|
|
86
|
+
IqujiLocaleProvider,
|
|
87
|
+
IQUJI_LOCALES
|
|
88
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,40 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@iquji-community/web-i18n",
|
|
3
|
-
"
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
}
|
|
3
|
+
"author": {
|
|
4
|
+
"name": "IQuji Community",
|
|
5
|
+
"email": "community@iqu.lol"
|
|
6
|
+
},
|
|
7
|
+
"version": "0.2.0",
|
|
8
|
+
"license": "MIT",
|
|
9
|
+
"type": "module",
|
|
10
|
+
"files": [
|
|
11
|
+
"dist",
|
|
12
|
+
"README.md",
|
|
13
|
+
"README.zh-CN.md",
|
|
14
|
+
"LICENSE"
|
|
15
|
+
],
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"import": "./dist/index.js"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"sideEffects": false,
|
|
23
|
+
"peerDependencies": {
|
|
24
|
+
"react": "19.2.4"
|
|
25
|
+
},
|
|
26
|
+
"description": "由应用注入词典的 React 国际化状态。",
|
|
27
|
+
"publishConfig": {
|
|
28
|
+
"access": "public",
|
|
29
|
+
"registry": "https://registry.npmjs.org/"
|
|
30
|
+
},
|
|
31
|
+
"repository": {
|
|
32
|
+
"type": "git",
|
|
33
|
+
"url": "git+https://github.com/haydardsx/iquji-community-web.git",
|
|
34
|
+
"directory": "i18n"
|
|
35
|
+
},
|
|
36
|
+
"homepage": "https://haydardsx.github.io/iquji-community-web/",
|
|
37
|
+
"bugs": {
|
|
38
|
+
"url": "https://github.com/haydardsx/iquji-community-web/issues"
|
|
39
|
+
}
|
|
40
|
+
}
|