@iquji-web/web-i18n 0.0.0-stage → 0.1.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 +4 -2
- package/dist/index.d.ts +24 -0
- package/dist/index.js +88 -0
- package/package.json +33 -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,5 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Web i18n
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
`@iquji-web/web-i18n` 只提供通用 i18n 能力,JavaScript、类型声明、必要样式及功能测试随源码交付。
|
|
4
|
+
|
|
5
|
+
安装使用本目录锁文件,运行 `bun install --frozen-lockfile` 和 `bun run check`。正式入口只导出 `dist` 中的 JavaScript、类型声明,不要求消费者编译本包源码。
|
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,35 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@iquji-web/web-i18n",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"files": [
|
|
7
|
+
"dist",
|
|
8
|
+
"README.md",
|
|
9
|
+
"LICENSE"
|
|
10
|
+
],
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"import": "./dist/index.js"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"sideEffects": false,
|
|
18
|
+
"peerDependencies": {
|
|
19
|
+
"react": "19.2.4"
|
|
20
|
+
},
|
|
21
|
+
"description": "由应用注入词典的 React 国际化状态。",
|
|
22
|
+
"publishConfig": {
|
|
23
|
+
"access": "public",
|
|
24
|
+
"registry": "https://registry.npmjs.org/"
|
|
25
|
+
},
|
|
26
|
+
"repository": {
|
|
27
|
+
"type": "git",
|
|
28
|
+
"url": "git+https://github.com/haydardsx/iquji-community-web.git",
|
|
29
|
+
"directory": "i18n"
|
|
30
|
+
},
|
|
31
|
+
"homepage": "https://community-web.iquji.com",
|
|
32
|
+
"bugs": {
|
|
33
|
+
"url": "https://github.com/haydardsx/iquji-community-web/issues"
|
|
34
|
+
}
|
|
35
|
+
}
|