@lingo.dev/compiler 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/LICENSE.md +201 -0
- package/README.md +192 -0
- 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/metadata/manager.cjs +131 -0
- package/build/metadata/manager.mjs +123 -0
- package/build/metadata/manager.mjs.map +1 -0
- package/build/plugin/build-translator.cjs +198 -0
- package/build/plugin/build-translator.mjs +196 -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/next-compiler-loader.cjs +41 -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 +42 -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 +220 -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 +222 -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/index.cjs +44 -0
- package/build/plugin/transform/index.mjs +42 -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/parse-override.cjs +145 -0
- package/build/plugin/transform/parse-override.mjs +144 -0
- package/build/plugin/transform/parse-override.mjs.map +1 -0
- package/build/plugin/transform/process-file.cjs +391 -0
- package/build/plugin/transform/process-file.mjs +390 -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 +205 -0
- package/build/plugin/transform/utils.mjs +192 -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 +186 -0
- package/build/plugin/unplugin.mjs.map +1 -0
- package/build/plugin/vite.cjs +28 -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 +29 -0
- package/build/plugin/vite.mjs.map +1 -0
- package/build/plugin/webpack.cjs +27 -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 +28 -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/ServerLingoProvider.cjs +19 -0
- package/build/react/server/ServerLingoProvider.d.cts +12 -0
- package/build/react/server/ServerLingoProvider.d.cts.map +1 -0
- package/build/react/server/ServerLingoProvider.d.mts +12 -0
- package/build/react/server/ServerLingoProvider.d.mts.map +1 -0
- package/build/react/server/ServerLingoProvider.mjs +19 -0
- package/build/react/server/ServerLingoProvider.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 +85 -0
- package/build/react/server-only/translations.mjs +85 -0
- package/build/react/server-only/translations.mjs.map +1 -0
- package/build/react/shared/LingoContext.cjs +14 -0
- package/build/react/shared/LingoContext.d.cts +41 -0
- package/build/react/shared/LingoContext.d.cts.map +1 -0
- package/build/react/shared/LingoContext.d.mts +41 -0
- package/build/react/shared/LingoContext.d.mts.map +1 -0
- package/build/react/shared/LingoContext.mjs +13 -0
- package/build/react/shared/LingoContext.mjs.map +1 -0
- package/build/react/shared/LingoProvider.cjs +274 -0
- package/build/react/shared/LingoProvider.d.cts +76 -0
- package/build/react/shared/LingoProvider.d.cts.map +1 -0
- package/build/react/shared/LingoProvider.d.mts +76 -0
- package/build/react/shared/LingoProvider.d.mts.map +1 -0
- package/build/react/shared/LingoProvider.mjs +274 -0
- package/build/react/shared/LingoProvider.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/render-rich-text.cjs +55 -0
- package/build/react/shared/render-rich-text.d.cts +17 -0
- package/build/react/shared/render-rich-text.d.cts.map +1 -0
- package/build/react/shared/render-rich-text.d.mts +17 -0
- package/build/react/shared/render-rich-text.d.mts.map +1 -0
- package/build/react/shared/render-rich-text.mjs +54 -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/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 +547 -0
- package/build/translation-server/translation-server.mjs +544 -0
- package/build/translation-server/translation-server.mjs.map +1 -0
- package/build/translation-server/ws-events.cjs +15 -0
- package/build/translation-server/ws-events.mjs +15 -0
- package/build/translation-server/ws-events.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/lingo/model-factory.cjs +179 -0
- package/build/translators/lingo/model-factory.mjs +174 -0
- package/build/translators/lingo/model-factory.mjs.map +1 -0
- package/build/translators/lingo/prompt.cjs +43 -0
- package/build/translators/lingo/prompt.mjs +43 -0
- package/build/translators/lingo/prompt.mjs.map +1 -0
- package/build/translators/lingo/service.cjs +152 -0
- package/build/translators/lingo/service.mjs +152 -0
- package/build/translators/lingo/service.mjs.map +1 -0
- package/build/translators/lingo/shots.cjs +28 -0
- package/build/translators/lingo/shots.mjs +28 -0
- package/build/translators/lingo/shots.mjs.map +1 -0
- package/build/translators/local-cache.cjs +115 -0
- package/build/translators/local-cache.mjs +113 -0
- package/build/translators/local-cache.mjs.map +1 -0
- package/build/translators/parse-xml.cjs +109 -0
- package/build/translators/parse-xml.mjs +108 -0
- package/build/translators/parse-xml.mjs.map +1 -0
- package/build/translators/pluralization/icu-validator.cjs +36 -0
- package/build/translators/pluralization/icu-validator.mjs +36 -0
- package/build/translators/pluralization/icu-validator.mjs.map +1 -0
- package/build/translators/pluralization/pattern-detector.cjs +25 -0
- package/build/translators/pluralization/pattern-detector.mjs +25 -0
- package/build/translators/pluralization/pattern-detector.mjs.map +1 -0
- package/build/translators/pluralization/prompt.cjs +98 -0
- package/build/translators/pluralization/prompt.mjs +98 -0
- package/build/translators/pluralization/prompt.mjs.map +1 -0
- package/build/translators/pluralization/service.cjs +247 -0
- package/build/translators/pluralization/service.mjs +247 -0
- package/build/translators/pluralization/service.mjs.map +1 -0
- package/build/translators/pluralization/shots.cjs +53 -0
- package/build/translators/pluralization/shots.mjs +53 -0
- package/build/translators/pluralization/shots.mjs.map +1 -0
- package/build/translators/pluralization/types.d.cts +17 -0
- package/build/translators/pluralization/types.d.cts.map +1 -0
- package/build/translators/pluralization/types.d.mts +17 -0
- package/build/translators/pluralization/types.d.mts.map +1 -0
- package/build/translators/pseudotranslator/index.cjs +129 -0
- package/build/translators/pseudotranslator/index.mjs +129 -0
- package/build/translators/pseudotranslator/index.mjs.map +1 -0
- package/build/translators/translation-service.cjs +182 -0
- package/build/translators/translation-service.mjs +183 -0
- package/build/translators/translation-service.mjs.map +1 -0
- package/build/translators/translator-factory.cjs +49 -0
- package/build/translators/translator-factory.mjs +50 -0
- package/build/translators/translator-factory.mjs.map +1 -0
- package/build/types.d.cts +161 -0
- package/build/types.d.cts.map +1 -0
- package/build/types.d.mts +161 -0
- package/build/types.d.mts.map +1 -0
- package/build/utils/config-factory.cjs +58 -0
- package/build/utils/config-factory.mjs +58 -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 +14 -0
- package/build/utils/is-valid-locale.mjs +14 -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 +54 -0
- package/build/virtual/code-generator.mjs +53 -0
- package/build/virtual/code-generator.mjs.map +1 -0
- package/build/virtual/config.cjs +10 -0
- package/build/virtual/config.d.cts +9 -0
- package/build/virtual/config.d.cts.map +1 -0
- package/build/virtual/config.d.mts +9 -0
- package/build/virtual/config.d.mts.map +1 -0
- package/build/virtual/config.mjs +8 -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/build/widget/lingo-dev-widget.cjs +228 -0
- package/build/widget/lingo-dev-widget.mjs +229 -0
- package/build/widget/lingo-dev-widget.mjs.map +1 -0
- package/package.json +189 -0
|
@@ -0,0 +1,274 @@
|
|
|
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_LingoContext = require('./LingoContext.cjs');
|
|
7
|
+
let react = require("react");
|
|
8
|
+
let _lingo_dev_compiler_virtual_config = require("@lingo.dev/compiler/virtual/config");
|
|
9
|
+
let _lingo_dev_compiler_virtual_locale_client = require("@lingo.dev/compiler/virtual/locale/client");
|
|
10
|
+
let react_jsx_runtime = require("react/jsx-runtime");
|
|
11
|
+
|
|
12
|
+
//#region src/react/shared/LingoProvider.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 { LingoProvider } from '@lingo.dev/compiler-beta/react';
|
|
26
|
+
*
|
|
27
|
+
* export default function RootLayout({ children }) {
|
|
28
|
+
* return (
|
|
29
|
+
* <html>
|
|
30
|
+
* <body>
|
|
31
|
+
* <LingoProvider initialLocale="en">
|
|
32
|
+
* {children}
|
|
33
|
+
* </LingoProvider>
|
|
34
|
+
* </body>
|
|
35
|
+
* </html>
|
|
36
|
+
* );
|
|
37
|
+
* }
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
const LingoProvider = IS_DEV ? LingoProvider__Dev : LingoProvider__Prod;
|
|
41
|
+
function LingoProvider__Prod({ initialLocale, initialTranslations = {}, router, children }) {
|
|
42
|
+
const [locale, setLocaleState] = (0, react.useState)(() => {
|
|
43
|
+
if (initialLocale) return initialLocale;
|
|
44
|
+
if (typeof window !== "undefined") return (0, _lingo_dev_compiler_virtual_locale_client.getClientLocale)();
|
|
45
|
+
return _lingo_dev_compiler_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(`LingoProvider 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, _lingo_dev_compiler_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_LingoContext.LingoContext.Provider, {
|
|
95
|
+
value: {
|
|
96
|
+
locale,
|
|
97
|
+
setLocale,
|
|
98
|
+
translations,
|
|
99
|
+
registerHashes: noop,
|
|
100
|
+
isLoading,
|
|
101
|
+
sourceLocale: _lingo_dev_compiler_virtual_config.sourceLocale
|
|
102
|
+
},
|
|
103
|
+
children
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
function LingoProvider__Dev({ initialLocale, initialTranslations = {}, router, devWidget, children }) {
|
|
107
|
+
const [locale, setLocaleState] = (0, react.useState)(() => {
|
|
108
|
+
if (initialLocale) return initialLocale;
|
|
109
|
+
return (0, _lingo_dev_compiler_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(`LingoProvider checking translations for locale ${locale}, seen hashes: ${allSeenHashes.size}`);
|
|
156
|
+
const missingHashes = [];
|
|
157
|
+
require_logger.logger.debug("allSeenHashes: ", [...allSeenHashes.values()], [...pendingHashesRef.current.values()]);
|
|
158
|
+
for (const hash of allSeenHashes) if (!translations[hash] && !pendingHashesRef.current.has(hash) && !erroredHashesRef.current.has(hash)) {
|
|
159
|
+
missingHashes.push(hash);
|
|
160
|
+
pendingHashesRef.current.add(hash);
|
|
161
|
+
}
|
|
162
|
+
require_logger.logger.debug("Missing hashes: ", missingHashes.join(","));
|
|
163
|
+
if (missingHashes.length === 0 && localeRef.current == locale) return;
|
|
164
|
+
require_logger.logger.debug(`Requesting translations for ${missingHashes.length} hashes in locale ${locale}`);
|
|
165
|
+
if (batchTimerRef.current) clearTimeout(batchTimerRef.current);
|
|
166
|
+
batchTimerRef.current = setTimeout(async () => {
|
|
167
|
+
const hashesToFetch = Array.from(pendingHashesRef.current);
|
|
168
|
+
pendingHashesRef.current.clear();
|
|
169
|
+
require_logger.logger.debug(`Fetching translations for ${hashesToFetch.length} hashes`);
|
|
170
|
+
if (hashesToFetch.length === 0) return;
|
|
171
|
+
setIsLoading(true);
|
|
172
|
+
try {
|
|
173
|
+
const newTranslations = await require_utils.fetchTranslations(localeRef.current, hashesToFetch, _lingo_dev_compiler_virtual_config.serverUrl);
|
|
174
|
+
require_logger.logger.debug(`Fetched translations for ${hashesToFetch.length} hashes:`, newTranslations);
|
|
175
|
+
const receivedHashes = new Set(Object.keys(newTranslations));
|
|
176
|
+
const missingHashes$1 = hashesToFetch.filter((hash) => !receivedHashes.has(hash));
|
|
177
|
+
if (missingHashes$1.length > 0) {
|
|
178
|
+
require_logger.logger.warn(`Server did not return translations for ${missingHashes$1.length} hashes: ${missingHashes$1.join(", ")}`);
|
|
179
|
+
for (const hash of missingHashes$1) erroredHashesRef.current.add(hash);
|
|
180
|
+
}
|
|
181
|
+
setTranslations((prev) => ({
|
|
182
|
+
...prev,
|
|
183
|
+
...newTranslations
|
|
184
|
+
}));
|
|
185
|
+
for (const hash of hashesToFetch) registeredHashesRef.current.add(hash);
|
|
186
|
+
} catch (error) {
|
|
187
|
+
require_logger.logger.warn(`Failed to fetch translations from translation server: ${error}.`);
|
|
188
|
+
for (const hash of hashesToFetch) {
|
|
189
|
+
pendingHashesRef.current.delete(hash);
|
|
190
|
+
erroredHashesRef.current.add(hash);
|
|
191
|
+
}
|
|
192
|
+
} finally {
|
|
193
|
+
setIsLoading(false);
|
|
194
|
+
}
|
|
195
|
+
}, BATCH_DELAY);
|
|
196
|
+
}, [
|
|
197
|
+
allSeenHashes,
|
|
198
|
+
locale,
|
|
199
|
+
translations
|
|
200
|
+
]);
|
|
201
|
+
/**
|
|
202
|
+
* Clear batch timer on unmount
|
|
203
|
+
*/
|
|
204
|
+
(0, react.useEffect)(() => {
|
|
205
|
+
return () => {
|
|
206
|
+
if (batchTimerRef.current) clearTimeout(batchTimerRef.current);
|
|
207
|
+
};
|
|
208
|
+
}, []);
|
|
209
|
+
/**
|
|
210
|
+
* Change locale and load translations dynamically
|
|
211
|
+
*/
|
|
212
|
+
const setLocale = (0, react.useCallback)(async (newLocale) => {
|
|
213
|
+
(0, _lingo_dev_compiler_virtual_locale_client.persistLocale)(newLocale);
|
|
214
|
+
setLocaleState(newLocale);
|
|
215
|
+
if (router) router.refresh();
|
|
216
|
+
setIsLoading(true);
|
|
217
|
+
const startTime = performance.now();
|
|
218
|
+
try {
|
|
219
|
+
require_logger.logger.info(`Fetching translations for locale: ${newLocale}. Server url: ${_lingo_dev_compiler_virtual_config.serverUrl}`);
|
|
220
|
+
const translatedDict = await require_utils.fetchTranslations(newLocale, [], _lingo_dev_compiler_virtual_config.serverUrl);
|
|
221
|
+
const endTime = performance.now();
|
|
222
|
+
require_logger.logger.info(`Translation fetch complete for ${newLocale} in ${(endTime - startTime).toFixed(2)}ms`);
|
|
223
|
+
const allTranslations = translatedDict.entries || {};
|
|
224
|
+
require_logger.logger.debug(`Translations loaded for ${newLocale}:`, allTranslations);
|
|
225
|
+
setTranslations(allTranslations);
|
|
226
|
+
} catch (error) {
|
|
227
|
+
require_logger.logger.error(`Failed to load translations for ${newLocale}:`, error);
|
|
228
|
+
setTranslations({});
|
|
229
|
+
} finally {
|
|
230
|
+
setIsLoading(false);
|
|
231
|
+
}
|
|
232
|
+
}, [router]);
|
|
233
|
+
(0, react.useEffect)(() => {
|
|
234
|
+
if (devWidget?.enabled !== false) Promise.resolve().then(() => require("../../widget/lingo-dev-widget.cjs")).catch((err) => {
|
|
235
|
+
require_logger.logger.error("Failed to load dev widget:", err, err.message);
|
|
236
|
+
});
|
|
237
|
+
}, [devWidget?.enabled]);
|
|
238
|
+
(0, react.useEffect)(() => {
|
|
239
|
+
if (typeof window !== "undefined" && devWidget?.enabled !== false) {
|
|
240
|
+
window.__LINGO_DEV_STATE__ = {
|
|
241
|
+
isLoading,
|
|
242
|
+
locale,
|
|
243
|
+
sourceLocale: _lingo_dev_compiler_virtual_config.sourceLocale,
|
|
244
|
+
pendingCount: pendingHashesRef.current.size,
|
|
245
|
+
position: devWidget?.position || "bottom-left"
|
|
246
|
+
};
|
|
247
|
+
window.__LINGO_DEV_WS_URL__ = _lingo_dev_compiler_virtual_config.serverUrl;
|
|
248
|
+
window.__LINGO_DEV_UPDATE__?.();
|
|
249
|
+
}
|
|
250
|
+
}, [
|
|
251
|
+
isLoading,
|
|
252
|
+
locale,
|
|
253
|
+
_lingo_dev_compiler_virtual_config.sourceLocale,
|
|
254
|
+
devWidget
|
|
255
|
+
]);
|
|
256
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_LingoContext.LingoContext.Provider, {
|
|
257
|
+
value: {
|
|
258
|
+
locale,
|
|
259
|
+
setLocale,
|
|
260
|
+
translations,
|
|
261
|
+
registerHashes,
|
|
262
|
+
isLoading,
|
|
263
|
+
sourceLocale: _lingo_dev_compiler_virtual_config.sourceLocale,
|
|
264
|
+
_devStats: {
|
|
265
|
+
pendingCount: pendingHashesRef.current.size,
|
|
266
|
+
totalRegisteredCount: registeredHashesRef.current.size
|
|
267
|
+
}
|
|
268
|
+
},
|
|
269
|
+
children
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
//#endregion
|
|
274
|
+
exports.LingoProvider = LingoProvider;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { LocaleCode } from "lingo.dev/spec";
|
|
2
|
+
import * as react_jsx_runtime2 from "react/jsx-runtime";
|
|
3
|
+
import { PropsWithChildren } from "react";
|
|
4
|
+
|
|
5
|
+
//#region src/react/shared/LingoProvider.d.ts
|
|
6
|
+
/**
|
|
7
|
+
* Translation provider props
|
|
8
|
+
*/
|
|
9
|
+
type LingoProviderProps = PropsWithChildren<{
|
|
10
|
+
/**
|
|
11
|
+
* Initial locale to use
|
|
12
|
+
*/
|
|
13
|
+
initialLocale?: LocaleCode;
|
|
14
|
+
/**
|
|
15
|
+
* Initial translations (pre-loaded)
|
|
16
|
+
*/
|
|
17
|
+
initialTranslations?: Record<string, string>;
|
|
18
|
+
/**
|
|
19
|
+
* Optional router instance for Next.js integration
|
|
20
|
+
* If provided, calls router.refresh() after locale change
|
|
21
|
+
* This ensures Server Components re-render with new locale
|
|
22
|
+
*/
|
|
23
|
+
router?: {
|
|
24
|
+
refresh: () => void;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Development widget configuration
|
|
28
|
+
*/
|
|
29
|
+
devWidget?: {
|
|
30
|
+
/**
|
|
31
|
+
* Enable/disable widget (default: true in dev mode)
|
|
32
|
+
* Set to false to opt-out
|
|
33
|
+
*/
|
|
34
|
+
enabled?: boolean;
|
|
35
|
+
/**
|
|
36
|
+
* Widget position on screen
|
|
37
|
+
* @default 'bottom-left'
|
|
38
|
+
*/
|
|
39
|
+
position?: "bottom-left" | "bottom-right" | "top-left" | "top-right";
|
|
40
|
+
};
|
|
41
|
+
}>;
|
|
42
|
+
/**
|
|
43
|
+
* Translation Provider Component
|
|
44
|
+
*
|
|
45
|
+
* Wraps your app to provide translation context to all components.
|
|
46
|
+
* Handles locale switching and on-demand translation loading.
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```tsx
|
|
50
|
+
* // In your root layout
|
|
51
|
+
* import { LingoProvider } from '@lingo.dev/compiler-beta/react';
|
|
52
|
+
*
|
|
53
|
+
* export default function RootLayout({ children }) {
|
|
54
|
+
* return (
|
|
55
|
+
* <html>
|
|
56
|
+
* <body>
|
|
57
|
+
* <LingoProvider initialLocale="en">
|
|
58
|
+
* {children}
|
|
59
|
+
* </LingoProvider>
|
|
60
|
+
* </body>
|
|
61
|
+
* </html>
|
|
62
|
+
* );
|
|
63
|
+
* }
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
declare const LingoProvider: typeof LingoProvider__Dev;
|
|
67
|
+
declare function LingoProvider__Dev({
|
|
68
|
+
initialLocale,
|
|
69
|
+
initialTranslations,
|
|
70
|
+
router,
|
|
71
|
+
devWidget,
|
|
72
|
+
children
|
|
73
|
+
}: LingoProviderProps): react_jsx_runtime2.JSX.Element;
|
|
74
|
+
//#endregion
|
|
75
|
+
export { LingoProvider, LingoProviderProps };
|
|
76
|
+
//# sourceMappingURL=LingoProvider.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"LingoProvider.d.cts","names":[],"sources":["../../../src/react/shared/LingoProvider.tsx"],"sourcesContent":[],"mappings":";;;;;;;;AAyBY,KAAA,kBAAA,GAAqB,iBAAH,CAAA;EAIZ;;;EAJgC,aAAA,CAAA,EAIhC,UAJgC;EAgErC;AAAkE;;EAoI7E,mBAAA,CAAA,EA3LsB,MA2LtB,CAAA,MAAA,EAAA,MAAA,CAAA;EACA;;;;;EAGmB,MAAA,CAAA,EAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAxIR,sBAAa;iBAkIjB,kBAAA;;;;;;GAMN,qBAAkB,kBAAA,CAAA,GAAA,CAAA"}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { PropsWithChildren } from "react";
|
|
2
|
+
import * as react_jsx_runtime2 from "react/jsx-runtime";
|
|
3
|
+
import { LocaleCode } from "lingo.dev/spec";
|
|
4
|
+
|
|
5
|
+
//#region src/react/shared/LingoProvider.d.ts
|
|
6
|
+
/**
|
|
7
|
+
* Translation provider props
|
|
8
|
+
*/
|
|
9
|
+
type LingoProviderProps = PropsWithChildren<{
|
|
10
|
+
/**
|
|
11
|
+
* Initial locale to use
|
|
12
|
+
*/
|
|
13
|
+
initialLocale?: LocaleCode;
|
|
14
|
+
/**
|
|
15
|
+
* Initial translations (pre-loaded)
|
|
16
|
+
*/
|
|
17
|
+
initialTranslations?: Record<string, string>;
|
|
18
|
+
/**
|
|
19
|
+
* Optional router instance for Next.js integration
|
|
20
|
+
* If provided, calls router.refresh() after locale change
|
|
21
|
+
* This ensures Server Components re-render with new locale
|
|
22
|
+
*/
|
|
23
|
+
router?: {
|
|
24
|
+
refresh: () => void;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Development widget configuration
|
|
28
|
+
*/
|
|
29
|
+
devWidget?: {
|
|
30
|
+
/**
|
|
31
|
+
* Enable/disable widget (default: true in dev mode)
|
|
32
|
+
* Set to false to opt-out
|
|
33
|
+
*/
|
|
34
|
+
enabled?: boolean;
|
|
35
|
+
/**
|
|
36
|
+
* Widget position on screen
|
|
37
|
+
* @default 'bottom-left'
|
|
38
|
+
*/
|
|
39
|
+
position?: "bottom-left" | "bottom-right" | "top-left" | "top-right";
|
|
40
|
+
};
|
|
41
|
+
}>;
|
|
42
|
+
/**
|
|
43
|
+
* Translation Provider Component
|
|
44
|
+
*
|
|
45
|
+
* Wraps your app to provide translation context to all components.
|
|
46
|
+
* Handles locale switching and on-demand translation loading.
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```tsx
|
|
50
|
+
* // In your root layout
|
|
51
|
+
* import { LingoProvider } from '@lingo.dev/compiler-beta/react';
|
|
52
|
+
*
|
|
53
|
+
* export default function RootLayout({ children }) {
|
|
54
|
+
* return (
|
|
55
|
+
* <html>
|
|
56
|
+
* <body>
|
|
57
|
+
* <LingoProvider initialLocale="en">
|
|
58
|
+
* {children}
|
|
59
|
+
* </LingoProvider>
|
|
60
|
+
* </body>
|
|
61
|
+
* </html>
|
|
62
|
+
* );
|
|
63
|
+
* }
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
declare const LingoProvider: typeof LingoProvider__Dev;
|
|
67
|
+
declare function LingoProvider__Dev({
|
|
68
|
+
initialLocale,
|
|
69
|
+
initialTranslations,
|
|
70
|
+
router,
|
|
71
|
+
devWidget,
|
|
72
|
+
children
|
|
73
|
+
}: LingoProviderProps): react_jsx_runtime2.JSX.Element;
|
|
74
|
+
//#endregion
|
|
75
|
+
export { LingoProvider, LingoProviderProps };
|
|
76
|
+
//# sourceMappingURL=LingoProvider.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"LingoProvider.d.mts","names":[],"sources":["../../../src/react/shared/LingoProvider.tsx"],"sourcesContent":[],"mappings":";;;;;;;;AAyBY,KAAA,kBAAA,GAAqB,iBAAH,CAAA;EAIZ;;;EAJgC,aAAA,CAAA,EAIhC,UAJgC;EAgErC;AAAkE;;EAoI7E,mBAAA,CAAA,EA3LsB,MA2LtB,CAAA,MAAA,EAAA,MAAA,CAAA;EACA;;;;;EAGmB,MAAA,CAAA,EAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;cAxIR,sBAAa;iBAkIjB,kBAAA;;;;;;GAMN,qBAAkB,kBAAA,CAAA,GAAA,CAAA"}
|