@better-translate/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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Jorge Alvarenga
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 ADDED
@@ -0,0 +1,5 @@
1
+ # @better-translate/react
2
+
3
+ `@better-translate/react` adds a provider and hooks on top of `@better-translate/core`. Use it in React web apps and Expo apps when components need access to translations and locale switching.
4
+
5
+ Full docs: [better-translate-placeholder.com/en/docs/adapters/react](https://better-translate-placeholder.com/en/docs/adapters/react)
@@ -0,0 +1,3 @@
1
+ import type { AnyUseTranslationsValue } from "./types.js";
2
+ export declare const BetterTranslateContext: import("react").Context<AnyUseTranslationsValue | null>;
3
+ //# sourceMappingURL=context.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,YAAY,CAAC;AAE1D,eAAO,MAAM,sBAAsB,yDACkB,CAAC"}
@@ -0,0 +1,4 @@
1
+ export { BetterTranslateProvider } from "./provider.js";
2
+ export { useTranslations } from "./use-translations.js";
3
+ export type { AnyBetterTranslateTranslator, BetterTranslateProviderProps, InferLocale, InferMessages, UseTranslationsValue, } from "./types.js";
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,uBAAuB,EAAE,MAAM,eAAe,CAAC;AACxD,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAExD,YAAY,EACV,4BAA4B,EAC5B,4BAA4B,EAC5B,WAAW,EACX,aAAa,EACb,oBAAoB,GACrB,MAAM,YAAY,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,130 @@
1
+ // src/provider.tsx
2
+ import { useEffect, useReducer, useRef, useState, useTransition } from "react";
3
+
4
+ // src/context.ts
5
+ import { createContext } from "react";
6
+ var BetterTranslateContext = createContext(null);
7
+
8
+ // src/provider.tsx
9
+ import { jsx } from "react/jsx-runtime";
10
+ function BetterTranslateProvider({
11
+ children,
12
+ initialLocale,
13
+ translator
14
+ }) {
15
+ const [locale, setLocaleState] = useState(
16
+ initialLocale ?? translator.defaultLocale
17
+ );
18
+ const [localeError, setLocaleError] = useState(null);
19
+ const [loadingLocale, setLoadingLocale] = useState(null);
20
+ const [, refreshMessages] = useReducer((count) => count + 1, 0);
21
+ const [isPending, startTransition] = useTransition();
22
+ const requestIdRef = useRef(0);
23
+ useEffect(() => {
24
+ requestIdRef.current += 1;
25
+ setLocaleState(
26
+ initialLocale ?? translator.defaultLocale
27
+ );
28
+ setLocaleError(null);
29
+ setLoadingLocale(null);
30
+ refreshMessages();
31
+ }, [initialLocale, translator]);
32
+ async function loadLocale(nextLocale) {
33
+ setLocaleError(null);
34
+ setLoadingLocale(nextLocale);
35
+ try {
36
+ const loadedLocale = await translator.loadLocale(nextLocale);
37
+ startTransition(() => {
38
+ refreshMessages();
39
+ });
40
+ return loadedLocale;
41
+ } catch (error) {
42
+ setLocaleError(error);
43
+ throw error;
44
+ } finally {
45
+ setLoadingLocale(
46
+ (currentLocale) => currentLocale === nextLocale ? null : currentLocale
47
+ );
48
+ }
49
+ }
50
+ async function setLocale(nextLocale) {
51
+ if (locale === nextLocale) {
52
+ return;
53
+ }
54
+ const requestId = ++requestIdRef.current;
55
+ setLocaleError(null);
56
+ const hasCachedMessages = Object.prototype.hasOwnProperty.call(
57
+ translator.getMessages(),
58
+ nextLocale
59
+ );
60
+ if (!hasCachedMessages) {
61
+ setLoadingLocale(nextLocale);
62
+ try {
63
+ await translator.loadLocale(nextLocale);
64
+ } catch (error) {
65
+ if (requestId === requestIdRef.current) {
66
+ setLocaleError(error);
67
+ setLoadingLocale(null);
68
+ }
69
+ return;
70
+ }
71
+ }
72
+ if (requestId !== requestIdRef.current) {
73
+ return;
74
+ }
75
+ startTransition(() => {
76
+ setLocaleState(nextLocale);
77
+ setLocaleError(null);
78
+ setLoadingLocale(null);
79
+ refreshMessages();
80
+ });
81
+ }
82
+ const translate = ((...args) => {
83
+ const [key, options] = args;
84
+ return translator.t(
85
+ key,
86
+ {
87
+ ...options,
88
+ locale: options?.locale ?? locale
89
+ }
90
+ );
91
+ });
92
+ const value = {
93
+ availableLanguages: translator.getAvailableLanguages(),
94
+ defaultLocale: translator.defaultLocale,
95
+ direction: translator.getDirection({
96
+ locale
97
+ }),
98
+ fallbackLocale: translator.fallbackLocale,
99
+ isLoadingLocale: loadingLocale !== null || isPending,
100
+ loadLocale,
101
+ loadingLocale,
102
+ locale,
103
+ localeError,
104
+ messages: translator.getMessages(),
105
+ rtl: translator.isRtl({
106
+ locale
107
+ }),
108
+ setLocale,
109
+ supportedLocales: translator.getSupportedLocales(),
110
+ t: translate,
111
+ translator
112
+ };
113
+ return /* @__PURE__ */ jsx(BetterTranslateContext.Provider, { value, children });
114
+ }
115
+
116
+ // src/use-translations.ts
117
+ import { useContext } from "react";
118
+ function useTranslations() {
119
+ const context = useContext(BetterTranslateContext);
120
+ if (!context) {
121
+ throw new Error(
122
+ "useTranslations() must be used inside <BetterTranslateProvider />."
123
+ );
124
+ }
125
+ return context;
126
+ }
127
+ export {
128
+ BetterTranslateProvider,
129
+ useTranslations
130
+ };
@@ -0,0 +1,9 @@
1
+ import type { AnyBetterTranslateTranslator, BetterTranslateProviderProps } from "./types.js";
2
+ /**
3
+ * Provides a configured Better Translate translator to a React subtree.
4
+ *
5
+ * The provider owns the active locale state and exposes locale-bound helpers
6
+ * through `useTranslations()`.
7
+ */
8
+ export declare function BetterTranslateProvider<TTranslator extends AnyBetterTranslateTranslator>({ children, initialLocale, translator, }: BetterTranslateProviderProps<TTranslator>): import("react/jsx-runtime").JSX.Element;
9
+ //# sourceMappingURL=provider.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../src/provider.tsx"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACV,4BAA4B,EAC5B,4BAA4B,EAI7B,MAAM,YAAY,CAAC;AAOpB;;;;;GAKG;AACH,wBAAgB,uBAAuB,CACrC,WAAW,SAAS,4BAA4B,EAChD,EACA,QAAQ,EACR,aAAa,EACb,UAAU,GACX,EAAE,4BAA4B,CAAC,WAAW,CAAC,2CAsI3C"}
@@ -0,0 +1,29 @@
1
+ import type { CachedMessages, ConfiguredTranslator, DeepPartialMessages, TranslationDirection, TranslationLanguageMetadata, TranslateFunction, TranslationMessages } from "@better-translate/core";
2
+ import type { ReactNode } from "react";
3
+ export type AnyBetterTranslateTranslator = ConfiguredTranslator<any, TranslationMessages>;
4
+ export type InferLocale<TTranslator extends AnyBetterTranslateTranslator> = TTranslator extends ConfiguredTranslator<infer TLocale, TranslationMessages> ? TLocale : never;
5
+ export type InferMessages<TTranslator extends AnyBetterTranslateTranslator> = TTranslator extends ConfiguredTranslator<any, infer TSourceMessages> ? TSourceMessages : TranslationMessages;
6
+ export interface BetterTranslateProviderProps<TTranslator extends AnyBetterTranslateTranslator> {
7
+ children: ReactNode;
8
+ initialLocale?: InferLocale<TTranslator>;
9
+ translator: TTranslator;
10
+ }
11
+ export interface UseTranslationsValue<TTranslator extends AnyBetterTranslateTranslator> {
12
+ availableLanguages: readonly TranslationLanguageMetadata<InferLocale<TTranslator>>[];
13
+ defaultLocale: InferLocale<TTranslator>;
14
+ direction: TranslationDirection;
15
+ fallbackLocale: InferLocale<TTranslator>;
16
+ isLoadingLocale: boolean;
17
+ loadLocale(locale: InferLocale<TTranslator>): Promise<DeepPartialMessages<InferMessages<TTranslator>> | InferMessages<TTranslator> | undefined>;
18
+ loadingLocale: InferLocale<TTranslator> | null;
19
+ locale: InferLocale<TTranslator>;
20
+ localeError: unknown;
21
+ messages: CachedMessages<InferLocale<TTranslator>, InferMessages<TTranslator>>;
22
+ rtl: boolean;
23
+ setLocale(locale: InferLocale<TTranslator>): Promise<void>;
24
+ supportedLocales: readonly InferLocale<TTranslator>[];
25
+ t: TranslateFunction<InferLocale<TTranslator>, InferMessages<TTranslator>>;
26
+ translator: TTranslator;
27
+ }
28
+ export type AnyUseTranslationsValue = UseTranslationsValue<AnyBetterTranslateTranslator>;
29
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,cAAc,EACd,oBAAoB,EACpB,mBAAmB,EACnB,oBAAoB,EACpB,2BAA2B,EAC3B,iBAAiB,EACjB,mBAAmB,EACpB,MAAM,wBAAwB,CAAC;AAChC,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAEvC,MAAM,MAAM,4BAA4B,GAAG,oBAAoB,CAC7D,GAAG,EAC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S,CAAC,MAAM,EAAE,WAAW,CAAC,WAAW,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC3D,gBAAgB,EAAE,SAAS,WAAW,CAAC,WAAW,CAAC,EAAE,CAAC;IACtD,CAAC,EAAE,iBAAiB,CAAC,WAAW,CAAC,WAAW,CAAC,EAAE,aAAa,CAAC,WAAW,CAAC,CAAC,CAAC;IAC3E,UAAU,EAAE,WAAW,CAAC;CACzB;AAED,MAAM,MAAM,uBAAuB,GACjC,oBAAoB,CAAC,4BAA4B,CAAC,CAAC"}
@@ -0,0 +1,8 @@
1
+ import type { AnyBetterTranslateTranslator, UseTranslationsValue } from "./types.js";
2
+ /**
3
+ * Returns the Better Translate React context for the current subtree.
4
+ *
5
+ * Throws when used outside `BetterTranslateProvider`.
6
+ */
7
+ export declare function useTranslations<TTranslator extends AnyBetterTranslateTranslator = AnyBetterTranslateTranslator>(): UseTranslationsValue<TTranslator>;
8
+ //# sourceMappingURL=use-translations.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-translations.d.ts","sourceRoot":"","sources":["../src/use-translations.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACV,4BAA4B,EAC5B,oBAAoB,EACrB,MAAM,YAAY,CAAC;AAEpB;;;;GAIG;AACH,wBAAgB,eAAe,CAC7B,WAAW,SACT,4BAA4B,GAAG,4BAA4B,KAC1D,oBAAoB,CAAC,WAAW,CAAC,CAUrC"}
package/package.json ADDED
@@ -0,0 +1,56 @@
1
+ {
2
+ "name": "@better-translate/react",
3
+ "version": "1.0.0",
4
+ "description": "React context and hooks for Better Translate in web and native React apps.",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "sideEffects": false,
8
+ "main": "./dist/index.js",
9
+ "module": "./dist/index.js",
10
+ "types": "./dist/index.d.ts",
11
+ "exports": {
12
+ ".": {
13
+ "types": "./dist/index.d.ts",
14
+ "import": "./dist/index.js"
15
+ },
16
+ "./package.json": "./package.json"
17
+ },
18
+ "files": ["dist", "LICENSE", "README.md"],
19
+ "scripts": {
20
+ "build": "tsup src/index.ts --format esm --clean && tsc -p tsconfig.build.json",
21
+ "check-types": "tsc --noEmit",
22
+ "test": "bun test tests/runtime",
23
+ "test:types": "tsc -p tests/types/tsconfig.json --noEmit"
24
+ },
25
+ "publishConfig": {
26
+ "access": "public"
27
+ },
28
+ "repository": {
29
+ "type": "git",
30
+ "url": "git+https://github.com/jralvarenga/better-translate.git",
31
+ "directory": "packages/react"
32
+ },
33
+ "homepage": "https://github.com/jralvarenga/better-translate/tree/main/packages/react#readme",
34
+ "bugs": {
35
+ "url": "https://github.com/jralvarenga/better-translate/issues"
36
+ },
37
+ "dependencies": {
38
+ "@better-translate/core": "workspace:^"
39
+ },
40
+ "peerDependencies": {
41
+ "react": "^19.0.0"
42
+ },
43
+ "devDependencies": {
44
+ "@repo/typescript-config": "*",
45
+ "react-test-renderer": "^19.2.0"
46
+ },
47
+ "keywords": [
48
+ "expo",
49
+ "i18n",
50
+ "l10n",
51
+ "react",
52
+ "react-native",
53
+ "translations",
54
+ "typescript"
55
+ ]
56
+ }