@sehv-oss/i18n-react 1.0.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 +7 -0
- package/README.md +132 -0
- package/dist/react.d.mts +84 -0
- package/dist/react.mjs +1 -0
- package/package.json +42 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
ISC License
|
|
2
|
+
|
|
3
|
+
Copyright 2026 sehv-oss
|
|
4
|
+
|
|
5
|
+
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
# @sehv-oss/i18n-react
|
|
2
|
+
|
|
3
|
+
React bindings for @sehv-oss/i18n.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
# npm
|
|
9
|
+
npm install @sehv-oss/i18n @sehv-oss/i18n-react
|
|
10
|
+
|
|
11
|
+
# yarn
|
|
12
|
+
yarn add @sehv-oss/i18n @sehv-oss/i18n-react
|
|
13
|
+
|
|
14
|
+
# pnpm
|
|
15
|
+
pnpm add @sehv-oss/i18n @sehv-oss/i18n-react
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Usage
|
|
19
|
+
|
|
20
|
+
### Setup
|
|
21
|
+
|
|
22
|
+
```tsx
|
|
23
|
+
import { createI18n } from '@sehv-oss/i18n';
|
|
24
|
+
import { I18nProvider } from '@sehv-oss/i18n-react';
|
|
25
|
+
|
|
26
|
+
const i18n = createI18n({
|
|
27
|
+
locale: 'en',
|
|
28
|
+
messages: {
|
|
29
|
+
en: {
|
|
30
|
+
greeting: 'Hello, {$name}!',
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
function App() {
|
|
36
|
+
return (
|
|
37
|
+
<I18nProvider i18n={i18n}>
|
|
38
|
+
<MyComponent />
|
|
39
|
+
</I18nProvider>
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### Hooks
|
|
45
|
+
|
|
46
|
+
```tsx
|
|
47
|
+
import {
|
|
48
|
+
useTranslate,
|
|
49
|
+
useLocale,
|
|
50
|
+
useFormatNumber,
|
|
51
|
+
useFormatCurrency,
|
|
52
|
+
useFormatDate,
|
|
53
|
+
} from '@sehv-oss/i18n-react';
|
|
54
|
+
|
|
55
|
+
function MyComponent() {
|
|
56
|
+
const translate = useTranslate();
|
|
57
|
+
const [locale, setLocale] = useLocale();
|
|
58
|
+
const formatNumber = useFormatNumber();
|
|
59
|
+
const formatCurrency = useFormatCurrency();
|
|
60
|
+
const formatDate = useFormatDate();
|
|
61
|
+
|
|
62
|
+
return (
|
|
63
|
+
<div>
|
|
64
|
+
<p>{translate('greeting', { name: 'World' })}</p>
|
|
65
|
+
<p>{formatNumber(1234.56)}</p>
|
|
66
|
+
<p>{formatCurrency(99.9, 'BRL')}</p>
|
|
67
|
+
<p>{formatDate(new Date(), { dateStyle: 'long' })}</p>
|
|
68
|
+
<button onClick={() => setLocale('en')}>English</button>
|
|
69
|
+
</div>
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### Components
|
|
75
|
+
|
|
76
|
+
```tsx
|
|
77
|
+
import {
|
|
78
|
+
Translate,
|
|
79
|
+
FormatNumber,
|
|
80
|
+
FormatCurrency,
|
|
81
|
+
FormatDate,
|
|
82
|
+
FormatList,
|
|
83
|
+
FormatRelativeTime,
|
|
84
|
+
} from '@sehv-oss/i18n-react';
|
|
85
|
+
|
|
86
|
+
function MyComponent() {
|
|
87
|
+
return (
|
|
88
|
+
<div>
|
|
89
|
+
<Translate id="greeting" values={{ name: 'World' }} />
|
|
90
|
+
<FormatNumber value={1234.56} />
|
|
91
|
+
<FormatCurrency value={99.9} currency="BRL" />
|
|
92
|
+
<FormatDate value={new Date()} dateStyle="long" />
|
|
93
|
+
<FormatList values={['a', 'b', 'c']} />
|
|
94
|
+
<FormatRelativeTime value={-2} unit="days" />
|
|
95
|
+
</div>
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## API
|
|
101
|
+
|
|
102
|
+
### Provider
|
|
103
|
+
|
|
104
|
+
- `I18nProvider` — Provides i18n context to children
|
|
105
|
+
|
|
106
|
+
### Hooks
|
|
107
|
+
|
|
108
|
+
| Hook | Returns |
|
|
109
|
+
| ------------------------- | ------------------------------------------- |
|
|
110
|
+
| `useI18n()` | i18n instance |
|
|
111
|
+
| `useLocale()` | `[locale, setLocale]` |
|
|
112
|
+
| `useTranslate()` | `translate(key, values?)` |
|
|
113
|
+
| `useFormatNumber()` | `formatNumber(value, options?)` |
|
|
114
|
+
| `useFormatCurrency()` | `formatCurrency(value, currency, options?)` |
|
|
115
|
+
| `useFormatDate()` | `formatDate(value, options?)` |
|
|
116
|
+
| `useFormatList()` | `formatList(values, options?)` |
|
|
117
|
+
| `useFormatRelativeTime()` | `formatRelativeTime(value, unit, options?)` |
|
|
118
|
+
|
|
119
|
+
### Components
|
|
120
|
+
|
|
121
|
+
| Component | Props |
|
|
122
|
+
| -------------------- | --------------------------------- |
|
|
123
|
+
| `Translate` | `id`, `values?` |
|
|
124
|
+
| `FormatNumber` | `value`, `...options` |
|
|
125
|
+
| `FormatCurrency` | `value`, `currency`, `...options` |
|
|
126
|
+
| `FormatDate` | `value`, `...options` |
|
|
127
|
+
| `FormatList` | `values`, `...options` |
|
|
128
|
+
| `FormatRelativeTime` | `value`, `unit`, `...options` |
|
|
129
|
+
|
|
130
|
+
## License
|
|
131
|
+
|
|
132
|
+
ISC
|
package/dist/react.d.mts
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import * as react_jsx_runtime0 from "react/jsx-runtime";
|
|
3
|
+
import * as _sehv_oss_i18n0 from "@sehv-oss/i18n";
|
|
4
|
+
import { FormatCurrencyOptions, FormatDateOptions, FormatListOptions, FormatNumberOptions, FormatRelativeTimeOptions, FormatRelativeTimeUnit, I18nInstance } from "@sehv-oss/i18n";
|
|
5
|
+
|
|
6
|
+
//#region src/components/format-currency.d.ts
|
|
7
|
+
type FormatCurrencyProps = {
|
|
8
|
+
value: number;
|
|
9
|
+
currency: string;
|
|
10
|
+
} & FormatCurrencyOptions;
|
|
11
|
+
declare function FormatCurrency(props: FormatCurrencyProps): string;
|
|
12
|
+
//#endregion
|
|
13
|
+
//#region src/components/format-date.d.ts
|
|
14
|
+
type FormatDateProps = {
|
|
15
|
+
value: Date | number;
|
|
16
|
+
} & FormatDateOptions;
|
|
17
|
+
declare function FormatDate(props: FormatDateProps): string;
|
|
18
|
+
//#endregion
|
|
19
|
+
//#region src/components/format-list.d.ts
|
|
20
|
+
type FormatListProps = {
|
|
21
|
+
values: string[];
|
|
22
|
+
} & FormatListOptions;
|
|
23
|
+
declare function FormatList(props: FormatListProps): string;
|
|
24
|
+
//#endregion
|
|
25
|
+
//#region src/components/format-number.d.ts
|
|
26
|
+
type FormatNumberProps = {
|
|
27
|
+
value: number;
|
|
28
|
+
} & FormatNumberOptions;
|
|
29
|
+
declare function FormatNumber(props: FormatNumberProps): string;
|
|
30
|
+
//#endregion
|
|
31
|
+
//#region src/components/format-relative-time.d.ts
|
|
32
|
+
type FormatRelativeTimeProps = {
|
|
33
|
+
value: number;
|
|
34
|
+
unit: FormatRelativeTimeUnit;
|
|
35
|
+
} & FormatRelativeTimeOptions;
|
|
36
|
+
declare function FormatRelativeTime(props: FormatRelativeTimeProps): string;
|
|
37
|
+
//#endregion
|
|
38
|
+
//#region src/components/translate.d.ts
|
|
39
|
+
type TranslateProps = {
|
|
40
|
+
id: string;
|
|
41
|
+
values?: Record<string, unknown>;
|
|
42
|
+
};
|
|
43
|
+
declare function Translate(props: TranslateProps): string;
|
|
44
|
+
//#endregion
|
|
45
|
+
//#region src/hooks/use-format-currency.d.ts
|
|
46
|
+
declare function useFormatCurrency(): (value: number, currency: string, options?: FormatCurrencyOptions) => string;
|
|
47
|
+
//#endregion
|
|
48
|
+
//#region src/hooks/use-format-date.d.ts
|
|
49
|
+
declare function useFormatDate(): (value: Date | number, options?: FormatDateOptions) => string;
|
|
50
|
+
//#endregion
|
|
51
|
+
//#region src/hooks/use-format-list.d.ts
|
|
52
|
+
declare function useFormatList(): (values: string[], options?: FormatListOptions) => string;
|
|
53
|
+
//#endregion
|
|
54
|
+
//#region src/hooks/use-format-number.d.ts
|
|
55
|
+
declare function useFormatNumber(): (value: number, options?: FormatNumberOptions) => string;
|
|
56
|
+
//#endregion
|
|
57
|
+
//#region src/hooks/use-format-relative-time.d.ts
|
|
58
|
+
declare function useFormatRelativeTime(): (value: number, unit: FormatRelativeTimeUnit, options?: FormatRelativeTimeOptions) => string;
|
|
59
|
+
//#endregion
|
|
60
|
+
//#region src/hooks/use-i18n.d.ts
|
|
61
|
+
declare function useI18n(): _sehv_oss_i18n0.I18nInstance;
|
|
62
|
+
//#endregion
|
|
63
|
+
//#region src/hooks/use-locale.d.ts
|
|
64
|
+
type UseLocaleReturn = [string, (locale: string) => void];
|
|
65
|
+
declare function useLocale(): UseLocaleReturn;
|
|
66
|
+
//#endregion
|
|
67
|
+
//#region src/hooks/use-translate.d.ts
|
|
68
|
+
declare const useTranslate: () => (key: string, values?: Record<string, unknown>) => string;
|
|
69
|
+
//#endregion
|
|
70
|
+
//#region src/context.d.ts
|
|
71
|
+
type I18nContextValue = {
|
|
72
|
+
i18n: I18nInstance;
|
|
73
|
+
};
|
|
74
|
+
declare const I18nContext: React.Context<I18nContextValue | null>;
|
|
75
|
+
declare const useI18nContext: () => I18nContextValue;
|
|
76
|
+
//#endregion
|
|
77
|
+
//#region src/provider.d.ts
|
|
78
|
+
type I18nProviderProps = {
|
|
79
|
+
i18n: I18nInstance;
|
|
80
|
+
children: React.ReactNode;
|
|
81
|
+
};
|
|
82
|
+
declare function I18nProvider(props: I18nProviderProps): react_jsx_runtime0.JSX.Element;
|
|
83
|
+
//#endregion
|
|
84
|
+
export { FormatCurrency, FormatCurrencyProps, FormatDate, FormatDateProps, FormatList, FormatListProps, FormatNumber, FormatNumberProps, FormatRelativeTime, FormatRelativeTimeProps, I18nContext, I18nContextValue, I18nProvider, I18nProviderProps, Translate, TranslateProps, UseLocaleReturn, useFormatCurrency, useFormatDate, useFormatList, useFormatNumber, useFormatRelativeTime, useI18n, useI18nContext, useLocale, useTranslate };
|
package/dist/react.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import*as e from"react";import{jsx as t}from"react/jsx-runtime";const n=e.createContext(null),r=()=>{let t=e.useContext(n);if(!t)throw Error(`Context not initialized.`);return t};function i(){let{i18n:t}=r();return e.useCallback((e,n,r)=>t.formatCurrency(e,n,r),[t])}function a(e){let{value:t,currency:n,...r}=e;return i()(t,n,r)}function o(){let{i18n:t}=r();return e.useCallback((e,n)=>t.formatDate(e,n),[t])}function s(e){let{value:t,...n}=e;return o()(t,n)}function c(){let{i18n:t}=r();return e.useCallback((e,n)=>t.formatList(e,n),[t])}function l(e){let{values:t,...n}=e;return c()(t,n)}function u(){let{i18n:t}=r();return e.useCallback((e,n)=>t.formatNumber(e,n),[t])}function d(e){let{value:t,...n}=e;return u()(t,n)}function f(){let{i18n:t}=r();return e.useCallback((e,n,r)=>t.formatRelativeTime(e,n,r),[t])}function p(e){let{value:t,unit:n,...r}=e;return f()(t,n,r)}const m=()=>{let{i18n:t}=r();return e.useCallback((e,n)=>t.translate(e,n),[t])};function h(e){let{id:t,values:n}=e;return m()(t,n)}function g(){let{i18n:e}=r();return e}function _(){let{i18n:t}=r(),n=e.useCallback(e=>{t.setLocale(e)},[t]);return[t.getLocale(),n]}function v(r){let{i18n:i,children:a}=r,[o,s]=e.useState(i.getLocale());return e.useEffect(()=>i.onLocaleChange(s),[i]),t(n,{value:e.useMemo(()=>({i18n:i,locale:o}),[i,o]),children:a})}export{a as FormatCurrency,s as FormatDate,l as FormatList,d as FormatNumber,p as FormatRelativeTime,n as I18nContext,v as I18nProvider,h as Translate,i as useFormatCurrency,o as useFormatDate,c as useFormatList,u as useFormatNumber,f as useFormatRelativeTime,g as useI18n,r as useI18nContext,_ as useLocale,m as useTranslate};
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@sehv-oss/i18n-react",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "React bindings for @sehv-oss/i18n",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"files": [
|
|
7
|
+
"./dist"
|
|
8
|
+
],
|
|
9
|
+
"exports": {
|
|
10
|
+
"./package.json": "./package.json",
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/react.d.ts",
|
|
13
|
+
"import": "./dist/react.mjs"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"i18n",
|
|
18
|
+
"react",
|
|
19
|
+
"internationalization",
|
|
20
|
+
"localization"
|
|
21
|
+
],
|
|
22
|
+
"license": "ISC",
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "https://github.com/sehv-oss/i18n.git",
|
|
26
|
+
"directory": "packages/react"
|
|
27
|
+
},
|
|
28
|
+
"engines": {
|
|
29
|
+
"node": ">=22 <25"
|
|
30
|
+
},
|
|
31
|
+
"peerDependencies": {
|
|
32
|
+
"react": "^19.0.0",
|
|
33
|
+
"@sehv-oss/i18n": "1.0.0"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@types/react": "^19.0.0"
|
|
37
|
+
},
|
|
38
|
+
"scripts": {
|
|
39
|
+
"build": "tsdown",
|
|
40
|
+
"lint:eslint": "eslint ."
|
|
41
|
+
}
|
|
42
|
+
}
|