@better-intl/react 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/dist/index.cjs ADDED
@@ -0,0 +1,64 @@
1
+ "use client";
2
+ 'use strict';
3
+
4
+ var react = require('react');
5
+ var core = require('@better-intl/core');
6
+ var jsxRuntime = require('react/jsx-runtime');
7
+
8
+ // src/context.ts
9
+ var IntlContext = react.createContext(null);
10
+ function useIntlContext() {
11
+ const ctx = react.useContext(IntlContext);
12
+ if (!ctx) {
13
+ throw new Error(
14
+ "[@better-intl/react] useTranslation must be used within <IntlProvider>. Wrap your app with <IntlProvider locale={locale} messages={messages}>."
15
+ );
16
+ }
17
+ return ctx;
18
+ }
19
+ function IntlProvider({
20
+ locale,
21
+ messages,
22
+ defaultLocale,
23
+ children
24
+ }) {
25
+ const runtime = react.useMemo(() => {
26
+ const rt = core.createIntlRuntime(defaultLocale ?? locale);
27
+ rt.loadMessages(locale, messages);
28
+ rt.setLocale(locale);
29
+ return rt;
30
+ }, [locale, messages, defaultLocale]);
31
+ return /* @__PURE__ */ jsxRuntime.jsx(IntlContext.Provider, { value: runtime, children });
32
+ }
33
+ function T({ id, values, children }) {
34
+ const runtime = useIntlContext();
35
+ const defaultMessage = typeof children === "string" ? children : "";
36
+ const messageId = id ?? core.generateId({
37
+ text: defaultMessage,
38
+ filePath: "__runtime__",
39
+ context: "T"
40
+ });
41
+ const translated = runtime.t(messageId, values, defaultMessage);
42
+ return /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: translated });
43
+ }
44
+ function useTranslation() {
45
+ const runtime = useIntlContext();
46
+ const t = react.useCallback(
47
+ (id, values, defaultMessage) => runtime.t(id, values, defaultMessage),
48
+ [runtime]
49
+ );
50
+ return { t, locale: runtime.locale };
51
+ }
52
+ function useTranslationRuntime(id, values, defaultMessage) {
53
+ const runtime = useIntlContext();
54
+ return runtime.t(id, values, defaultMessage);
55
+ }
56
+
57
+ exports.IntlContext = IntlContext;
58
+ exports.IntlProvider = IntlProvider;
59
+ exports.T = T;
60
+ exports.useIntlContext = useIntlContext;
61
+ exports.useTranslation = useTranslation;
62
+ exports.useTranslationRuntime = useTranslationRuntime;
63
+ //# sourceMappingURL=index.cjs.map
64
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/context.ts","../src/provider.tsx","../src/t-component.tsx","../src/use-translation.ts"],"names":["createContext","useContext","useMemo","createIntlRuntime","generateId","jsx","Fragment","useCallback"],"mappings":";;;;;;;AAGO,IAAM,WAAA,GAAcA,oBAAkC,IAAI;AAE1D,SAAS,cAAA,GAA8B;AAC5C,EAAA,MAAM,GAAA,GAAMC,iBAAW,WAAW,CAAA;AAClC,EAAA,IAAI,CAAC,GAAA,EAAK;AACR,IAAA,MAAM,IAAI,KAAA;AAAA,MACR;AAAA,KAEF;AAAA,EACF;AACA,EAAA,OAAO,GAAA;AACT;ACHO,SAAS,YAAA,CAAa;AAAA,EAC3B,MAAA;AAAA,EACA,QAAA;AAAA,EACA,aAAA;AAAA,EACA;AACF,CAAA,EAAsB;AACpB,EAAA,MAAM,OAAA,GAAUC,cAAQ,MAAM;AAC5B,IAAA,MAAM,EAAA,GAAKC,sBAAA,CAAkB,aAAA,IAAiB,MAAM,CAAA;AACpD,IAAA,EAAA,CAAG,YAAA,CAAa,QAAQ,QAAQ,CAAA;AAChC,IAAA,EAAA,CAAG,UAAU,MAAM,CAAA;AACnB,IAAA,OAAO,EAAA;AAAA,EACT,CAAA,EAAG,CAAC,MAAA,EAAQ,QAAA,EAAU,aAAa,CAAC,CAAA;AAEpC,EAAA,sCACG,WAAA,CAAY,QAAA,EAAZ,EAAqB,KAAA,EAAO,SAAU,QAAA,EAAS,CAAA;AAEpD;ACLO,SAAS,CAAA,CAAE,EAAE,EAAA,EAAI,MAAA,EAAQ,UAAS,EAAW;AAClD,EAAA,MAAM,UAAU,cAAA,EAAe;AAG/B,EAAA,MAAM,cAAA,GAAiB,OAAO,QAAA,KAAa,QAAA,GAAW,QAAA,GAAW,EAAA;AAEjE,EAAA,MAAM,SAAA,GACJ,MACAC,eAAA,CAAW;AAAA,IACT,IAAA,EAAM,cAAA;AAAA,IACN,QAAA,EAAU,aAAA;AAAA,IACV,OAAA,EAAS;AAAA,GACV,CAAA;AAEH,EAAA,MAAM,UAAA,GAAa,OAAA,CAAQ,CAAA,CAAE,SAAA,EAAW,QAAQ,cAAc,CAAA;AAE9D,EAAA,uBAAOC,cAAAA,CAAAC,mBAAA,EAAA,EAAG,QAAA,EAAA,UAAA,EAAW,CAAA;AACvB;ACnBO,SAAS,cAAA,GAAuC;AACrD,EAAA,MAAM,UAAU,cAAA,EAAe;AAE/B,EAAA,MAAM,CAAA,GAAIC,iBAAA;AAAA,IACR,CAAC,IAAY,MAAA,EAA8B,cAAA,KACzC,QAAQ,CAAA,CAAE,EAAA,EAAI,QAAQ,cAAc,CAAA;AAAA,IACtC,CAAC,OAAO;AAAA,GACV;AAEA,EAAA,OAAO,EAAE,CAAA,EAAG,MAAA,EAAQ,OAAA,CAAQ,MAAA,EAAO;AACrC;AAWO,SAAS,qBAAA,CACd,EAAA,EACA,MAAA,EACA,cAAA,EACQ;AACR,EAAA,MAAM,UAAU,cAAA,EAAe;AAC/B,EAAA,OAAO,OAAA,CAAQ,CAAA,CAAE,EAAA,EAAI,MAAA,EAAQ,cAAc,CAAA;AAC7C","file":"index.cjs","sourcesContent":["import type { IntlRuntime } from \"@better-intl/core\";\nimport { createContext, useContext } from \"react\";\n\nexport const IntlContext = createContext<IntlRuntime | null>(null);\n\nexport function useIntlContext(): IntlRuntime {\n const ctx = useContext(IntlContext);\n if (!ctx) {\n throw new Error(\n \"[@better-intl/react] useTranslation must be used within <IntlProvider>. \" +\n \"Wrap your app with <IntlProvider locale={locale} messages={messages}>.\",\n );\n }\n return ctx;\n}\n","import { createIntlRuntime, type Messages } from \"@better-intl/core\";\nimport { type ReactNode, useMemo } from \"react\";\nimport { IntlContext } from \"./context.js\";\n\nexport interface IntlProviderProps {\n locale: string;\n messages: Messages;\n defaultLocale?: string;\n children: ReactNode;\n}\n\nexport function IntlProvider({\n locale,\n messages,\n defaultLocale,\n children,\n}: IntlProviderProps) {\n const runtime = useMemo(() => {\n const rt = createIntlRuntime(defaultLocale ?? locale);\n rt.loadMessages(locale, messages);\n rt.setLocale(locale);\n return rt;\n }, [locale, messages, defaultLocale]);\n\n return (\n <IntlContext.Provider value={runtime}>{children}</IntlContext.Provider>\n );\n}\n","import type { InterpolationValues } from \"@better-intl/core\";\nimport { generateId } from \"@better-intl/core\";\nimport type { ReactNode } from \"react\";\nimport { useIntlContext } from \"./context.js\";\n\nexport interface TProps {\n /** Explicit message ID (optional — if omitted, generated from children text) */\n id?: string;\n /** Interpolation values */\n values?: InterpolationValues;\n /** Children text acts as default message */\n children: ReactNode;\n}\n\n/**\n * Explicit translation component.\n *\n * @example\n * <T>Hello world</T>\n * <T id=\"custom.key\">Fallback text</T>\n * <T values={{ name: \"Alice\" }}>Hello {\"{name}\"}</T>\n */\nexport function T({ id, values, children }: TProps) {\n const runtime = useIntlContext();\n\n // Extract text from children for default message\n const defaultMessage = typeof children === \"string\" ? children : \"\";\n\n const messageId =\n id ??\n generateId({\n text: defaultMessage,\n filePath: \"__runtime__\",\n context: \"T\",\n });\n\n const translated = runtime.t(messageId, values, defaultMessage);\n\n return <>{translated}</>;\n}\n","import type { InterpolationValues } from \"@better-intl/core\";\nimport { useCallback } from \"react\";\nimport { useIntlContext } from \"./context.js\";\n\nexport interface UseTranslationReturn {\n t: (\n id: string,\n values?: InterpolationValues,\n defaultMessage?: string,\n ) => string;\n locale: string;\n}\n\n/**\n * Hook for translating messages.\n *\n * @example\n * const { t, locale } = useTranslation();\n * return <h1>{t(\"greeting\", { name: \"World\" })}</h1>;\n */\nexport function useTranslation(): UseTranslationReturn {\n const runtime = useIntlContext();\n\n const t = useCallback(\n (id: string, values?: InterpolationValues, defaultMessage?: string) =>\n runtime.t(id, values, defaultMessage),\n [runtime],\n );\n\n return { t, locale: runtime.locale };\n}\n\n/**\n * Standalone `t` accessor for compiler-generated code.\n * The compiler transforms `<h1>Hello</h1>` into `<h1>{__t(\"id\")}</h1>`\n * and adds `import { useTranslationRuntime as __t } from \"@better-intl/react\"`.\n *\n * This is intentionally NOT a hook — it's called inline per expression.\n * The actual hook call happens inside, which is valid because this function\n * is always called during render of a component wrapped in IntlProvider.\n */\nexport function useTranslationRuntime(\n id: string,\n values?: InterpolationValues,\n defaultMessage?: string,\n): string {\n const runtime = useIntlContext();\n return runtime.t(id, values, defaultMessage);\n}\n"]}
@@ -0,0 +1,58 @@
1
+ import * as react from 'react';
2
+ import { ReactNode } from 'react';
3
+ import { IntlRuntime, Messages, InterpolationValues } from '@better-intl/core';
4
+ import * as react_jsx_runtime from 'react/jsx-runtime';
5
+
6
+ declare const IntlContext: react.Context<IntlRuntime | null>;
7
+ declare function useIntlContext(): IntlRuntime;
8
+
9
+ interface IntlProviderProps {
10
+ locale: string;
11
+ messages: Messages;
12
+ defaultLocale?: string;
13
+ children: ReactNode;
14
+ }
15
+ declare function IntlProvider({ locale, messages, defaultLocale, children, }: IntlProviderProps): react_jsx_runtime.JSX.Element;
16
+
17
+ interface TProps {
18
+ /** Explicit message ID (optional — if omitted, generated from children text) */
19
+ id?: string;
20
+ /** Interpolation values */
21
+ values?: InterpolationValues;
22
+ /** Children text acts as default message */
23
+ children: ReactNode;
24
+ }
25
+ /**
26
+ * Explicit translation component.
27
+ *
28
+ * @example
29
+ * <T>Hello world</T>
30
+ * <T id="custom.key">Fallback text</T>
31
+ * <T values={{ name: "Alice" }}>Hello {"{name}"}</T>
32
+ */
33
+ declare function T({ id, values, children }: TProps): react_jsx_runtime.JSX.Element;
34
+
35
+ interface UseTranslationReturn {
36
+ t: (id: string, values?: InterpolationValues, defaultMessage?: string) => string;
37
+ locale: string;
38
+ }
39
+ /**
40
+ * Hook for translating messages.
41
+ *
42
+ * @example
43
+ * const { t, locale } = useTranslation();
44
+ * return <h1>{t("greeting", { name: "World" })}</h1>;
45
+ */
46
+ declare function useTranslation(): UseTranslationReturn;
47
+ /**
48
+ * Standalone `t` accessor for compiler-generated code.
49
+ * The compiler transforms `<h1>Hello</h1>` into `<h1>{__t("id")}</h1>`
50
+ * and adds `import { useTranslationRuntime as __t } from "@better-intl/react"`.
51
+ *
52
+ * This is intentionally NOT a hook — it's called inline per expression.
53
+ * The actual hook call happens inside, which is valid because this function
54
+ * is always called during render of a component wrapped in IntlProvider.
55
+ */
56
+ declare function useTranslationRuntime(id: string, values?: InterpolationValues, defaultMessage?: string): string;
57
+
58
+ export { IntlContext, IntlProvider, type IntlProviderProps, T, type TProps, type UseTranslationReturn, useIntlContext, useTranslation, useTranslationRuntime };
@@ -0,0 +1,58 @@
1
+ import * as react from 'react';
2
+ import { ReactNode } from 'react';
3
+ import { IntlRuntime, Messages, InterpolationValues } from '@better-intl/core';
4
+ import * as react_jsx_runtime from 'react/jsx-runtime';
5
+
6
+ declare const IntlContext: react.Context<IntlRuntime | null>;
7
+ declare function useIntlContext(): IntlRuntime;
8
+
9
+ interface IntlProviderProps {
10
+ locale: string;
11
+ messages: Messages;
12
+ defaultLocale?: string;
13
+ children: ReactNode;
14
+ }
15
+ declare function IntlProvider({ locale, messages, defaultLocale, children, }: IntlProviderProps): react_jsx_runtime.JSX.Element;
16
+
17
+ interface TProps {
18
+ /** Explicit message ID (optional — if omitted, generated from children text) */
19
+ id?: string;
20
+ /** Interpolation values */
21
+ values?: InterpolationValues;
22
+ /** Children text acts as default message */
23
+ children: ReactNode;
24
+ }
25
+ /**
26
+ * Explicit translation component.
27
+ *
28
+ * @example
29
+ * <T>Hello world</T>
30
+ * <T id="custom.key">Fallback text</T>
31
+ * <T values={{ name: "Alice" }}>Hello {"{name}"}</T>
32
+ */
33
+ declare function T({ id, values, children }: TProps): react_jsx_runtime.JSX.Element;
34
+
35
+ interface UseTranslationReturn {
36
+ t: (id: string, values?: InterpolationValues, defaultMessage?: string) => string;
37
+ locale: string;
38
+ }
39
+ /**
40
+ * Hook for translating messages.
41
+ *
42
+ * @example
43
+ * const { t, locale } = useTranslation();
44
+ * return <h1>{t("greeting", { name: "World" })}</h1>;
45
+ */
46
+ declare function useTranslation(): UseTranslationReturn;
47
+ /**
48
+ * Standalone `t` accessor for compiler-generated code.
49
+ * The compiler transforms `<h1>Hello</h1>` into `<h1>{__t("id")}</h1>`
50
+ * and adds `import { useTranslationRuntime as __t } from "@better-intl/react"`.
51
+ *
52
+ * This is intentionally NOT a hook — it's called inline per expression.
53
+ * The actual hook call happens inside, which is valid because this function
54
+ * is always called during render of a component wrapped in IntlProvider.
55
+ */
56
+ declare function useTranslationRuntime(id: string, values?: InterpolationValues, defaultMessage?: string): string;
57
+
58
+ export { IntlContext, IntlProvider, type IntlProviderProps, T, type TProps, type UseTranslationReturn, useIntlContext, useTranslation, useTranslationRuntime };
package/dist/index.js ADDED
@@ -0,0 +1,57 @@
1
+ "use client";
2
+ import { createContext, useContext, useMemo, useCallback } from 'react';
3
+ import { createIntlRuntime, generateId } from '@better-intl/core';
4
+ import { jsx, Fragment } from 'react/jsx-runtime';
5
+
6
+ // src/context.ts
7
+ var IntlContext = createContext(null);
8
+ function useIntlContext() {
9
+ const ctx = useContext(IntlContext);
10
+ if (!ctx) {
11
+ throw new Error(
12
+ "[@better-intl/react] useTranslation must be used within <IntlProvider>. Wrap your app with <IntlProvider locale={locale} messages={messages}>."
13
+ );
14
+ }
15
+ return ctx;
16
+ }
17
+ function IntlProvider({
18
+ locale,
19
+ messages,
20
+ defaultLocale,
21
+ children
22
+ }) {
23
+ const runtime = useMemo(() => {
24
+ const rt = createIntlRuntime(defaultLocale ?? locale);
25
+ rt.loadMessages(locale, messages);
26
+ rt.setLocale(locale);
27
+ return rt;
28
+ }, [locale, messages, defaultLocale]);
29
+ return /* @__PURE__ */ jsx(IntlContext.Provider, { value: runtime, children });
30
+ }
31
+ function T({ id, values, children }) {
32
+ const runtime = useIntlContext();
33
+ const defaultMessage = typeof children === "string" ? children : "";
34
+ const messageId = id ?? generateId({
35
+ text: defaultMessage,
36
+ filePath: "__runtime__",
37
+ context: "T"
38
+ });
39
+ const translated = runtime.t(messageId, values, defaultMessage);
40
+ return /* @__PURE__ */ jsx(Fragment, { children: translated });
41
+ }
42
+ function useTranslation() {
43
+ const runtime = useIntlContext();
44
+ const t = useCallback(
45
+ (id, values, defaultMessage) => runtime.t(id, values, defaultMessage),
46
+ [runtime]
47
+ );
48
+ return { t, locale: runtime.locale };
49
+ }
50
+ function useTranslationRuntime(id, values, defaultMessage) {
51
+ const runtime = useIntlContext();
52
+ return runtime.t(id, values, defaultMessage);
53
+ }
54
+
55
+ export { IntlContext, IntlProvider, T, useIntlContext, useTranslation, useTranslationRuntime };
56
+ //# sourceMappingURL=index.js.map
57
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/context.ts","../src/provider.tsx","../src/t-component.tsx","../src/use-translation.ts"],"names":["jsx"],"mappings":";;;;;AAGO,IAAM,WAAA,GAAc,cAAkC,IAAI;AAE1D,SAAS,cAAA,GAA8B;AAC5C,EAAA,MAAM,GAAA,GAAM,WAAW,WAAW,CAAA;AAClC,EAAA,IAAI,CAAC,GAAA,EAAK;AACR,IAAA,MAAM,IAAI,KAAA;AAAA,MACR;AAAA,KAEF;AAAA,EACF;AACA,EAAA,OAAO,GAAA;AACT;ACHO,SAAS,YAAA,CAAa;AAAA,EAC3B,MAAA;AAAA,EACA,QAAA;AAAA,EACA,aAAA;AAAA,EACA;AACF,CAAA,EAAsB;AACpB,EAAA,MAAM,OAAA,GAAU,QAAQ,MAAM;AAC5B,IAAA,MAAM,EAAA,GAAK,iBAAA,CAAkB,aAAA,IAAiB,MAAM,CAAA;AACpD,IAAA,EAAA,CAAG,YAAA,CAAa,QAAQ,QAAQ,CAAA;AAChC,IAAA,EAAA,CAAG,UAAU,MAAM,CAAA;AACnB,IAAA,OAAO,EAAA;AAAA,EACT,CAAA,EAAG,CAAC,MAAA,EAAQ,QAAA,EAAU,aAAa,CAAC,CAAA;AAEpC,EAAA,2BACG,WAAA,CAAY,QAAA,EAAZ,EAAqB,KAAA,EAAO,SAAU,QAAA,EAAS,CAAA;AAEpD;ACLO,SAAS,CAAA,CAAE,EAAE,EAAA,EAAI,MAAA,EAAQ,UAAS,EAAW;AAClD,EAAA,MAAM,UAAU,cAAA,EAAe;AAG/B,EAAA,MAAM,cAAA,GAAiB,OAAO,QAAA,KAAa,QAAA,GAAW,QAAA,GAAW,EAAA;AAEjE,EAAA,MAAM,SAAA,GACJ,MACA,UAAA,CAAW;AAAA,IACT,IAAA,EAAM,cAAA;AAAA,IACN,QAAA,EAAU,aAAA;AAAA,IACV,OAAA,EAAS;AAAA,GACV,CAAA;AAEH,EAAA,MAAM,UAAA,GAAa,OAAA,CAAQ,CAAA,CAAE,SAAA,EAAW,QAAQ,cAAc,CAAA;AAE9D,EAAA,uBAAOA,GAAAA,CAAA,QAAA,EAAA,EAAG,QAAA,EAAA,UAAA,EAAW,CAAA;AACvB;ACnBO,SAAS,cAAA,GAAuC;AACrD,EAAA,MAAM,UAAU,cAAA,EAAe;AAE/B,EAAA,MAAM,CAAA,GAAI,WAAA;AAAA,IACR,CAAC,IAAY,MAAA,EAA8B,cAAA,KACzC,QAAQ,CAAA,CAAE,EAAA,EAAI,QAAQ,cAAc,CAAA;AAAA,IACtC,CAAC,OAAO;AAAA,GACV;AAEA,EAAA,OAAO,EAAE,CAAA,EAAG,MAAA,EAAQ,OAAA,CAAQ,MAAA,EAAO;AACrC;AAWO,SAAS,qBAAA,CACd,EAAA,EACA,MAAA,EACA,cAAA,EACQ;AACR,EAAA,MAAM,UAAU,cAAA,EAAe;AAC/B,EAAA,OAAO,OAAA,CAAQ,CAAA,CAAE,EAAA,EAAI,MAAA,EAAQ,cAAc,CAAA;AAC7C","file":"index.js","sourcesContent":["import type { IntlRuntime } from \"@better-intl/core\";\nimport { createContext, useContext } from \"react\";\n\nexport const IntlContext = createContext<IntlRuntime | null>(null);\n\nexport function useIntlContext(): IntlRuntime {\n const ctx = useContext(IntlContext);\n if (!ctx) {\n throw new Error(\n \"[@better-intl/react] useTranslation must be used within <IntlProvider>. \" +\n \"Wrap your app with <IntlProvider locale={locale} messages={messages}>.\",\n );\n }\n return ctx;\n}\n","import { createIntlRuntime, type Messages } from \"@better-intl/core\";\nimport { type ReactNode, useMemo } from \"react\";\nimport { IntlContext } from \"./context.js\";\n\nexport interface IntlProviderProps {\n locale: string;\n messages: Messages;\n defaultLocale?: string;\n children: ReactNode;\n}\n\nexport function IntlProvider({\n locale,\n messages,\n defaultLocale,\n children,\n}: IntlProviderProps) {\n const runtime = useMemo(() => {\n const rt = createIntlRuntime(defaultLocale ?? locale);\n rt.loadMessages(locale, messages);\n rt.setLocale(locale);\n return rt;\n }, [locale, messages, defaultLocale]);\n\n return (\n <IntlContext.Provider value={runtime}>{children}</IntlContext.Provider>\n );\n}\n","import type { InterpolationValues } from \"@better-intl/core\";\nimport { generateId } from \"@better-intl/core\";\nimport type { ReactNode } from \"react\";\nimport { useIntlContext } from \"./context.js\";\n\nexport interface TProps {\n /** Explicit message ID (optional — if omitted, generated from children text) */\n id?: string;\n /** Interpolation values */\n values?: InterpolationValues;\n /** Children text acts as default message */\n children: ReactNode;\n}\n\n/**\n * Explicit translation component.\n *\n * @example\n * <T>Hello world</T>\n * <T id=\"custom.key\">Fallback text</T>\n * <T values={{ name: \"Alice\" }}>Hello {\"{name}\"}</T>\n */\nexport function T({ id, values, children }: TProps) {\n const runtime = useIntlContext();\n\n // Extract text from children for default message\n const defaultMessage = typeof children === \"string\" ? children : \"\";\n\n const messageId =\n id ??\n generateId({\n text: defaultMessage,\n filePath: \"__runtime__\",\n context: \"T\",\n });\n\n const translated = runtime.t(messageId, values, defaultMessage);\n\n return <>{translated}</>;\n}\n","import type { InterpolationValues } from \"@better-intl/core\";\nimport { useCallback } from \"react\";\nimport { useIntlContext } from \"./context.js\";\n\nexport interface UseTranslationReturn {\n t: (\n id: string,\n values?: InterpolationValues,\n defaultMessage?: string,\n ) => string;\n locale: string;\n}\n\n/**\n * Hook for translating messages.\n *\n * @example\n * const { t, locale } = useTranslation();\n * return <h1>{t(\"greeting\", { name: \"World\" })}</h1>;\n */\nexport function useTranslation(): UseTranslationReturn {\n const runtime = useIntlContext();\n\n const t = useCallback(\n (id: string, values?: InterpolationValues, defaultMessage?: string) =>\n runtime.t(id, values, defaultMessage),\n [runtime],\n );\n\n return { t, locale: runtime.locale };\n}\n\n/**\n * Standalone `t` accessor for compiler-generated code.\n * The compiler transforms `<h1>Hello</h1>` into `<h1>{__t(\"id\")}</h1>`\n * and adds `import { useTranslationRuntime as __t } from \"@better-intl/react\"`.\n *\n * This is intentionally NOT a hook — it's called inline per expression.\n * The actual hook call happens inside, which is valid because this function\n * is always called during render of a component wrapped in IntlProvider.\n */\nexport function useTranslationRuntime(\n id: string,\n values?: InterpolationValues,\n defaultMessage?: string,\n): string {\n const runtime = useIntlContext();\n return runtime.t(id, values, defaultMessage);\n}\n"]}
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "@better-intl/react",
3
+ "version": "0.1.0",
4
+ "description": "React bindings for better-intl — Provider, hooks, <T> component",
5
+ "type": "module",
6
+ "main": "./dist/index.cjs",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "import": "./dist/index.js",
12
+ "require": "./dist/index.cjs",
13
+ "types": "./dist/index.d.ts"
14
+ }
15
+ },
16
+ "files": [
17
+ "dist"
18
+ ],
19
+ "dependencies": {
20
+ "@better-intl/core": "0.1.0"
21
+ },
22
+ "peerDependencies": {
23
+ "react": "^19.0.0"
24
+ },
25
+ "sideEffects": false,
26
+ "license": "MIT",
27
+ "scripts": {
28
+ "build": "tsup",
29
+ "dev": "tsup --watch"
30
+ }
31
+ }