@verbaly/react 0.79.0 → 0.80.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/README.md CHANGED
@@ -18,9 +18,13 @@ React hooks for [Verbaly](https://github.com/AronSoto/verbaly): a thin layer (Re
18
18
  ## 🚀 Install
19
19
 
20
20
  ```bash
21
- pnpm add verbaly @verbaly/react
21
+ pnpm add verbaly @verbaly/react @verbaly/vite # @verbaly/unplugin on webpack, Rollup, esbuild or Rspack
22
+ pnpm add -D @verbaly/compiler # the verbaly command: init, extract, check
23
+ npx verbaly init
22
24
  ```
23
25
 
26
+ Add `verbaly()` to the plugins in `vite.config` (the [`@verbaly/vite`](https://www.npmjs.com/package/@verbaly/vite) README shows it). It writes `verbaly/app`, the module these hooks read. In Next.js, use [`@verbaly/next`](https://www.npmjs.com/package/@verbaly/next) instead, which re-exports them.
27
+
24
28
  ```tsx
25
29
  import { VerbalyProvider, useT, useLocale } from '@verbaly/react';
26
30
  import { verbaly } from 'verbaly/app';
@@ -33,9 +37,33 @@ function Inbox() {
33
37
  const t = useT();
34
38
  return <p>{t('inbox', { count: 3 })}</p>;
35
39
  }
40
+
41
+ function LanguagePicker() {
42
+ const [locale, setLocale] = useLocale();
43
+ return (
44
+ <select value={locale} onChange={(event) => setLocale(event.target.value)}>
45
+ <option value="en">English</option>
46
+ <option value="es">Español</option>
47
+ </select>
48
+ );
49
+ }
50
+ ```
51
+
52
+ **The provider is optional in the browser.** With none above it, a hook reads the instance `verbaly/app` creates, so a single-page app can skip it and a component test renders with no wrapper, as with react-i18next. The Vite plugin and unplugin load `verbaly/app` in every file that imports `@verbaly/react`, so that instance is there before the first render. On a server the provider is required, since every request has its own instance.
53
+
54
+ **Switching languages.** `setLocale` from `useLocale()` switches every component, and the page follows: `<html lang>`, `<html dir>` and the choice, saved in the browser. Where a server renders the page, as in Next.js, switch with `useSwitchLocale` from `@verbaly/next/client` instead: it also sets the cookie the server reads. A single-page app starts in its source language, so read that choice back before the first render:
55
+
56
+ ```tsx
57
+ // main.tsx: the saved choice, else the browser's, loaded before anything renders
58
+ import { resolveLocale } from 'verbaly';
59
+ import { locales, setLocale } from 'verbaly/app';
60
+
61
+ setLocale(resolveLocale({ supported: locales, path: false })).then(() => {
62
+ createRoot(document.getElementById('root')!).render(<App />);
63
+ });
36
64
  ```
37
65
 
38
- **The provider is optional in the browser.** With none above it, a hook reads the instance `verbaly/app` creates, so a single-page app can skip it and a component test renders with no wrapper, as with react-i18next. On a server it is required, since every request has its own instance.
66
+ `.then` rather than a top-level `await`, which can split the entry into more chunks.
39
67
 
40
68
  **Keys are typed.** `useT()` checks every key and its params against your source catalog, through the types Verbaly generates (`src/verbaly.d.ts` in a Vite app): a key no catalog has does not compile. A key that comes from data, like `t(item.key)` or a variable typed as a union of keys, passes as it is, and its params are left open; declare it with `defineKeys` from `verbaly/app` when you want `check` to verify it exists.
41
69
 
package/dist/index.cjs CHANGED
@@ -1,6 +1,7 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
2
  const require_render = require("./render-4doKDlxH.cjs");
3
3
  let react = require("react");
4
+ let verbaly = require("verbaly");
4
5
  // src/index.ts
5
6
  const VerbalyContext = (0, react.createContext)(null);
6
7
  const APP_INSTANCE = Symbol.for("verbaly.app");
@@ -30,7 +31,14 @@ function bound(t) {
30
31
  function useLocale() {
31
32
  const instance = useVerbaly();
32
33
  useVersion(instance);
33
- return [instance.locale, instance.setLocale];
34
+ const set = (0, react.useMemo)(() => switcher(instance), [instance]);
35
+ return [instance.locale, set];
36
+ }
37
+ function switcher(instance) {
38
+ return (locale) => {
39
+ instance.setLocale(locale);
40
+ (0, verbaly.persistLocale)(locale);
41
+ };
34
42
  }
35
43
  function useVersion(instance) {
36
44
  return (0, react.useSyncExternalStore)(instance.subscribe, () => instance.version, () => instance.version);
package/dist/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { t as renderTrans } from "./render-DitbbbzJ.js";
2
2
  import { createContext, createElement, useContext, useMemo, useSyncExternalStore } from "react";
3
+ import { persistLocale } from "verbaly";
3
4
  // src/index.ts
4
5
  const VerbalyContext = createContext(null);
5
6
  const APP_INSTANCE = Symbol.for("verbaly.app");
@@ -29,7 +30,14 @@ function bound(t) {
29
30
  function useLocale() {
30
31
  const instance = useVerbaly();
31
32
  useVersion(instance);
32
- return [instance.locale, instance.setLocale];
33
+ const set = useMemo(() => switcher(instance), [instance]);
34
+ return [instance.locale, set];
35
+ }
36
+ function switcher(instance) {
37
+ return (locale) => {
38
+ instance.setLocale(locale);
39
+ persistLocale(locale);
40
+ };
33
41
  }
34
42
  function useVersion(instance) {
35
43
  return useSyncExternalStore(instance.subscribe, () => instance.version, () => instance.version);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@verbaly/react",
3
- "version": "0.79.0",
3
+ "version": "0.80.0",
4
4
  "description": "React bindings for Verbaly: hooks over the reactive core.",
5
5
  "keywords": [
6
6
  "i18n",
@@ -62,7 +62,7 @@
62
62
  },
63
63
  "peerDependencies": {
64
64
  "react": "^18.0.0 || ^19.0.0",
65
- "verbaly": "^0.79.0"
65
+ "verbaly": "^0.80.0"
66
66
  },
67
67
  "devDependencies": {
68
68
  "@types/react": "^19.3.0",
@@ -72,7 +72,7 @@
72
72
  "preact-render-to-string": "^6.8.2",
73
73
  "react": "^19.3.1",
74
74
  "react-dom": "^19.3.1",
75
- "verbaly": "^0.79.0"
75
+ "verbaly": "^0.80.0"
76
76
  },
77
77
  "scripts": {
78
78
  "build": "tsdown",