@weglot/translate-react 0.0.0-stage → 0.2.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 +202 -0
- package/NOTICE +6 -0
- package/README.md +199 -2
- package/build/_virtual/rolldown_runtime.cjs +29 -0
- package/build/_virtual/rolldown_runtime.mjs +7 -0
- package/build/index.cjs +0 -0
- package/build/index.d.cts +2 -0
- package/build/index.d.mts +2 -0
- package/build/index.mjs +1 -0
- package/build/locale.d.cts +6 -0
- package/build/locale.d.cts.map +1 -0
- package/build/locale.d.mts +6 -0
- package/build/locale.d.mts.map +1 -0
- package/build/metadata/manager.cjs +119 -0
- package/build/metadata/manager.mjs +114 -0
- package/build/metadata/manager.mjs.map +1 -0
- package/build/plugin/build-translator.cjs +210 -0
- package/build/plugin/build-translator.mjs +208 -0
- package/build/plugin/build-translator.mjs.map +1 -0
- package/build/plugin/cleanup.cjs +20 -0
- package/build/plugin/cleanup.mjs +20 -0
- package/build/plugin/cleanup.mjs.map +1 -0
- package/build/plugin/index.cjs +0 -0
- package/build/plugin/index.d.cts +2 -0
- package/build/plugin/index.d.mts +2 -0
- package/build/plugin/index.mjs +1 -0
- package/build/plugin/next-compiler-loader.cjs +40 -0
- package/build/plugin/next-compiler-loader.d.cts +12 -0
- package/build/plugin/next-compiler-loader.d.cts.map +1 -0
- package/build/plugin/next-compiler-loader.d.mts +13 -0
- package/build/plugin/next-compiler-loader.d.mts.map +1 -0
- package/build/plugin/next-compiler-loader.mjs +41 -0
- package/build/plugin/next-compiler-loader.mjs.map +1 -0
- package/build/plugin/next-config-loader.cjs +13 -0
- package/build/plugin/next-config-loader.d.cts +8 -0
- package/build/plugin/next-config-loader.d.cts.map +1 -0
- package/build/plugin/next-config-loader.d.mts +9 -0
- package/build/plugin/next-config-loader.d.mts.map +1 -0
- package/build/plugin/next-config-loader.mjs +14 -0
- package/build/plugin/next-config-loader.mjs.map +1 -0
- package/build/plugin/next-locale-client-loader.cjs +9 -0
- package/build/plugin/next-locale-client-loader.d.cts +8 -0
- package/build/plugin/next-locale-client-loader.d.cts.map +1 -0
- package/build/plugin/next-locale-client-loader.d.mts +9 -0
- package/build/plugin/next-locale-client-loader.d.mts.map +1 -0
- package/build/plugin/next-locale-client-loader.mjs +10 -0
- package/build/plugin/next-locale-client-loader.mjs.map +1 -0
- package/build/plugin/next-locale-server-loader.cjs +9 -0
- package/build/plugin/next-locale-server-loader.d.cts +8 -0
- package/build/plugin/next-locale-server-loader.d.cts.map +1 -0
- package/build/plugin/next-locale-server-loader.d.mts +9 -0
- package/build/plugin/next-locale-server-loader.d.mts.map +1 -0
- package/build/plugin/next-locale-server-loader.mjs +10 -0
- package/build/plugin/next-locale-server-loader.mjs.map +1 -0
- package/build/plugin/next.cjs +227 -0
- package/build/plugin/next.d.cts +9 -0
- package/build/plugin/next.d.cts.map +1 -0
- package/build/plugin/next.d.mts +9 -0
- package/build/plugin/next.d.mts.map +1 -0
- package/build/plugin/next.mjs +229 -0
- package/build/plugin/next.mjs.map +1 -0
- package/build/plugin/transform/babel-compat.cjs +13 -0
- package/build/plugin/transform/babel-compat.mjs +10 -0
- package/build/plugin/transform/babel-compat.mjs.map +1 -0
- package/build/plugin/transform/expression-strings.cjs +145 -0
- package/build/plugin/transform/expression-strings.mjs +140 -0
- package/build/plugin/transform/expression-strings.mjs.map +1 -0
- package/build/plugin/transform/index.cjs +45 -0
- package/build/plugin/transform/index.mjs +43 -0
- package/build/plugin/transform/index.mjs.map +1 -0
- package/build/plugin/transform/metadata.cjs +142 -0
- package/build/plugin/transform/metadata.mjs +141 -0
- package/build/plugin/transform/metadata.mjs.map +1 -0
- package/build/plugin/transform/process-file.cjs +525 -0
- package/build/plugin/transform/process-file.mjs +524 -0
- package/build/plugin/transform/process-file.mjs.map +1 -0
- package/build/plugin/transform/use-i18n.cjs +8 -0
- package/build/plugin/transform/use-i18n.mjs +7 -0
- package/build/plugin/transform/use-i18n.mjs.map +1 -0
- package/build/plugin/transform/utils.cjs +237 -0
- package/build/plugin/transform/utils.mjs +219 -0
- package/build/plugin/transform/utils.mjs.map +1 -0
- package/build/plugin/unplugin.cjs +188 -0
- package/build/plugin/unplugin.d.cts +8 -0
- package/build/plugin/unplugin.d.cts.map +1 -0
- package/build/plugin/unplugin.d.mts +8 -0
- package/build/plugin/unplugin.d.mts.map +1 -0
- package/build/plugin/unplugin.mjs +188 -0
- package/build/plugin/unplugin.mjs.map +1 -0
- package/build/plugin/vite.cjs +27 -0
- package/build/plugin/vite.d.cts +9 -0
- package/build/plugin/vite.d.cts.map +1 -0
- package/build/plugin/vite.d.mts +9 -0
- package/build/plugin/vite.d.mts.map +1 -0
- package/build/plugin/vite.mjs +28 -0
- package/build/plugin/vite.mjs.map +1 -0
- package/build/plugin/webpack.cjs +26 -0
- package/build/plugin/webpack.d.cts +8 -0
- package/build/plugin/webpack.d.cts.map +1 -0
- package/build/plugin/webpack.d.mts +8 -0
- package/build/plugin/webpack.d.mts.map +1 -0
- package/build/plugin/webpack.mjs +27 -0
- package/build/plugin/webpack.mjs.map +1 -0
- package/build/react/client/index.cjs +9 -0
- package/build/react/client/index.d.cts +5 -0
- package/build/react/client/index.d.mts +5 -0
- package/build/react/client/index.mjs +6 -0
- package/build/react/client/useTranslation.cjs +71 -0
- package/build/react/client/useTranslation.d.cts +42 -0
- package/build/react/client/useTranslation.d.cts.map +1 -0
- package/build/react/client/useTranslation.d.mts +42 -0
- package/build/react/client/useTranslation.d.mts.map +1 -0
- package/build/react/client/useTranslation.mjs +71 -0
- package/build/react/client/useTranslation.mjs.map +1 -0
- package/build/react/next/client.cjs +25 -0
- package/build/react/next/client.d.cts +9 -0
- package/build/react/next/client.d.cts.map +1 -0
- package/build/react/next/client.d.mts +9 -0
- package/build/react/next/client.d.mts.map +1 -0
- package/build/react/next/client.mjs +24 -0
- package/build/react/next/client.mjs.map +1 -0
- package/build/react/next/cookie-locale-resolver.cjs +29 -0
- package/build/react/next/cookie-locale-resolver.d.cts +33 -0
- package/build/react/next/cookie-locale-resolver.d.cts.map +1 -0
- package/build/react/next/cookie-locale-resolver.d.mts +33 -0
- package/build/react/next/cookie-locale-resolver.d.mts.map +1 -0
- package/build/react/next/cookie-locale-resolver.mjs +29 -0
- package/build/react/next/cookie-locale-resolver.mjs.map +1 -0
- package/build/react/next/server.cjs +21 -0
- package/build/react/next/server.d.cts +13 -0
- package/build/react/next/server.d.cts.map +1 -0
- package/build/react/next/server.d.mts +14 -0
- package/build/react/next/server.d.mts.map +1 -0
- package/build/react/next/server.mjs +20 -0
- package/build/react/next/server.mjs.map +1 -0
- package/build/react/server/ServerWeglotProvider.cjs +19 -0
- package/build/react/server/ServerWeglotProvider.d.cts +12 -0
- package/build/react/server/ServerWeglotProvider.d.cts.map +1 -0
- package/build/react/server/ServerWeglotProvider.d.mts +12 -0
- package/build/react/server/ServerWeglotProvider.d.mts.map +1 -0
- package/build/react/server/ServerWeglotProvider.mjs +19 -0
- package/build/react/server/ServerWeglotProvider.mjs.map +1 -0
- package/build/react/server/index.cjs +7 -0
- package/build/react/server/index.d.cts +4 -0
- package/build/react/server/index.d.mts +4 -0
- package/build/react/server/index.mjs +5 -0
- package/build/react/server/useTranslation.cjs +60 -0
- package/build/react/server/useTranslation.d.cts +36 -0
- package/build/react/server/useTranslation.d.cts.map +1 -0
- package/build/react/server/useTranslation.d.mts +36 -0
- package/build/react/server/useTranslation.d.mts.map +1 -0
- package/build/react/server/useTranslation.mjs +60 -0
- package/build/react/server/useTranslation.mjs.map +1 -0
- package/build/react/server-only/index.cjs +42 -0
- package/build/react/server-only/index.d.cts +38 -0
- package/build/react/server-only/index.d.cts.map +1 -0
- package/build/react/server-only/index.d.mts +38 -0
- package/build/react/server-only/index.d.mts.map +1 -0
- package/build/react/server-only/index.mjs +42 -0
- package/build/react/server-only/index.mjs.map +1 -0
- package/build/react/server-only/translations.cjs +86 -0
- package/build/react/server-only/translations.mjs +86 -0
- package/build/react/server-only/translations.mjs.map +1 -0
- package/build/react/shared/LocaleSwitcher.cjs +61 -0
- package/build/react/shared/LocaleSwitcher.d.cts +71 -0
- package/build/react/shared/LocaleSwitcher.d.cts.map +1 -0
- package/build/react/shared/LocaleSwitcher.d.mts +71 -0
- package/build/react/shared/LocaleSwitcher.d.mts.map +1 -0
- package/build/react/shared/LocaleSwitcher.mjs +61 -0
- package/build/react/shared/LocaleSwitcher.mjs.map +1 -0
- package/build/react/shared/WeglotContext.cjs +14 -0
- package/build/react/shared/WeglotContext.d.cts +34 -0
- package/build/react/shared/WeglotContext.d.cts.map +1 -0
- package/build/react/shared/WeglotContext.d.mts +34 -0
- package/build/react/shared/WeglotContext.d.mts.map +1 -0
- package/build/react/shared/WeglotContext.mjs +13 -0
- package/build/react/shared/WeglotContext.mjs.map +1 -0
- package/build/react/shared/WeglotProvider.cjs +250 -0
- package/build/react/shared/WeglotProvider.d.cts +60 -0
- package/build/react/shared/WeglotProvider.d.cts.map +1 -0
- package/build/react/shared/WeglotProvider.d.mts +60 -0
- package/build/react/shared/WeglotProvider.d.mts.map +1 -0
- package/build/react/shared/WeglotProvider.mjs +250 -0
- package/build/react/shared/WeglotProvider.mjs.map +1 -0
- package/build/react/shared/render-rich-text.cjs +56 -0
- package/build/react/shared/render-rich-text.d.cts +16 -0
- package/build/react/shared/render-rich-text.d.cts.map +1 -0
- package/build/react/shared/render-rich-text.d.mts +16 -0
- package/build/react/shared/render-rich-text.d.mts.map +1 -0
- package/build/react/shared/render-rich-text.mjs +55 -0
- package/build/react/shared/render-rich-text.mjs.map +1 -0
- package/build/react/shared/utils.cjs +34 -0
- package/build/react/shared/utils.mjs +35 -0
- package/build/react/shared/utils.mjs.map +1 -0
- package/build/react/types.d.cts +16 -0
- package/build/react/types.d.cts.map +1 -0
- package/build/react/types.d.mts +16 -0
- package/build/react/types.d.mts.map +1 -0
- package/build/runtime/html/segmenter.cjs +226 -0
- package/build/runtime/html/segmenter.mjs +225 -0
- package/build/runtime/html/segmenter.mjs.map +1 -0
- package/build/runtime/index.cjs +6 -0
- package/build/runtime/index.d.cts +2 -0
- package/build/runtime/index.d.mts +2 -0
- package/build/runtime/index.mjs +3 -0
- package/build/runtime/translate-dynamic.cjs +144 -0
- package/build/runtime/translate-dynamic.d.cts +29 -0
- package/build/runtime/translate-dynamic.d.cts.map +1 -0
- package/build/runtime/translate-dynamic.d.mts +29 -0
- package/build/runtime/translate-dynamic.d.mts.map +1 -0
- package/build/runtime/translate-dynamic.mjs +141 -0
- package/build/runtime/translate-dynamic.mjs.map +1 -0
- package/build/translation-server/logger.cjs +37 -0
- package/build/translation-server/logger.mjs +37 -0
- package/build/translation-server/logger.mjs.map +1 -0
- package/build/translation-server/translation-server.cjs +451 -0
- package/build/translation-server/translation-server.mjs +448 -0
- package/build/translation-server/translation-server.mjs.map +1 -0
- package/build/translators/api.cjs +12 -0
- package/build/translators/api.mjs +12 -0
- package/build/translators/api.mjs.map +1 -0
- package/build/translators/cache-factory.cjs +26 -0
- package/build/translators/cache-factory.mjs +27 -0
- package/build/translators/cache-factory.mjs.map +1 -0
- package/build/translators/local-cache.cjs +132 -0
- package/build/translators/local-cache.mjs +130 -0
- package/build/translators/local-cache.mjs.map +1 -0
- package/build/translators/translation-service.cjs +139 -0
- package/build/translators/translation-service.mjs +140 -0
- package/build/translators/translation-service.mjs.map +1 -0
- package/build/translators/weglot/api-client.cjs +131 -0
- package/build/translators/weglot/api-client.mjs +130 -0
- package/build/translators/weglot/api-client.mjs.map +1 -0
- package/build/translators/weglot/definitions/definitions.generated.cjs +53 -0
- package/build/translators/weglot/definitions/definitions.generated.mjs +52 -0
- package/build/translators/weglot/definitions/definitions.generated.mjs.map +1 -0
- package/build/translators/weglot/engine.cjs +43 -0
- package/build/translators/weglot/engine.mjs +40 -0
- package/build/translators/weglot/engine.mjs.map +1 -0
- package/build/translators/weglot/translator.cjs +67 -0
- package/build/translators/weglot/translator.mjs +68 -0
- package/build/translators/weglot/translator.mjs.map +1 -0
- package/build/translators/weglot/wire-format.cjs +128 -0
- package/build/translators/weglot/wire-format.mjs +129 -0
- package/build/translators/weglot/wire-format.mjs.map +1 -0
- package/build/types.d.cts +153 -0
- package/build/types.d.cts.map +1 -0
- package/build/types.d.mts +153 -0
- package/build/types.d.mts.map +1 -0
- package/build/utils/config-factory.cjs +100 -0
- package/build/utils/config-factory.mjs +97 -0
- package/build/utils/config-factory.mjs.map +1 -0
- package/build/utils/hash.cjs +17 -0
- package/build/utils/hash.mjs +16 -0
- package/build/utils/hash.mjs.map +1 -0
- package/build/utils/is-valid-locale.cjs +13 -0
- package/build/utils/is-valid-locale.mjs +13 -0
- package/build/utils/is-valid-locale.mjs.map +1 -0
- package/build/utils/logger.cjs +51 -0
- package/build/utils/logger.mjs +50 -0
- package/build/utils/logger.mjs.map +1 -0
- package/build/utils/path-helpers.cjs +49 -0
- package/build/utils/path-helpers.mjs +47 -0
- package/build/utils/path-helpers.mjs.map +1 -0
- package/build/utils/timeout.cjs +42 -0
- package/build/utils/timeout.mjs +41 -0
- package/build/utils/timeout.mjs.map +1 -0
- package/build/virtual/code-generator.cjs +58 -0
- package/build/virtual/code-generator.mjs +57 -0
- package/build/virtual/code-generator.mjs.map +1 -0
- package/build/virtual/config.cjs +19 -0
- package/build/virtual/config.d.cts +15 -0
- package/build/virtual/config.d.cts.map +1 -0
- package/build/virtual/config.d.mts +15 -0
- package/build/virtual/config.d.mts.map +1 -0
- package/build/virtual/config.mjs +14 -0
- package/build/virtual/config.mjs.map +1 -0
- package/build/virtual/locale/client.cjs +23 -0
- package/build/virtual/locale/client.d.cts +19 -0
- package/build/virtual/locale/client.d.cts.map +1 -0
- package/build/virtual/locale/client.d.mts +19 -0
- package/build/virtual/locale/client.d.mts.map +1 -0
- package/build/virtual/locale/client.mjs +22 -0
- package/build/virtual/locale/client.mjs.map +1 -0
- package/build/virtual/locale/server.cjs +13 -0
- package/build/virtual/locale/server.d.cts +13 -0
- package/build/virtual/locale/server.d.cts.map +1 -0
- package/build/virtual/locale/server.d.mts +13 -0
- package/build/virtual/locale/server.d.mts.map +1 -0
- package/build/virtual/locale/server.mjs +13 -0
- package/build/virtual/locale/server.mjs.map +1 -0
- package/package.json +260 -4
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
const require_rolldown_runtime = require('../../_virtual/rolldown_runtime.cjs');
|
|
4
|
+
const require_WeglotContext = require('./WeglotContext.cjs');
|
|
5
|
+
let react_jsx_runtime = require("react/jsx-runtime");
|
|
6
|
+
|
|
7
|
+
//#region src/react/shared/LocaleSwitcher.tsx
|
|
8
|
+
/**
|
|
9
|
+
* LocaleSwitcher Component
|
|
10
|
+
*
|
|
11
|
+
* Provides a dropdown to switch between locales.
|
|
12
|
+
* Works with both Next.js and other React frameworks.
|
|
13
|
+
*
|
|
14
|
+
* **How it works:**
|
|
15
|
+
* 1. User selects new locale
|
|
16
|
+
* 2. WeglotProvider automatically:
|
|
17
|
+
* - Updates cookie for persistence
|
|
18
|
+
* - Refreshes Server Components (if Next.js router provided to provider)
|
|
19
|
+
* - Loads translations for new locale
|
|
20
|
+
* 3. Client state is preserved throughout!
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```tsx
|
|
24
|
+
* import { LocaleSwitcher } from '@weglot/translate-react/react';
|
|
25
|
+
*
|
|
26
|
+
* export function Header() {
|
|
27
|
+
* return (
|
|
28
|
+
* <header>
|
|
29
|
+
* <LocaleSwitcher locales={[
|
|
30
|
+
* { code: 'en', label: 'English' },
|
|
31
|
+
* { code: 'de', label: 'Deutsch' },
|
|
32
|
+
* { code: 'fr', label: 'Français' },
|
|
33
|
+
* ]} />
|
|
34
|
+
* </header>
|
|
35
|
+
* );
|
|
36
|
+
* }
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
function LocaleSwitcher({ locales, style, className = "weglot-locale-switcher", showLoadingState = true }) {
|
|
40
|
+
const { locale, setLocale, isLoading } = require_WeglotContext.useWeglotContext();
|
|
41
|
+
const loading = showLoadingState && isLoading;
|
|
42
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("select", {
|
|
43
|
+
value: locale,
|
|
44
|
+
onChange: (e) => setLocale(e.target.value),
|
|
45
|
+
disabled: loading,
|
|
46
|
+
className,
|
|
47
|
+
style: {
|
|
48
|
+
cursor: loading ? "wait" : "pointer",
|
|
49
|
+
...style
|
|
50
|
+
},
|
|
51
|
+
"aria-label": "Select language",
|
|
52
|
+
"data-testid": "weglot-locale-switcher",
|
|
53
|
+
children: locales.map((loc) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)("option", {
|
|
54
|
+
value: loc.code,
|
|
55
|
+
children: loc.label
|
|
56
|
+
}, loc.code))
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
//#endregion
|
|
61
|
+
exports.LocaleSwitcher = LocaleSwitcher;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { LocaleCode } from "../../locale.cjs";
|
|
2
|
+
import * as react_jsx_runtime2 from "react/jsx-runtime";
|
|
3
|
+
import { CSSProperties } from "react";
|
|
4
|
+
|
|
5
|
+
//#region src/react/shared/LocaleSwitcher.d.ts
|
|
6
|
+
interface LocaleConfig {
|
|
7
|
+
code: LocaleCode;
|
|
8
|
+
label: string;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* LocaleSwitcher component props
|
|
12
|
+
*/
|
|
13
|
+
interface LocaleSwitcherProps {
|
|
14
|
+
/**
|
|
15
|
+
* Available locales, e.g. [{ code: 'en', label: 'English' }]
|
|
16
|
+
*/
|
|
17
|
+
locales: LocaleConfig[];
|
|
18
|
+
/**
|
|
19
|
+
* Custom styles for the select element
|
|
20
|
+
*/
|
|
21
|
+
style?: CSSProperties;
|
|
22
|
+
/**
|
|
23
|
+
* Custom class name
|
|
24
|
+
*/
|
|
25
|
+
className?: string;
|
|
26
|
+
/**
|
|
27
|
+
* Show loading indicator
|
|
28
|
+
* @default true
|
|
29
|
+
*/
|
|
30
|
+
showLoadingState?: boolean;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* LocaleSwitcher Component
|
|
34
|
+
*
|
|
35
|
+
* Provides a dropdown to switch between locales.
|
|
36
|
+
* Works with both Next.js and other React frameworks.
|
|
37
|
+
*
|
|
38
|
+
* **How it works:**
|
|
39
|
+
* 1. User selects new locale
|
|
40
|
+
* 2. WeglotProvider automatically:
|
|
41
|
+
* - Updates cookie for persistence
|
|
42
|
+
* - Refreshes Server Components (if Next.js router provided to provider)
|
|
43
|
+
* - Loads translations for new locale
|
|
44
|
+
* 3. Client state is preserved throughout!
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```tsx
|
|
48
|
+
* import { LocaleSwitcher } from '@weglot/translate-react/react';
|
|
49
|
+
*
|
|
50
|
+
* export function Header() {
|
|
51
|
+
* return (
|
|
52
|
+
* <header>
|
|
53
|
+
* <LocaleSwitcher locales={[
|
|
54
|
+
* { code: 'en', label: 'English' },
|
|
55
|
+
* { code: 'de', label: 'Deutsch' },
|
|
56
|
+
* { code: 'fr', label: 'Français' },
|
|
57
|
+
* ]} />
|
|
58
|
+
* </header>
|
|
59
|
+
* );
|
|
60
|
+
* }
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
declare function LocaleSwitcher({
|
|
64
|
+
locales,
|
|
65
|
+
style,
|
|
66
|
+
className,
|
|
67
|
+
showLoadingState
|
|
68
|
+
}: LocaleSwitcherProps): react_jsx_runtime2.JSX.Element;
|
|
69
|
+
//#endregion
|
|
70
|
+
export { LocaleSwitcher };
|
|
71
|
+
//# sourceMappingURL=LocaleSwitcher.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"LocaleSwitcher.d.cts","names":[],"sources":["../../../src/react/shared/LocaleSwitcher.tsx"],"sourcesContent":[],"mappings":";;;;;UAMiB,YAAA;QACT;;AADR;AAQA;AAsDA;;AAEE,UAxDe,mBAAA,CAwDf;EACA;;;EAEoB,OAAA,EAvDX,YAuDW,EAAA;EAAA;;;UAlDZ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA6CM,cAAA;;;;;GAKb,sBAAmB,kBAAA,CAAA,GAAA,CAAA"}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { LocaleCode } from "../../locale.mjs";
|
|
2
|
+
import { CSSProperties } from "react";
|
|
3
|
+
import * as react_jsx_runtime2 from "react/jsx-runtime";
|
|
4
|
+
|
|
5
|
+
//#region src/react/shared/LocaleSwitcher.d.ts
|
|
6
|
+
interface LocaleConfig {
|
|
7
|
+
code: LocaleCode;
|
|
8
|
+
label: string;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* LocaleSwitcher component props
|
|
12
|
+
*/
|
|
13
|
+
interface LocaleSwitcherProps {
|
|
14
|
+
/**
|
|
15
|
+
* Available locales, e.g. [{ code: 'en', label: 'English' }]
|
|
16
|
+
*/
|
|
17
|
+
locales: LocaleConfig[];
|
|
18
|
+
/**
|
|
19
|
+
* Custom styles for the select element
|
|
20
|
+
*/
|
|
21
|
+
style?: CSSProperties;
|
|
22
|
+
/**
|
|
23
|
+
* Custom class name
|
|
24
|
+
*/
|
|
25
|
+
className?: string;
|
|
26
|
+
/**
|
|
27
|
+
* Show loading indicator
|
|
28
|
+
* @default true
|
|
29
|
+
*/
|
|
30
|
+
showLoadingState?: boolean;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* LocaleSwitcher Component
|
|
34
|
+
*
|
|
35
|
+
* Provides a dropdown to switch between locales.
|
|
36
|
+
* Works with both Next.js and other React frameworks.
|
|
37
|
+
*
|
|
38
|
+
* **How it works:**
|
|
39
|
+
* 1. User selects new locale
|
|
40
|
+
* 2. WeglotProvider automatically:
|
|
41
|
+
* - Updates cookie for persistence
|
|
42
|
+
* - Refreshes Server Components (if Next.js router provided to provider)
|
|
43
|
+
* - Loads translations for new locale
|
|
44
|
+
* 3. Client state is preserved throughout!
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```tsx
|
|
48
|
+
* import { LocaleSwitcher } from '@weglot/translate-react/react';
|
|
49
|
+
*
|
|
50
|
+
* export function Header() {
|
|
51
|
+
* return (
|
|
52
|
+
* <header>
|
|
53
|
+
* <LocaleSwitcher locales={[
|
|
54
|
+
* { code: 'en', label: 'English' },
|
|
55
|
+
* { code: 'de', label: 'Deutsch' },
|
|
56
|
+
* { code: 'fr', label: 'Français' },
|
|
57
|
+
* ]} />
|
|
58
|
+
* </header>
|
|
59
|
+
* );
|
|
60
|
+
* }
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
declare function LocaleSwitcher({
|
|
64
|
+
locales,
|
|
65
|
+
style,
|
|
66
|
+
className,
|
|
67
|
+
showLoadingState
|
|
68
|
+
}: LocaleSwitcherProps): react_jsx_runtime2.JSX.Element;
|
|
69
|
+
//#endregion
|
|
70
|
+
export { LocaleSwitcher };
|
|
71
|
+
//# sourceMappingURL=LocaleSwitcher.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"LocaleSwitcher.d.mts","names":[],"sources":["../../../src/react/shared/LocaleSwitcher.tsx"],"sourcesContent":[],"mappings":";;;;;UAMiB,YAAA;QACT;;AADR;AAQA;AAsDA;;AAEE,UAxDe,mBAAA,CAwDf;EACA;;;EAEoB,OAAA,EAvDX,YAuDW,EAAA;EAAA;;;UAlDZ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA6CM,cAAA;;;;;GAKb,sBAAmB,kBAAA,CAAA,GAAA,CAAA"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useWeglotContext } from "./WeglotContext.mjs";
|
|
4
|
+
import { jsx } from "react/jsx-runtime";
|
|
5
|
+
|
|
6
|
+
//#region src/react/shared/LocaleSwitcher.tsx
|
|
7
|
+
/**
|
|
8
|
+
* LocaleSwitcher Component
|
|
9
|
+
*
|
|
10
|
+
* Provides a dropdown to switch between locales.
|
|
11
|
+
* Works with both Next.js and other React frameworks.
|
|
12
|
+
*
|
|
13
|
+
* **How it works:**
|
|
14
|
+
* 1. User selects new locale
|
|
15
|
+
* 2. WeglotProvider automatically:
|
|
16
|
+
* - Updates cookie for persistence
|
|
17
|
+
* - Refreshes Server Components (if Next.js router provided to provider)
|
|
18
|
+
* - Loads translations for new locale
|
|
19
|
+
* 3. Client state is preserved throughout!
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```tsx
|
|
23
|
+
* import { LocaleSwitcher } from '@weglot/translate-react/react';
|
|
24
|
+
*
|
|
25
|
+
* export function Header() {
|
|
26
|
+
* return (
|
|
27
|
+
* <header>
|
|
28
|
+
* <LocaleSwitcher locales={[
|
|
29
|
+
* { code: 'en', label: 'English' },
|
|
30
|
+
* { code: 'de', label: 'Deutsch' },
|
|
31
|
+
* { code: 'fr', label: 'Français' },
|
|
32
|
+
* ]} />
|
|
33
|
+
* </header>
|
|
34
|
+
* );
|
|
35
|
+
* }
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
function LocaleSwitcher({ locales, style, className = "weglot-locale-switcher", showLoadingState = true }) {
|
|
39
|
+
const { locale, setLocale, isLoading } = useWeglotContext();
|
|
40
|
+
const loading = showLoadingState && isLoading;
|
|
41
|
+
return /* @__PURE__ */ jsx("select", {
|
|
42
|
+
value: locale,
|
|
43
|
+
onChange: (e) => setLocale(e.target.value),
|
|
44
|
+
disabled: loading,
|
|
45
|
+
className,
|
|
46
|
+
style: {
|
|
47
|
+
cursor: loading ? "wait" : "pointer",
|
|
48
|
+
...style
|
|
49
|
+
},
|
|
50
|
+
"aria-label": "Select language",
|
|
51
|
+
"data-testid": "weglot-locale-switcher",
|
|
52
|
+
children: locales.map((loc) => /* @__PURE__ */ jsx("option", {
|
|
53
|
+
value: loc.code,
|
|
54
|
+
children: loc.label
|
|
55
|
+
}, loc.code))
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
//#endregion
|
|
60
|
+
export { LocaleSwitcher };
|
|
61
|
+
//# sourceMappingURL=LocaleSwitcher.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"LocaleSwitcher.mjs","names":[],"sources":["../../../src/react/shared/LocaleSwitcher.tsx"],"sourcesContent":["\"use client\";\n\nimport type { CSSProperties } from \"react\";\nimport { useWeglotContext } from \"./WeglotContext\";\nimport type { LocaleCode } from \"../../locale\";\n\nexport interface LocaleConfig {\n code: LocaleCode;\n label: string;\n}\n\n/**\n * LocaleSwitcher component props\n */\nexport interface LocaleSwitcherProps {\n /**\n * Available locales, e.g. [{ code: 'en', label: 'English' }]\n */\n locales: LocaleConfig[];\n\n /**\n * Custom styles for the select element\n */\n style?: CSSProperties;\n\n /**\n * Custom class name\n */\n className?: string;\n\n /**\n * Show loading indicator\n * @default true\n */\n showLoadingState?: boolean;\n}\n\n/**\n * LocaleSwitcher Component\n *\n * Provides a dropdown to switch between locales.\n * Works with both Next.js and other React frameworks.\n *\n * **How it works:**\n * 1. User selects new locale\n * 2. WeglotProvider automatically:\n * - Updates cookie for persistence\n * - Refreshes Server Components (if Next.js router provided to provider)\n * - Loads translations for new locale\n * 3. Client state is preserved throughout!\n *\n * @example\n * ```tsx\n * import { LocaleSwitcher } from '@weglot/translate-react/react';\n *\n * export function Header() {\n * return (\n * <header>\n * <LocaleSwitcher locales={[\n * { code: 'en', label: 'English' },\n * { code: 'de', label: 'Deutsch' },\n * { code: 'fr', label: 'Français' },\n * ]} />\n * </header>\n * );\n * }\n * ```\n */\nexport function LocaleSwitcher({\n locales,\n style,\n className = \"weglot-locale-switcher\",\n showLoadingState = true,\n}: LocaleSwitcherProps) {\n const { locale, setLocale, isLoading } = useWeglotContext();\n\n const loading = showLoadingState && isLoading;\n\n return (\n <select\n value={locale}\n onChange={(e) => setLocale(e.target.value as LocaleCode)}\n disabled={loading}\n className={className}\n style={{\n cursor: loading ? \"wait\" : \"pointer\",\n ...style,\n }}\n aria-label=\"Select language\"\n data-testid=\"weglot-locale-switcher\"\n >\n {locales.map((loc) => (\n <option key={loc.code} value={loc.code}>\n {loc.label}\n </option>\n ))}\n </select>\n );\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoEA,SAAgB,eAAe,EAC7B,SACA,OACA,YAAY,0BACZ,mBAAmB,QACG;CACtB,MAAM,EAAE,QAAQ,WAAW,cAAc,kBAAkB;CAE3D,MAAM,UAAU,oBAAoB;AAEpC,QACE,oBAAC;EACC,OAAO;EACP,WAAW,MAAM,UAAU,EAAE,OAAO,MAAoB;EACxD,UAAU;EACC;EACX,OAAO;GACL,QAAQ,UAAU,SAAS;GAC3B,GAAG;GACJ;EACD,cAAW;EACX,eAAY;YAEX,QAAQ,KAAK,QACZ,oBAAC;GAAsB,OAAO,IAAI;aAC/B,IAAI;KADM,IAAI,KAER,CACT;GACK"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
const require_rolldown_runtime = require('../../_virtual/rolldown_runtime.cjs');
|
|
2
|
+
let react = require("react");
|
|
3
|
+
|
|
4
|
+
//#region src/react/shared/WeglotContext.ts
|
|
5
|
+
const WeglotContext = (0, react.createContext)(null);
|
|
6
|
+
function useWeglotContext() {
|
|
7
|
+
const context = (0, react.useContext)(WeglotContext);
|
|
8
|
+
if (!context) throw new Error("useWeglotContext must be used within WeglotProvider");
|
|
9
|
+
return context;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
//#endregion
|
|
13
|
+
exports.WeglotContext = WeglotContext;
|
|
14
|
+
exports.useWeglotContext = useWeglotContext;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { LocaleCode } from "../../locale.cjs";
|
|
2
|
+
|
|
3
|
+
//#region src/react/shared/WeglotContext.d.ts
|
|
4
|
+
interface WeglotContextType {
|
|
5
|
+
/**
|
|
6
|
+
* Source locale (default language)
|
|
7
|
+
*/
|
|
8
|
+
sourceLocale: LocaleCode;
|
|
9
|
+
/**
|
|
10
|
+
* Current locale (e.g., 'en', 'de', 'fr')
|
|
11
|
+
*/
|
|
12
|
+
locale: LocaleCode;
|
|
13
|
+
/**
|
|
14
|
+
* Change the current locale and dynamically load translations
|
|
15
|
+
*/
|
|
16
|
+
setLocale: (locale: LocaleCode) => Promise<void>;
|
|
17
|
+
/**
|
|
18
|
+
* Translation dictionary: hash -> translated text
|
|
19
|
+
*/
|
|
20
|
+
translations: Record<string, string>;
|
|
21
|
+
/**
|
|
22
|
+
* Register a hash as being used in a component
|
|
23
|
+
* The provider will automatically request missing translations
|
|
24
|
+
*/
|
|
25
|
+
registerHashes: (hashes: string[]) => void;
|
|
26
|
+
/**
|
|
27
|
+
* Whether translations are currently being loaded
|
|
28
|
+
*/
|
|
29
|
+
isLoading: boolean;
|
|
30
|
+
}
|
|
31
|
+
declare function useWeglotContext(): WeglotContextType;
|
|
32
|
+
//#endregion
|
|
33
|
+
export { useWeglotContext };
|
|
34
|
+
//# sourceMappingURL=WeglotContext.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"WeglotContext.d.cts","names":[],"sources":["../../../src/react/shared/WeglotContext.ts"],"sourcesContent":[],"mappings":";;;UAGU,iBAAA;;AAFqC;;EAWrC,YAAA,EALM,UAKN;EAKY;;;EAKA,MAAA,EAVZ,UAUY;EAgBN;;;sBArBM,eAAe;;;;gBAKrB;;;;;;;;;;;iBAgBA,gBAAA,CAAA,GAAoB"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { LocaleCode } from "../../locale.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/react/shared/WeglotContext.d.ts
|
|
4
|
+
interface WeglotContextType {
|
|
5
|
+
/**
|
|
6
|
+
* Source locale (default language)
|
|
7
|
+
*/
|
|
8
|
+
sourceLocale: LocaleCode;
|
|
9
|
+
/**
|
|
10
|
+
* Current locale (e.g., 'en', 'de', 'fr')
|
|
11
|
+
*/
|
|
12
|
+
locale: LocaleCode;
|
|
13
|
+
/**
|
|
14
|
+
* Change the current locale and dynamically load translations
|
|
15
|
+
*/
|
|
16
|
+
setLocale: (locale: LocaleCode) => Promise<void>;
|
|
17
|
+
/**
|
|
18
|
+
* Translation dictionary: hash -> translated text
|
|
19
|
+
*/
|
|
20
|
+
translations: Record<string, string>;
|
|
21
|
+
/**
|
|
22
|
+
* Register a hash as being used in a component
|
|
23
|
+
* The provider will automatically request missing translations
|
|
24
|
+
*/
|
|
25
|
+
registerHashes: (hashes: string[]) => void;
|
|
26
|
+
/**
|
|
27
|
+
* Whether translations are currently being loaded
|
|
28
|
+
*/
|
|
29
|
+
isLoading: boolean;
|
|
30
|
+
}
|
|
31
|
+
declare function useWeglotContext(): WeglotContextType;
|
|
32
|
+
//#endregion
|
|
33
|
+
export { useWeglotContext };
|
|
34
|
+
//# sourceMappingURL=WeglotContext.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"WeglotContext.d.mts","names":[],"sources":["../../../src/react/shared/WeglotContext.ts"],"sourcesContent":[],"mappings":";;;UAGU,iBAAA;;AAFqC;;EAWrC,YAAA,EALM,UAKN;EAKY;;;EAKA,MAAA,EAVZ,UAUY;EAgBN;;;sBArBM,eAAe;;;;gBAKrB;;;;;;;;;;;iBAgBA,gBAAA,CAAA,GAAoB"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { createContext, useContext } from "react";
|
|
2
|
+
|
|
3
|
+
//#region src/react/shared/WeglotContext.ts
|
|
4
|
+
const WeglotContext = createContext(null);
|
|
5
|
+
function useWeglotContext() {
|
|
6
|
+
const context = useContext(WeglotContext);
|
|
7
|
+
if (!context) throw new Error("useWeglotContext must be used within WeglotProvider");
|
|
8
|
+
return context;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
//#endregion
|
|
12
|
+
export { WeglotContext, useWeglotContext };
|
|
13
|
+
//# sourceMappingURL=WeglotContext.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"WeglotContext.mjs","names":[],"sources":["../../../src/react/shared/WeglotContext.ts"],"sourcesContent":["import { createContext, useContext } from \"react\";\nimport type { LocaleCode } from \"../../locale\";\n\ninterface WeglotContextType {\n /**\n * Source locale (default language)\n */\n sourceLocale: LocaleCode;\n\n /**\n * Current locale (e.g., 'en', 'de', 'fr')\n */\n locale: LocaleCode;\n\n /**\n * Change the current locale and dynamically load translations\n */\n setLocale: (locale: LocaleCode) => Promise<void>;\n\n /**\n * Translation dictionary: hash -> translated text\n */\n translations: Record<string, string>;\n\n /**\n * Register a hash as being used in a component\n * The provider will automatically request missing translations\n */\n registerHashes: (hashes: string[]) => void;\n\n /**\n * Whether translations are currently being loaded\n */\n isLoading: boolean;\n}\n\nexport const WeglotContext = createContext<WeglotContextType | null>(null);\n\nexport function useWeglotContext(): WeglotContextType {\n const context = useContext(WeglotContext);\n\n if (!context) {\n // TODO (AleksandrSl 14/12/2025): Shouldn't throw in production\n throw new Error(\"useWeglotContext must be used within WeglotProvider\");\n }\n\n return context;\n}\n"],"mappings":";;;AAoCA,MAAa,gBAAgB,cAAwC,KAAK;AAE1E,SAAgB,mBAAsC;CACpD,MAAM,UAAU,WAAW,cAAc;AAEzC,KAAI,CAAC,QAEH,OAAM,IAAI,MAAM,sDAAsD;AAGxE,QAAO"}
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
const require_rolldown_runtime = require('../../_virtual/rolldown_runtime.cjs');
|
|
4
|
+
const require_logger = require('../../utils/logger.cjs');
|
|
5
|
+
const require_utils = require('./utils.cjs');
|
|
6
|
+
const require_WeglotContext = require('./WeglotContext.cjs');
|
|
7
|
+
let react = require("react");
|
|
8
|
+
let _weglot_translate_react_virtual_config = require("@weglot/translate-react/virtual/config");
|
|
9
|
+
let _weglot_translate_react_virtual_locale_client = require("@weglot/translate-react/virtual/locale/client");
|
|
10
|
+
let react_jsx_runtime = require("react/jsx-runtime");
|
|
11
|
+
|
|
12
|
+
//#region src/react/shared/WeglotProvider.tsx
|
|
13
|
+
const noop = () => {};
|
|
14
|
+
const IS_DEV = process.env.NODE_ENV === "development";
|
|
15
|
+
const BATCH_DELAY = 200;
|
|
16
|
+
/**
|
|
17
|
+
* Translation Provider Component
|
|
18
|
+
*
|
|
19
|
+
* Wraps your app to provide translation context to all components.
|
|
20
|
+
* Handles locale switching and on-demand translation loading.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```tsx
|
|
24
|
+
* // In your root layout
|
|
25
|
+
* import { WeglotProvider } from '@weglot/translate-react/react';
|
|
26
|
+
*
|
|
27
|
+
* export default function RootLayout({ children }) {
|
|
28
|
+
* return (
|
|
29
|
+
* <html>
|
|
30
|
+
* <body>
|
|
31
|
+
* <WeglotProvider initialLocale="en">
|
|
32
|
+
* {children}
|
|
33
|
+
* </WeglotProvider>
|
|
34
|
+
* </body>
|
|
35
|
+
* </html>
|
|
36
|
+
* );
|
|
37
|
+
* }
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
const WeglotProvider = IS_DEV ? WeglotProvider__Dev : WeglotProvider__Prod;
|
|
41
|
+
function WeglotProvider__Prod({ initialLocale, initialTranslations = {}, router, children }) {
|
|
42
|
+
const [locale, setLocaleState] = (0, react.useState)(() => {
|
|
43
|
+
if (initialLocale) return initialLocale;
|
|
44
|
+
if (typeof window !== "undefined") return (0, _weglot_translate_react_virtual_locale_client.getClientLocale)();
|
|
45
|
+
return _weglot_translate_react_virtual_config.sourceLocale;
|
|
46
|
+
});
|
|
47
|
+
const [translations, setTranslations] = (0, react.useState)(initialTranslations);
|
|
48
|
+
const [isLoading, setIsLoading] = (0, react.useState)(false);
|
|
49
|
+
require_logger.logger.debug(`WeglotProvider initialized with locale: ${locale}`, initialTranslations);
|
|
50
|
+
/**
|
|
51
|
+
* Update HTML lang attribute when locale changes
|
|
52
|
+
* This ensures screen readers and SEO understand the page language
|
|
53
|
+
*/
|
|
54
|
+
(0, react.useEffect)(() => {
|
|
55
|
+
if (typeof document !== "undefined") document.documentElement.lang = locale;
|
|
56
|
+
}, [locale]);
|
|
57
|
+
/**
|
|
58
|
+
* Load translations from public/translations/{locale}.json
|
|
59
|
+
* Lazy loads on-demand for SPAs
|
|
60
|
+
*/
|
|
61
|
+
const loadTranslations = (0, react.useCallback)(async (targetLocale) => {
|
|
62
|
+
if (Object.keys(initialTranslations).length > 0) return;
|
|
63
|
+
setIsLoading(true);
|
|
64
|
+
try {
|
|
65
|
+
const response = await fetch(`/translations/${targetLocale}.json`);
|
|
66
|
+
if (!response.ok) throw new Error(`Failed to load translations for ${targetLocale}: ${response.statusText}`);
|
|
67
|
+
const data = await response.json();
|
|
68
|
+
setTranslations(data.entries || data);
|
|
69
|
+
require_logger.logger.debug(`Loaded translations for ${targetLocale}:`, Object.keys(data.entries || data).length);
|
|
70
|
+
} catch (error) {
|
|
71
|
+
require_logger.logger.error(`Failed to load translations for ${targetLocale}:`, error);
|
|
72
|
+
setTranslations({});
|
|
73
|
+
} finally {
|
|
74
|
+
setIsLoading(false);
|
|
75
|
+
}
|
|
76
|
+
}, [initialTranslations]);
|
|
77
|
+
(0, react.useEffect)(() => {
|
|
78
|
+
if (Object.keys(initialTranslations).length === 0) loadTranslations(locale);
|
|
79
|
+
}, []);
|
|
80
|
+
(0, react.useEffect)(() => {
|
|
81
|
+
if (router) setTranslations(initialTranslations);
|
|
82
|
+
}, [initialTranslations, router]);
|
|
83
|
+
/**
|
|
84
|
+
* Change locale
|
|
85
|
+
* - For Next.js SSR: triggers server re-render via router.refresh()
|
|
86
|
+
* - For SPAs: lazy loads translations from /translations/{locale}.json
|
|
87
|
+
*/
|
|
88
|
+
const setLocale = (0, react.useCallback)(async (newLocale) => {
|
|
89
|
+
(0, _weglot_translate_react_virtual_locale_client.persistLocale)(newLocale);
|
|
90
|
+
setLocaleState(newLocale);
|
|
91
|
+
if (router) router.refresh();
|
|
92
|
+
else await loadTranslations(newLocale);
|
|
93
|
+
}, [router, loadTranslations]);
|
|
94
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_WeglotContext.WeglotContext.Provider, {
|
|
95
|
+
value: {
|
|
96
|
+
locale,
|
|
97
|
+
setLocale,
|
|
98
|
+
translations,
|
|
99
|
+
registerHashes: noop,
|
|
100
|
+
isLoading,
|
|
101
|
+
sourceLocale: _weglot_translate_react_virtual_config.sourceLocale
|
|
102
|
+
},
|
|
103
|
+
children
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
function WeglotProvider__Dev({ initialLocale, initialTranslations = {}, router, children }) {
|
|
107
|
+
const [locale, setLocaleState] = (0, react.useState)(() => {
|
|
108
|
+
if (initialLocale) return initialLocale;
|
|
109
|
+
return (0, _weglot_translate_react_virtual_locale_client.getClientLocale)();
|
|
110
|
+
});
|
|
111
|
+
const [translations, setTranslations] = (0, react.useState)(initialTranslations);
|
|
112
|
+
const [isLoading, setIsLoading] = (0, react.useState)(false);
|
|
113
|
+
const [allSeenHashes, setAllSeenHashes] = (0, react.useState)(/* @__PURE__ */ new Set());
|
|
114
|
+
const registeredHashesRef = (0, react.useRef)(/* @__PURE__ */ new Set());
|
|
115
|
+
const pendingHashesRef = (0, react.useRef)(/* @__PURE__ */ new Set());
|
|
116
|
+
const erroredHashesRef = (0, react.useRef)(/* @__PURE__ */ new Set());
|
|
117
|
+
const batchTimerRef = (0, react.useRef)(null);
|
|
118
|
+
const translationsRef = (0, react.useRef)(initialTranslations);
|
|
119
|
+
const localeRef = (0, react.useRef)(locale);
|
|
120
|
+
(0, react.useEffect)(() => {
|
|
121
|
+
translationsRef.current = translations;
|
|
122
|
+
}, [translations]);
|
|
123
|
+
(0, react.useEffect)(() => {
|
|
124
|
+
localeRef.current = locale;
|
|
125
|
+
}, [locale]);
|
|
126
|
+
/**
|
|
127
|
+
* Update HTML lang attribute when locale changes
|
|
128
|
+
* This ensures screen readers and SEO understand the page language
|
|
129
|
+
*/
|
|
130
|
+
(0, react.useEffect)(() => {
|
|
131
|
+
if (typeof document !== "undefined") document.documentElement.lang = locale;
|
|
132
|
+
}, [locale]);
|
|
133
|
+
/**
|
|
134
|
+
* Register a hash as being used in a component
|
|
135
|
+
* Called during render - must not trigger state updates immediately
|
|
136
|
+
*/
|
|
137
|
+
const registerHashes = (0, react.useCallback)((hashes) => {
|
|
138
|
+
let wasNew = false;
|
|
139
|
+
hashes.forEach((hash) => {
|
|
140
|
+
wasNew = wasNew || !registeredHashesRef.current.has(hash);
|
|
141
|
+
registeredHashesRef.current.add(hash);
|
|
142
|
+
});
|
|
143
|
+
require_logger.logger.debug(`Registering hashes: ${hashes.join(", ")}. Registered hashes: ${registeredHashesRef.current.values()}. wasNew: ${wasNew}`);
|
|
144
|
+
if (wasNew) setAllSeenHashes((prev) => {
|
|
145
|
+
const next = prev.union(new Set(hashes));
|
|
146
|
+
require_logger.logger.debug(`New allSeenHashes: ${[...next.values()]}`);
|
|
147
|
+
return next;
|
|
148
|
+
});
|
|
149
|
+
}, []);
|
|
150
|
+
/**
|
|
151
|
+
* Check for missing translations and request them (batched)
|
|
152
|
+
* This runs when allSeenHashes changes (hot reload or new components mount)
|
|
153
|
+
*/
|
|
154
|
+
(0, react.useEffect)(() => {
|
|
155
|
+
require_logger.logger.debug(`WeglotProvider checking translations for locale ${locale}, seen hashes: ${allSeenHashes.size}`);
|
|
156
|
+
if (locale === _weglot_translate_react_virtual_config.sourceLocale) return;
|
|
157
|
+
const missingHashes = [];
|
|
158
|
+
require_logger.logger.debug("allSeenHashes: ", [...allSeenHashes.values()], [...pendingHashesRef.current.values()]);
|
|
159
|
+
for (const hash of allSeenHashes) if (!translations[hash] && !pendingHashesRef.current.has(hash) && !erroredHashesRef.current.has(hash)) {
|
|
160
|
+
missingHashes.push(hash);
|
|
161
|
+
pendingHashesRef.current.add(hash);
|
|
162
|
+
}
|
|
163
|
+
require_logger.logger.debug("Missing hashes: ", missingHashes.join(","));
|
|
164
|
+
if (missingHashes.length === 0 && localeRef.current == locale) return;
|
|
165
|
+
require_logger.logger.debug(`Requesting translations for ${missingHashes.length} hashes in locale ${locale}`);
|
|
166
|
+
if (batchTimerRef.current) clearTimeout(batchTimerRef.current);
|
|
167
|
+
batchTimerRef.current = setTimeout(async () => {
|
|
168
|
+
const hashesToFetch = Array.from(pendingHashesRef.current);
|
|
169
|
+
pendingHashesRef.current.clear();
|
|
170
|
+
require_logger.logger.debug(`Fetching translations for ${hashesToFetch.length} hashes`);
|
|
171
|
+
if (hashesToFetch.length === 0) return;
|
|
172
|
+
setIsLoading(true);
|
|
173
|
+
try {
|
|
174
|
+
const newTranslations = await require_utils.fetchTranslations(localeRef.current, hashesToFetch, _weglot_translate_react_virtual_config.serverUrl);
|
|
175
|
+
require_logger.logger.debug(`Fetched translations for ${hashesToFetch.length} hashes:`, newTranslations);
|
|
176
|
+
const receivedHashes = new Set(Object.keys(newTranslations));
|
|
177
|
+
const missingHashes$1 = hashesToFetch.filter((hash) => !receivedHashes.has(hash));
|
|
178
|
+
if (missingHashes$1.length > 0) {
|
|
179
|
+
require_logger.logger.warn(`Server did not return translations for ${missingHashes$1.length} hashes: ${missingHashes$1.join(", ")}`);
|
|
180
|
+
for (const hash of missingHashes$1) erroredHashesRef.current.add(hash);
|
|
181
|
+
}
|
|
182
|
+
setTranslations((prev) => ({
|
|
183
|
+
...prev,
|
|
184
|
+
...newTranslations
|
|
185
|
+
}));
|
|
186
|
+
for (const hash of hashesToFetch) registeredHashesRef.current.add(hash);
|
|
187
|
+
} catch (error) {
|
|
188
|
+
require_logger.logger.warn(`Failed to fetch translations from translation server: ${error}.`);
|
|
189
|
+
for (const hash of hashesToFetch) {
|
|
190
|
+
pendingHashesRef.current.delete(hash);
|
|
191
|
+
erroredHashesRef.current.add(hash);
|
|
192
|
+
}
|
|
193
|
+
} finally {
|
|
194
|
+
setIsLoading(false);
|
|
195
|
+
}
|
|
196
|
+
}, BATCH_DELAY);
|
|
197
|
+
}, [
|
|
198
|
+
allSeenHashes,
|
|
199
|
+
locale,
|
|
200
|
+
translations
|
|
201
|
+
]);
|
|
202
|
+
/**
|
|
203
|
+
* Clear batch timer on unmount
|
|
204
|
+
*/
|
|
205
|
+
(0, react.useEffect)(() => {
|
|
206
|
+
return () => {
|
|
207
|
+
if (batchTimerRef.current) clearTimeout(batchTimerRef.current);
|
|
208
|
+
};
|
|
209
|
+
}, []);
|
|
210
|
+
/**
|
|
211
|
+
* Change locale and load translations dynamically
|
|
212
|
+
*/
|
|
213
|
+
const setLocale = (0, react.useCallback)(async (newLocale) => {
|
|
214
|
+
(0, _weglot_translate_react_virtual_locale_client.persistLocale)(newLocale);
|
|
215
|
+
setLocaleState(newLocale);
|
|
216
|
+
if (router) router.refresh();
|
|
217
|
+
setIsLoading(true);
|
|
218
|
+
const startTime = performance.now();
|
|
219
|
+
try {
|
|
220
|
+
require_logger.logger.info(`Fetching translations for locale: ${newLocale}. Server url: ${_weglot_translate_react_virtual_config.serverUrl}`);
|
|
221
|
+
const hashes = [...registeredHashesRef.current];
|
|
222
|
+
pendingHashesRef.current.clear();
|
|
223
|
+
erroredHashesRef.current.clear();
|
|
224
|
+
const newTranslations = hashes.length ? await require_utils.fetchTranslations(newLocale, hashes, _weglot_translate_react_virtual_config.serverUrl) : {};
|
|
225
|
+
const endTime = performance.now();
|
|
226
|
+
require_logger.logger.info(`Translation fetch complete for ${newLocale} in ${(endTime - startTime).toFixed(2)}ms`);
|
|
227
|
+
require_logger.logger.debug(`Translations loaded for ${newLocale}:`, newTranslations);
|
|
228
|
+
setTranslations(newTranslations);
|
|
229
|
+
} catch (error) {
|
|
230
|
+
require_logger.logger.error(`Failed to load translations for ${newLocale}:`, error);
|
|
231
|
+
setTranslations({});
|
|
232
|
+
} finally {
|
|
233
|
+
setIsLoading(false);
|
|
234
|
+
}
|
|
235
|
+
}, [router]);
|
|
236
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_WeglotContext.WeglotContext.Provider, {
|
|
237
|
+
value: {
|
|
238
|
+
locale,
|
|
239
|
+
setLocale,
|
|
240
|
+
translations,
|
|
241
|
+
registerHashes,
|
|
242
|
+
isLoading,
|
|
243
|
+
sourceLocale: _weglot_translate_react_virtual_config.sourceLocale
|
|
244
|
+
},
|
|
245
|
+
children
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
//#endregion
|
|
250
|
+
exports.WeglotProvider = WeglotProvider;
|