@rcooler/react-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 +25 -2
- package/dist/index.d.ts +25 -0
- package/dist/index.js +49 -0
- package/package.json +38 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Roman Derevianko
|
|
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,26 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @rcooler/react-i18n
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
`createI18n(dictionaries)`: a provider and a hook that give React components the translator of
|
|
4
|
+
[`@rcooler/i18n-core`](https://www.npmjs.com/package/@rcooler/i18n-core) and a language switch.
|
|
5
|
+
React 19.3 is a peer dependency.
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install @rcooler/react-i18n
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
```tsx
|
|
12
|
+
import { createI18n } from "@rcooler/react-i18n";
|
|
13
|
+
|
|
14
|
+
const en = { "nav.home": "Home", "error.unknown": "Something went wrong." };
|
|
15
|
+
const uk = { "nav.home": "Головна", "error.unknown": "Щось пішло не так." };
|
|
16
|
+
|
|
17
|
+
export const { I18nProvider, useI18n } = createI18n({ en, uk }, { storageKey: "app.locale" });
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
ESM with type declarations. Part of [web-kit](https://github.com/RCooLeR/web-kit): the
|
|
21
|
+
[README](https://github.com/RCooLeR/web-kit#readme) there describes every package, and the
|
|
22
|
+
[changelog](https://github.com/RCooLeR/web-kit/blob/main/CHANGELOG.md) lists the changes.
|
|
23
|
+
|
|
24
|
+
## Licence
|
|
25
|
+
|
|
26
|
+
[MIT](LICENSE)
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { type Dictionaries, type Locale, type Translator, type UNKNOWN_ERROR_KEY } from "@rcooler/i18n-core";
|
|
2
|
+
import { type ReactNode } from "react";
|
|
3
|
+
export interface I18n<Key extends string> extends Translator<Key> {
|
|
4
|
+
/** The locales the app speaks, for a switch. */
|
|
5
|
+
readonly locales: readonly Locale[];
|
|
6
|
+
readonly setLocale: (locale: Locale) => void;
|
|
7
|
+
}
|
|
8
|
+
export interface I18nOptions {
|
|
9
|
+
/** Browser storage key the choice is kept under; not remembered when omitted. */
|
|
10
|
+
storageKey?: string;
|
|
11
|
+
/** Locale when nothing is stored and the browser's language is not supported; "uk" when omitted. */
|
|
12
|
+
fallback?: Locale;
|
|
13
|
+
}
|
|
14
|
+
export interface I18nProviderProps {
|
|
15
|
+
children: ReactNode;
|
|
16
|
+
/** Starts in this locale instead of the stored or the browser's one (tests, previews). */
|
|
17
|
+
initialLocale?: Locale;
|
|
18
|
+
}
|
|
19
|
+
/** The stored choice, or the browser's language. Storage can be unavailable (private mode). */
|
|
20
|
+
export declare function initialLocale(storageKey: string | undefined, fallback: Locale): Locale;
|
|
21
|
+
/** Creates the provider and the hook for one app's dictionaries. */
|
|
22
|
+
export declare function createI18n<Key extends string>(dictionaries: Dictionaries<Key> & Dictionaries<typeof UNKNOWN_ERROR_KEY>, options?: I18nOptions): {
|
|
23
|
+
I18nProvider: ({ children, initialLocale: forced }: I18nProviderProps) => import("react").JSX.Element;
|
|
24
|
+
useI18n: () => I18n<Key>;
|
|
25
|
+
};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
// @rcooler/react-i18n: the language of a React app. A provider holds the chosen locale, remembers
|
|
3
|
+
// it in the browser and gives every component below the translator of @rcooler/i18n-core.
|
|
4
|
+
//
|
|
5
|
+
// const { I18nProvider, useI18n } = createI18n({ en, uk }, { storageKey: "app.locale" });
|
|
6
|
+
import { LOCALES, createTranslator, toLocale, } from "@rcooler/i18n-core";
|
|
7
|
+
import { createContext, use, useEffect, useMemo, useState } from "react";
|
|
8
|
+
/** The stored choice, or the browser's language. Storage can be unavailable (private mode). */
|
|
9
|
+
export function initialLocale(storageKey, fallback) {
|
|
10
|
+
if (storageKey !== undefined) {
|
|
11
|
+
try {
|
|
12
|
+
const stored = window.localStorage.getItem(storageKey);
|
|
13
|
+
if (stored !== null)
|
|
14
|
+
return toLocale(stored, fallback);
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
// No storage: fall through to the browser's language.
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return toLocale(typeof navigator === "undefined" ? undefined : navigator.language, fallback);
|
|
21
|
+
}
|
|
22
|
+
/** Creates the provider and the hook for one app's dictionaries. */
|
|
23
|
+
export function createI18n(dictionaries, options = {}) {
|
|
24
|
+
const fallback = options.fallback ?? "uk";
|
|
25
|
+
const Context = createContext(null);
|
|
26
|
+
function I18nProvider({ children, initialLocale: forced }) {
|
|
27
|
+
const [locale, setLocale] = useState(() => forced ?? initialLocale(options.storageKey, fallback));
|
|
28
|
+
useEffect(() => {
|
|
29
|
+
document.documentElement.lang = locale;
|
|
30
|
+
if (options.storageKey === undefined)
|
|
31
|
+
return;
|
|
32
|
+
try {
|
|
33
|
+
window.localStorage.setItem(options.storageKey, locale);
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
// The choice then lasts for this visit only, which is all that can be done without storage.
|
|
37
|
+
}
|
|
38
|
+
}, [locale]);
|
|
39
|
+
const value = useMemo(() => ({ ...createTranslator(dictionaries, locale), locales: LOCALES, setLocale }), [locale]);
|
|
40
|
+
return _jsx(Context, { value: value, children: children });
|
|
41
|
+
}
|
|
42
|
+
function useI18n() {
|
|
43
|
+
const value = use(Context);
|
|
44
|
+
if (!value)
|
|
45
|
+
throw new Error("useI18n needs an I18nProvider above it");
|
|
46
|
+
return value;
|
|
47
|
+
}
|
|
48
|
+
return { I18nProvider, useI18n };
|
|
49
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,40 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rcooler/react-i18n",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "React provider and hook over @rcooler/i18n-core: the language of an app, switchable and remembered.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"@rcooler/source": "./src/index.tsx",
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"default": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"engines": {
|
|
19
|
+
"node": ">=24"
|
|
20
|
+
},
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "git+https://github.com/RCooLeR/web-kit.git",
|
|
24
|
+
"directory": "packages/react-i18n"
|
|
25
|
+
},
|
|
26
|
+
"publishConfig": {
|
|
27
|
+
"access": "public",
|
|
28
|
+
"registry": "https://registry.npmjs.org"
|
|
29
|
+
},
|
|
30
|
+
"peerDependencies": {
|
|
31
|
+
"react": "^19.3.1"
|
|
32
|
+
},
|
|
33
|
+
"scripts": {
|
|
34
|
+
"build": "node ../../node_modules/@typescript/native/bin/tsc -p tsconfig.build.json",
|
|
35
|
+
"typecheck": "node ../../node_modules/@typescript/native/bin/tsc --noEmit -p tsconfig.json"
|
|
36
|
+
},
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"@rcooler/i18n-core": "0.1.0"
|
|
39
|
+
}
|
|
40
|
+
}
|