gt-tanstack-start 11.0.11 → 11.0.12
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/CHANGELOG.md +10 -0
- package/dist/{index.d.mts → index.client.d.mts} +1 -1
- package/dist/index.client.d.mts.map +1 -0
- package/dist/index.client.mjs +140 -0
- package/dist/index.client.mjs.map +1 -0
- package/dist/{index.d.cts → index.server.d.mts} +7 -3
- package/dist/index.server.d.mts.map +1 -0
- package/dist/index.server.mjs +184 -0
- package/dist/index.server.mjs.map +1 -0
- package/dist/server.d.mts +25 -0
- package/dist/server.d.mts.map +1 -0
- package/dist/server.mjs +108 -0
- package/dist/server.mjs.map +1 -0
- package/package.json +29 -12
- package/dist/index.cjs +0 -260
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.cts.map +0 -1
- package/dist/index.d.mts.map +0 -1
- package/dist/index.mjs +0 -55
- package/dist/index.mjs.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
# gt-tanstack-start
|
|
2
2
|
|
|
3
|
+
## 11.0.12
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#1941](https://github.com/generaltranslation/gt/pull/1941) [`7fef71d`](https://github.com/generaltranslation/gt/commit/7fef71de88a770bd5e14ec9f62cdac91671b3d2f) Thanks [@ErnestM1234](https://github.com/ErnestM1234)! - Add request-scoped server middleware and server-only translation helpers for TanStack Start.
|
|
8
|
+
|
|
9
|
+
- Updated dependencies []:
|
|
10
|
+
- @generaltranslation/react-core@11.0.12
|
|
11
|
+
- gt-react@11.0.12
|
|
12
|
+
|
|
3
13
|
## 11.0.11
|
|
4
14
|
|
|
5
15
|
### Patch Changes
|
|
@@ -6,4 +6,4 @@ import { Branch, Currency, DateTime, Derive, GTProvider, LocaleSelector, Num, Pl
|
|
|
6
6
|
declare function parseLocale(): string;
|
|
7
7
|
//#endregion
|
|
8
8
|
export { Branch, Currency, DateTime, Derive, GTProvider, LocaleSelector, Num, Plural, RelativeTime, T, Var, declareVar, decodeMsg, decodeOptions, decodeVars, derive, getTranslationsSnapshot, gtFallback, initializeGT, mFallback, msg, parseLocale, t, useCustomMapping, useDefaultLocale, useEnableI18n, useFormatLocales, useGT, useLocale, useLocaleSelector, useLocales, useMessages, useSetEnableI18n, useSetLocale, useTranslations };
|
|
9
|
-
//# sourceMappingURL=index.d.mts.map
|
|
9
|
+
//# sourceMappingURL=index.client.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.client.d.mts","names":[],"sources":["../src/functions/parseLocale.ts"],"mappings":";;;;;iBAkBgB,WAAA,CAAA"}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { createIsomorphicFn } from "@tanstack/react-start";
|
|
2
|
+
import { getRequest, setCookie } from "@tanstack/react-start/server";
|
|
3
|
+
import { getI18nConfig } from "@generaltranslation/react-core/pure";
|
|
4
|
+
import { createGlobalSingleton, getCookieValue, parseAcceptLanguage } from "gt-i18n/internal";
|
|
5
|
+
import { Branch, Currency, DateTime, Derive, GTProvider, LocaleSelector, Num, Plural, RelativeTime, T, Var, declareVar, decodeMsg, decodeOptions, decodeVars, derive, getTranslationsSnapshot, gtFallback, initializeGT, mFallback, msg, t, useCustomMapping, useDefaultLocale, useEnableI18n, useFormatLocales, useGT, useLocale, useLocaleSelector, useLocales, useMessages, useSetEnableI18n, useSetLocale, useTranslations } from "gt-react";
|
|
6
|
+
//#region ../core/dist/api-BOEGbEF6.mjs
|
|
7
|
+
function ensureSentence(text) {
|
|
8
|
+
const trimmed = text.trim();
|
|
9
|
+
if (!trimmed) return "";
|
|
10
|
+
return /[.!?)]$/.test(trimmed) ? trimmed : `${trimmed}.`;
|
|
11
|
+
}
|
|
12
|
+
function stripSentence(text) {
|
|
13
|
+
const trimmed = text.trim();
|
|
14
|
+
let end = trimmed.length;
|
|
15
|
+
while (end > 0) {
|
|
16
|
+
const char = trimmed[end - 1];
|
|
17
|
+
if (char !== "." && char !== "!" && char !== "?") break;
|
|
18
|
+
end -= 1;
|
|
19
|
+
}
|
|
20
|
+
return trimmed.slice(0, end);
|
|
21
|
+
}
|
|
22
|
+
function lowercaseFirstWord(text) {
|
|
23
|
+
return text.replace(/^[A-Z][a-z]/, (match) => match.toLowerCase());
|
|
24
|
+
}
|
|
25
|
+
function formatDetails(details) {
|
|
26
|
+
if (!details) return "";
|
|
27
|
+
const detailText = Array.isArray(details) ? details.join(", ") : details;
|
|
28
|
+
if (!detailText.trim()) return "";
|
|
29
|
+
return ensureSentence(`Details: ${detailText}`);
|
|
30
|
+
}
|
|
31
|
+
function createDiagnosticMessage({ source, severity, whatHappened, reassurance, why, fix, wayOut, details, docsUrl }) {
|
|
32
|
+
const prefix = source ? severity ? `${source} ${severity}:` : `${source}:` : severity ? `${severity}:` : "";
|
|
33
|
+
const whatAndWhy = why ? `${stripSentence(whatHappened)} because ${lowercaseFirstWord(stripSentence(why))}` : whatHappened;
|
|
34
|
+
const shouldCombineWayOut = !!fix && !!wayOut && /^[a-z]/.test(stripSentence(wayOut));
|
|
35
|
+
const messageParts = [
|
|
36
|
+
whatAndWhy,
|
|
37
|
+
reassurance,
|
|
38
|
+
shouldCombineWayOut ? `${stripSentence(fix)}, or ${lowercaseFirstWord(stripSentence(wayOut))}` : fix,
|
|
39
|
+
shouldCombineWayOut ? void 0 : wayOut,
|
|
40
|
+
formatDetails(details)
|
|
41
|
+
].filter((part) => !!part).map(ensureSentence);
|
|
42
|
+
if (docsUrl) messageParts.push(`Learn more: ${docsUrl}`);
|
|
43
|
+
const message = messageParts.join(" ");
|
|
44
|
+
return prefix ? `${prefix} ${message}` : message;
|
|
45
|
+
}
|
|
46
|
+
//#endregion
|
|
47
|
+
//#region src/condition-store/singleton.ts
|
|
48
|
+
const conditionStoreNotInitializedError = createDiagnosticMessage({
|
|
49
|
+
source: "gt-tanstack-start",
|
|
50
|
+
severity: "Error",
|
|
51
|
+
whatHappened: "Cannot read GT server request state before initialization",
|
|
52
|
+
why: "initializeGT() has not initialized the TanStack Start server condition store",
|
|
53
|
+
fix: "Call initializeGT() from 'gt-tanstack-start' during application setup before using gtMiddleware or server APIs."
|
|
54
|
+
});
|
|
55
|
+
const conditionStoreSingleton = createGlobalSingleton({
|
|
56
|
+
namespace: "tanstackStart",
|
|
57
|
+
key: "conditionStore",
|
|
58
|
+
source: "gt-tanstack-start",
|
|
59
|
+
notInitialized: () => conditionStoreNotInitializedError
|
|
60
|
+
});
|
|
61
|
+
const getConditionStore = conditionStoreSingleton.get;
|
|
62
|
+
conditionStoreSingleton.set;
|
|
63
|
+
const isConditionStoreInitialized = conditionStoreSingleton.isInitialized;
|
|
64
|
+
//#endregion
|
|
65
|
+
//#region src/functions/requestConditions.ts
|
|
66
|
+
const localeCookieOptions = {
|
|
67
|
+
path: "/",
|
|
68
|
+
sameSite: "lax",
|
|
69
|
+
maxAge: 3600 * 24 * 365
|
|
70
|
+
};
|
|
71
|
+
const noLocaleCandidatesWarning = createDiagnosticMessage({
|
|
72
|
+
source: "gt-tanstack-start",
|
|
73
|
+
severity: "Warning",
|
|
74
|
+
whatHappened: "No locale preference was found for the current request",
|
|
75
|
+
reassurance: "GT will use the configured default locale",
|
|
76
|
+
why: "neither the locale cookie nor the Accept-Language header supplied a supported locale candidate"
|
|
77
|
+
});
|
|
78
|
+
function resolveRequestConditions(request, localeConfig) {
|
|
79
|
+
const i18nConfig = getI18nConfig();
|
|
80
|
+
const cookieHeader = request.headers.get("cookie");
|
|
81
|
+
const localeCandidates = [];
|
|
82
|
+
const cookieLocale = getCookieValue(cookieHeader, i18nConfig.getLocaleCookieName());
|
|
83
|
+
if (cookieLocale) localeCandidates.push(cookieLocale);
|
|
84
|
+
localeCandidates.push(...parseAcceptLanguage(request.headers.get("accept-language")));
|
|
85
|
+
if (localeCandidates.length === 0) console.warn(noLocaleCandidatesWarning);
|
|
86
|
+
const locale = i18nConfig.resolveSupportedLocale(localeCandidates, localeConfig ?? {
|
|
87
|
+
defaultLocale: i18nConfig.getDefaultLocale(),
|
|
88
|
+
locales: i18nConfig.getLocales(),
|
|
89
|
+
customMapping: i18nConfig.getCustomMapping()
|
|
90
|
+
});
|
|
91
|
+
setCookie(i18nConfig.getLocaleCookieName(), locale, localeCookieOptions);
|
|
92
|
+
const enableI18nCookie = getCookieValue(cookieHeader, i18nConfig.getEnableI18nCookieName());
|
|
93
|
+
return {
|
|
94
|
+
locale,
|
|
95
|
+
region: getCookieValue(cookieHeader, i18nConfig.getRegionCookieName()) || void 0,
|
|
96
|
+
enableI18n: enableI18nCookie === void 0 ? true : enableI18nCookie === "true"
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
//#endregion
|
|
100
|
+
//#region src/functions/parseLocale.ts
|
|
101
|
+
const determineLocale = createIsomorphicFn().server(determineLocaleServer).client(determineLocaleClient);
|
|
102
|
+
/**
|
|
103
|
+
* Resolve the user's locale for the current TanStack Start request or browser.
|
|
104
|
+
*/
|
|
105
|
+
function parseLocale() {
|
|
106
|
+
const i18nConfig = getI18nConfig();
|
|
107
|
+
return determineLocale({
|
|
108
|
+
defaultLocale: i18nConfig.getDefaultLocale(),
|
|
109
|
+
locales: i18nConfig.getLocales(),
|
|
110
|
+
customMapping: i18nConfig.getCustomMapping()
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
function determineLocaleServer({ defaultLocale, locales, customMapping }) {
|
|
114
|
+
if (isConditionStoreInitialized()) {
|
|
115
|
+
const conditionStore = getConditionStore();
|
|
116
|
+
if (conditionStore.hasActiveScope()) return conditionStore.getLocale();
|
|
117
|
+
}
|
|
118
|
+
return resolveRequestConditions(getRequest(), {
|
|
119
|
+
defaultLocale,
|
|
120
|
+
locales,
|
|
121
|
+
customMapping
|
|
122
|
+
}).locale;
|
|
123
|
+
}
|
|
124
|
+
function determineLocaleClient({ defaultLocale, locales, customMapping }) {
|
|
125
|
+
const i18nConfig = getI18nConfig();
|
|
126
|
+
const localeCookieName = i18nConfig.getLocaleCookieName();
|
|
127
|
+
const candidates = [];
|
|
128
|
+
const cookie = getCookieValue(document.cookie, localeCookieName);
|
|
129
|
+
if (cookie) candidates.push(cookie);
|
|
130
|
+
if (candidates.length === 0) console.warn("gt-tanstack-start(client): no locales could be determined for this request");
|
|
131
|
+
return i18nConfig.resolveSupportedLocale(candidates, {
|
|
132
|
+
defaultLocale,
|
|
133
|
+
locales,
|
|
134
|
+
customMapping
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
//#endregion
|
|
138
|
+
export { Branch, Currency, DateTime, Derive, GTProvider, LocaleSelector, Num, Plural, RelativeTime, T, Var, declareVar, decodeMsg, decodeOptions, decodeVars, derive, getTranslationsSnapshot, gtFallback, initializeGT, mFallback, msg, parseLocale, t, useCustomMapping, useDefaultLocale, useEnableI18n, useFormatLocales, useGT, useLocale, useLocaleSelector, useLocales, useMessages, useSetEnableI18n, useSetLocale, useTranslations };
|
|
139
|
+
|
|
140
|
+
//# sourceMappingURL=index.client.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.client.mjs","names":[],"sources":["../../core/dist/api-BOEGbEF6.mjs","../src/condition-store/singleton.ts","../src/functions/requestConditions.ts","../src/functions/parseLocale.ts"],"sourcesContent":["//#region src/settings/settings.ts\nconst libraryDefaultLocale = \"en\";\nconst defaultTimeout = 6e4;\n//#endregion\n//#region src/logging/diagnostics.ts\nfunction ensureSentence(text) {\n\tconst trimmed = text.trim();\n\tif (!trimmed) return \"\";\n\treturn /[.!?)]$/.test(trimmed) ? trimmed : `${trimmed}.`;\n}\nfunction stripSentence(text) {\n\tconst trimmed = text.trim();\n\tlet end = trimmed.length;\n\twhile (end > 0) {\n\t\tconst char = trimmed[end - 1];\n\t\tif (char !== \".\" && char !== \"!\" && char !== \"?\") break;\n\t\tend -= 1;\n\t}\n\treturn trimmed.slice(0, end);\n}\nfunction lowercaseFirstWord(text) {\n\treturn text.replace(/^[A-Z][a-z]/, (match) => match.toLowerCase());\n}\nfunction formatDetails(details) {\n\tif (!details) return \"\";\n\tconst detailText = Array.isArray(details) ? details.join(\", \") : details;\n\tif (!detailText.trim()) return \"\";\n\treturn ensureSentence(`Details: ${detailText}`);\n}\nfunction formatDiagnosticErrorDetails(error) {\n\tif (error == null) return void 0;\n\treturn String(error);\n}\nfunction createDiagnosticMessage({ source, severity, whatHappened, reassurance, why, fix, wayOut, details, docsUrl }) {\n\tconst prefix = source ? severity ? `${source} ${severity}:` : `${source}:` : severity ? `${severity}:` : \"\";\n\tconst whatAndWhy = why ? `${stripSentence(whatHappened)} because ${lowercaseFirstWord(stripSentence(why))}` : whatHappened;\n\tconst shouldCombineWayOut = !!fix && !!wayOut && /^[a-z]/.test(stripSentence(wayOut));\n\tconst messageParts = [\n\t\twhatAndWhy,\n\t\treassurance,\n\t\tshouldCombineWayOut ? `${stripSentence(fix)}, or ${lowercaseFirstWord(stripSentence(wayOut))}` : fix,\n\t\tshouldCombineWayOut ? void 0 : wayOut,\n\t\tformatDetails(details)\n\t].filter((part) => !!part).map(ensureSentence);\n\tif (docsUrl) messageParts.push(`Learn more: ${docsUrl}`);\n\tconst message = messageParts.join(\" \");\n\treturn prefix ? `${prefix} ${message}` : message;\n}\n//#endregion\n//#region src/settings/settingsUrls.ts\nconst defaultCacheUrl = \"https://cdn.gtx.dev\";\nconst defaultBaseUrl = \"https://api2.gtx.dev\";\nconst defaultRuntimeApiUrl = \"https://runtime2.gtx.dev\";\n//#endregion\n//#region src/translate/api.ts\nconst API_VERSION = \"2026-03-06.v1\";\n//#endregion\nexport { createDiagnosticMessage as a, libraryDefaultLocale as c, defaultRuntimeApiUrl as i, defaultBaseUrl as n, formatDiagnosticErrorDetails as o, defaultCacheUrl as r, defaultTimeout as s, API_VERSION as t };\n\n//# sourceMappingURL=api-BOEGbEF6.mjs.map","import { createDiagnosticMessage } from 'generaltranslation/internal';\nimport { createGlobalSingleton } from 'gt-i18n/internal';\nimport type { AsyncLocalConditionStore } from './AsyncLocalConditionStore';\n\nconst conditionStoreNotInitializedError = createDiagnosticMessage({\n source: 'gt-tanstack-start',\n severity: 'Error',\n whatHappened: 'Cannot read GT server request state before initialization',\n why: 'initializeGT() has not initialized the TanStack Start server condition store',\n fix: \"Call initializeGT() from 'gt-tanstack-start' during application setup before using gtMiddleware or server APIs.\",\n});\n\nconst conditionStoreSingleton = createGlobalSingleton<AsyncLocalConditionStore>(\n {\n namespace: 'tanstackStart',\n key: 'conditionStore',\n source: 'gt-tanstack-start',\n notInitialized: () => conditionStoreNotInitializedError,\n }\n);\n\nexport const getConditionStore = conditionStoreSingleton.get;\nexport const setConditionStore = conditionStoreSingleton.set;\nexport const isConditionStoreInitialized =\n conditionStoreSingleton.isInitialized;\n","import { setCookie } from '@tanstack/react-start/server';\nimport {\n getI18nConfig,\n type I18nConfigParams,\n} from '@generaltranslation/react-core/pure';\nimport { createDiagnosticMessage } from 'generaltranslation/internal';\nimport { getCookieValue, parseAcceptLanguage } from 'gt-i18n/internal';\nimport type { RequestConditions } from '../condition-store/AsyncLocalConditionStore';\n\nexport const localeCookieOptions = {\n path: '/',\n sameSite: 'lax' as const,\n maxAge: 60 * 60 * 24 * 365,\n};\n\nconst noLocaleCandidatesWarning = createDiagnosticMessage({\n source: 'gt-tanstack-start',\n severity: 'Warning',\n whatHappened: 'No locale preference was found for the current request',\n reassurance: 'GT will use the configured default locale',\n why: 'neither the locale cookie nor the Accept-Language header supplied a supported locale candidate',\n});\n\nexport function resolveRequestConditions(\n request: Request,\n localeConfig?: I18nConfigParams\n): RequestConditions {\n const i18nConfig = getI18nConfig();\n const cookieHeader = request.headers.get('cookie');\n const localeCandidates: string[] = [];\n const cookieLocale = getCookieValue(\n cookieHeader,\n i18nConfig.getLocaleCookieName()\n );\n if (cookieLocale) localeCandidates.push(cookieLocale);\n localeCandidates.push(\n ...parseAcceptLanguage(request.headers.get('accept-language'))\n );\n\n if (localeCandidates.length === 0) {\n console.warn(noLocaleCandidatesWarning);\n }\n\n const locale = i18nConfig.resolveSupportedLocale(\n localeCandidates,\n localeConfig ?? {\n defaultLocale: i18nConfig.getDefaultLocale(),\n locales: i18nConfig.getLocales(),\n customMapping: i18nConfig.getCustomMapping(),\n }\n );\n\n setCookie(i18nConfig.getLocaleCookieName(), locale, localeCookieOptions);\n\n const enableI18nCookie = getCookieValue(\n cookieHeader,\n i18nConfig.getEnableI18nCookieName()\n );\n\n return {\n locale,\n region:\n getCookieValue(cookieHeader, i18nConfig.getRegionCookieName()) ||\n undefined,\n enableI18n:\n enableI18nCookie === undefined ? true : enableI18nCookie === 'true',\n };\n}\n","import { createIsomorphicFn } from '@tanstack/react-start';\nimport { getRequest } from '@tanstack/react-start/server';\nimport { getI18nConfig } from '@generaltranslation/react-core/pure';\nimport { getCookieValue } from 'gt-i18n/internal';\nimport type { LocaleResolverConfig } from 'gt-i18n/internal/types';\nimport {\n getConditionStore,\n isConditionStoreInitialized,\n} from '../condition-store/singleton';\nimport { resolveRequestConditions } from './requestConditions';\n\nexport const determineLocale = createIsomorphicFn()\n .server(determineLocaleServer)\n .client(determineLocaleClient);\n\n/**\n * Resolve the user's locale for the current TanStack Start request or browser.\n */\nexport function parseLocale(): string {\n const i18nConfig = getI18nConfig();\n return determineLocale({\n defaultLocale: i18nConfig.getDefaultLocale(),\n locales: i18nConfig.getLocales(),\n customMapping: i18nConfig.getCustomMapping(),\n });\n}\n\nfunction determineLocaleServer({\n defaultLocale,\n locales,\n customMapping,\n}: LocaleResolverConfig) {\n if (isConditionStoreInitialized()) {\n const conditionStore = getConditionStore();\n if (conditionStore.hasActiveScope()) {\n return conditionStore.getLocale();\n }\n }\n\n return resolveRequestConditions(getRequest(), {\n defaultLocale,\n locales,\n customMapping,\n }).locale;\n}\n\nfunction determineLocaleClient({\n defaultLocale,\n locales,\n customMapping,\n}: LocaleResolverConfig) {\n const i18nConfig = getI18nConfig();\n const localeCookieName = i18nConfig.getLocaleCookieName();\n const candidates: string[] = [];\n\n const cookie = getCookieValue(document.cookie, localeCookieName);\n if (cookie) candidates.push(cookie);\n\n if (candidates.length === 0) {\n console.warn(\n 'gt-tanstack-start(client): no locales could be determined for this request'\n );\n }\n\n return i18nConfig.resolveSupportedLocale(candidates, {\n defaultLocale,\n locales,\n customMapping,\n });\n}\n"],"mappings":";;;;;;AAKA,SAAS,eAAe,MAAM;CAC7B,MAAM,UAAU,KAAK,MAAM;AAC3B,KAAI,CAAC,QAAS,QAAO;AACrB,QAAO,UAAU,KAAK,QAAQ,GAAG,UAAU,GAAG,QAAQ;;AAEvD,SAAS,cAAc,MAAM;CAC5B,MAAM,UAAU,KAAK,MAAM;CAC3B,IAAI,MAAM,QAAQ;AAClB,QAAO,MAAM,GAAG;EACf,MAAM,OAAO,QAAQ,MAAM;AAC3B,MAAI,SAAS,OAAO,SAAS,OAAO,SAAS,IAAK;AAClD,SAAO;;AAER,QAAO,QAAQ,MAAM,GAAG,IAAI;;AAE7B,SAAS,mBAAmB,MAAM;AACjC,QAAO,KAAK,QAAQ,gBAAgB,UAAU,MAAM,aAAa,CAAC;;AAEnE,SAAS,cAAc,SAAS;AAC/B,KAAI,CAAC,QAAS,QAAO;CACrB,MAAM,aAAa,MAAM,QAAQ,QAAQ,GAAG,QAAQ,KAAK,KAAK,GAAG;AACjE,KAAI,CAAC,WAAW,MAAM,CAAE,QAAO;AAC/B,QAAO,eAAe,YAAY,aAAa;;AAMhD,SAAS,wBAAwB,EAAE,QAAQ,UAAU,cAAc,aAAa,KAAK,KAAK,QAAQ,SAAS,WAAW;CACrH,MAAM,SAAS,SAAS,WAAW,GAAG,OAAO,GAAG,SAAS,KAAK,GAAG,OAAO,KAAK,WAAW,GAAG,SAAS,KAAK;CACzG,MAAM,aAAa,MAAM,GAAG,cAAc,aAAa,CAAC,WAAW,mBAAmB,cAAc,IAAI,CAAC,KAAK;CAC9G,MAAM,sBAAsB,CAAC,CAAC,OAAO,CAAC,CAAC,UAAU,SAAS,KAAK,cAAc,OAAO,CAAC;CACrF,MAAM,eAAe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AU,WAAW,qBAAqB,EAAE,QAAQ,oBAAoB;CAExE,MAAM,mBAAmB,eACvB,cACA,WAAW,yBAAyB,CACrC;AAED,QAAO;EACL;EACA,QACE,eAAe,cAAc,WAAW,qBAAqB,CAAC,IAC9D,KAAA;EACF,YACE,qBAAqB,KAAA,IAAY,OAAO,qBAAqB;EAChE;;;;ACvDH,MAAa,kBAAkB,oBAAoB,CAChD,OAAO,sBAAsB,CAC7B,OAAO,sBAAsB;;;;AAKhC,SAAgB,cAAsB;CACpC,MAAM,aAAa,eAAe;AAClC,QAAO,gBAAgB;EACrB,eAAe,WAAW,kBAAkB;EAC5C,SAAS,WAAW,YAAY;EAChC,eAAe,WAAW,kBAAkB;EAC7C,CAAC;;AAGJ,SAAS,sBAAsB,EAC7B,eACA,SACA,iBACuB;AACvB,KAAI,6BAA6B,EAAE;EACjC,MAAM,iBAAiB,mBAAmB;AAC1C,MAAI,eAAe,gBAAgB,CACjC,QAAO,eAAe,WAAW;;AAIrC,QAAO,yBAAyB,YAAY,EAAE;EAC5C;EACA;EACA;EACD,CAAC,CAAC;;AAGL,SAAS,sBAAsB,EAC7B,eACA,SACA,iBACuB;CACvB,MAAM,aAAa,eAAe;CAClC,MAAM,mBAAmB,WAAW,qBAAqB;CACzD,MAAM,aAAuB,EAAE;CAE/B,MAAM,SAAS,eAAe,SAAS,QAAQ,iBAAiB;AAChE,KAAI,OAAQ,YAAW,KAAK,OAAO;AAEnC,KAAI,WAAW,WAAW,EACxB,SAAQ,KACN,6EACD;AAGH,QAAO,WAAW,uBAAuB,YAAY;EACnD;EACA;EACA;EACD,CAAC"}
|
|
@@ -1,10 +1,14 @@
|
|
|
1
|
-
import { Branch, Currency, DateTime, Derive, GTProvider, LocaleSelector, Num, Plural, RelativeTime, T, Var, declareVar, decodeMsg, decodeOptions, decodeVars, derive, getTranslationsSnapshot, gtFallback, initializeGT, mFallback, msg, t, useCustomMapping, useDefaultLocale, useEnableI18n, useFormatLocales, useGT, useLocale, useLocaleSelector, useLocales, useMessages, useSetEnableI18n, useSetLocale, useTranslations } from "gt-react";
|
|
2
|
-
|
|
1
|
+
import { Branch, Currency, DateTime, Derive, GTProvider, LocaleSelector, Num, Plural, RelativeTime, T, Var, declareVar, decodeMsg, decodeOptions, decodeVars, derive, getTranslationsSnapshot, gtFallback, initializeGT as initializeGT$1, mFallback, msg, t, useCustomMapping, useDefaultLocale, useEnableI18n, useFormatLocales, useGT, useLocale, useLocaleSelector, useLocales, useMessages, useSetEnableI18n, useSetLocale, useTranslations } from "gt-react";
|
|
3
2
|
//#region src/functions/parseLocale.d.ts
|
|
4
3
|
/**
|
|
5
4
|
* Resolve the user's locale for the current TanStack Start request or browser.
|
|
6
5
|
*/
|
|
7
6
|
declare function parseLocale(): string;
|
|
8
7
|
//#endregion
|
|
8
|
+
//#region src/setup/initializeGT.d.ts
|
|
9
|
+
type InitializeGTParams = Parameters<typeof initializeGT$1>[0];
|
|
10
|
+
/** Initialize GT and its server request condition store. */
|
|
11
|
+
declare function initializeGT(config: InitializeGTParams): void;
|
|
12
|
+
//#endregion
|
|
9
13
|
export { Branch, Currency, DateTime, Derive, GTProvider, LocaleSelector, Num, Plural, RelativeTime, T, Var, declareVar, decodeMsg, decodeOptions, decodeVars, derive, getTranslationsSnapshot, gtFallback, initializeGT, mFallback, msg, parseLocale, t, useCustomMapping, useDefaultLocale, useEnableI18n, useFormatLocales, useGT, useLocale, useLocaleSelector, useLocales, useMessages, useSetEnableI18n, useSetLocale, useTranslations };
|
|
10
|
-
//# sourceMappingURL=index.d.
|
|
14
|
+
//# sourceMappingURL=index.server.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.server.d.mts","names":[],"sources":["../src/functions/parseLocale.ts","../src/setup/initializeGT.ts"],"mappings":";;;;;iBAkBgB,WAAA,CAAA;;;KCdX,kBAAA,GAAqB,UAAA,QAAkB,cAAA;;iBAG5B,YAAA,CAAa,MAAA,EAAQ,kBAAA"}
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import { createIsomorphicFn } from "@tanstack/react-start";
|
|
2
|
+
import { getRequest, setCookie } from "@tanstack/react-start/server";
|
|
3
|
+
import { getI18nConfig } from "@generaltranslation/react-core/pure";
|
|
4
|
+
import { createGlobalSingleton, getCookieValue, parseAcceptLanguage } from "gt-i18n/internal";
|
|
5
|
+
import { Branch, Currency, DateTime, Derive, GTProvider, LocaleSelector, Num, Plural, RelativeTime, T, Var, declareVar, decodeMsg, decodeOptions, decodeVars, derive, getTranslationsSnapshot, gtFallback, initializeGT as initializeGT$1, mFallback, msg, t, useCustomMapping, useDefaultLocale, useEnableI18n, useFormatLocales, useGT, useLocale, useLocaleSelector, useLocales, useMessages, useSetEnableI18n, useSetLocale, useTranslations } from "gt-react";
|
|
6
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
7
|
+
//#region ../core/dist/api-BOEGbEF6.mjs
|
|
8
|
+
function ensureSentence(text) {
|
|
9
|
+
const trimmed = text.trim();
|
|
10
|
+
if (!trimmed) return "";
|
|
11
|
+
return /[.!?)]$/.test(trimmed) ? trimmed : `${trimmed}.`;
|
|
12
|
+
}
|
|
13
|
+
function stripSentence(text) {
|
|
14
|
+
const trimmed = text.trim();
|
|
15
|
+
let end = trimmed.length;
|
|
16
|
+
while (end > 0) {
|
|
17
|
+
const char = trimmed[end - 1];
|
|
18
|
+
if (char !== "." && char !== "!" && char !== "?") break;
|
|
19
|
+
end -= 1;
|
|
20
|
+
}
|
|
21
|
+
return trimmed.slice(0, end);
|
|
22
|
+
}
|
|
23
|
+
function lowercaseFirstWord(text) {
|
|
24
|
+
return text.replace(/^[A-Z][a-z]/, (match) => match.toLowerCase());
|
|
25
|
+
}
|
|
26
|
+
function formatDetails(details) {
|
|
27
|
+
if (!details) return "";
|
|
28
|
+
const detailText = Array.isArray(details) ? details.join(", ") : details;
|
|
29
|
+
if (!detailText.trim()) return "";
|
|
30
|
+
return ensureSentence(`Details: ${detailText}`);
|
|
31
|
+
}
|
|
32
|
+
function createDiagnosticMessage({ source, severity, whatHappened, reassurance, why, fix, wayOut, details, docsUrl }) {
|
|
33
|
+
const prefix = source ? severity ? `${source} ${severity}:` : `${source}:` : severity ? `${severity}:` : "";
|
|
34
|
+
const whatAndWhy = why ? `${stripSentence(whatHappened)} because ${lowercaseFirstWord(stripSentence(why))}` : whatHappened;
|
|
35
|
+
const shouldCombineWayOut = !!fix && !!wayOut && /^[a-z]/.test(stripSentence(wayOut));
|
|
36
|
+
const messageParts = [
|
|
37
|
+
whatAndWhy,
|
|
38
|
+
reassurance,
|
|
39
|
+
shouldCombineWayOut ? `${stripSentence(fix)}, or ${lowercaseFirstWord(stripSentence(wayOut))}` : fix,
|
|
40
|
+
shouldCombineWayOut ? void 0 : wayOut,
|
|
41
|
+
formatDetails(details)
|
|
42
|
+
].filter((part) => !!part).map(ensureSentence);
|
|
43
|
+
if (docsUrl) messageParts.push(`Learn more: ${docsUrl}`);
|
|
44
|
+
const message = messageParts.join(" ");
|
|
45
|
+
return prefix ? `${prefix} ${message}` : message;
|
|
46
|
+
}
|
|
47
|
+
//#endregion
|
|
48
|
+
//#region src/condition-store/singleton.ts
|
|
49
|
+
const conditionStoreNotInitializedError = createDiagnosticMessage({
|
|
50
|
+
source: "gt-tanstack-start",
|
|
51
|
+
severity: "Error",
|
|
52
|
+
whatHappened: "Cannot read GT server request state before initialization",
|
|
53
|
+
why: "initializeGT() has not initialized the TanStack Start server condition store",
|
|
54
|
+
fix: "Call initializeGT() from 'gt-tanstack-start' during application setup before using gtMiddleware or server APIs."
|
|
55
|
+
});
|
|
56
|
+
const conditionStoreSingleton = createGlobalSingleton({
|
|
57
|
+
namespace: "tanstackStart",
|
|
58
|
+
key: "conditionStore",
|
|
59
|
+
source: "gt-tanstack-start",
|
|
60
|
+
notInitialized: () => conditionStoreNotInitializedError
|
|
61
|
+
});
|
|
62
|
+
const getConditionStore = conditionStoreSingleton.get;
|
|
63
|
+
const setConditionStore = conditionStoreSingleton.set;
|
|
64
|
+
const isConditionStoreInitialized = conditionStoreSingleton.isInitialized;
|
|
65
|
+
//#endregion
|
|
66
|
+
//#region src/functions/requestConditions.ts
|
|
67
|
+
const localeCookieOptions = {
|
|
68
|
+
path: "/",
|
|
69
|
+
sameSite: "lax",
|
|
70
|
+
maxAge: 3600 * 24 * 365
|
|
71
|
+
};
|
|
72
|
+
const noLocaleCandidatesWarning = createDiagnosticMessage({
|
|
73
|
+
source: "gt-tanstack-start",
|
|
74
|
+
severity: "Warning",
|
|
75
|
+
whatHappened: "No locale preference was found for the current request",
|
|
76
|
+
reassurance: "GT will use the configured default locale",
|
|
77
|
+
why: "neither the locale cookie nor the Accept-Language header supplied a supported locale candidate"
|
|
78
|
+
});
|
|
79
|
+
function resolveRequestConditions(request, localeConfig) {
|
|
80
|
+
const i18nConfig = getI18nConfig();
|
|
81
|
+
const cookieHeader = request.headers.get("cookie");
|
|
82
|
+
const localeCandidates = [];
|
|
83
|
+
const cookieLocale = getCookieValue(cookieHeader, i18nConfig.getLocaleCookieName());
|
|
84
|
+
if (cookieLocale) localeCandidates.push(cookieLocale);
|
|
85
|
+
localeCandidates.push(...parseAcceptLanguage(request.headers.get("accept-language")));
|
|
86
|
+
if (localeCandidates.length === 0) console.warn(noLocaleCandidatesWarning);
|
|
87
|
+
const locale = i18nConfig.resolveSupportedLocale(localeCandidates, localeConfig ?? {
|
|
88
|
+
defaultLocale: i18nConfig.getDefaultLocale(),
|
|
89
|
+
locales: i18nConfig.getLocales(),
|
|
90
|
+
customMapping: i18nConfig.getCustomMapping()
|
|
91
|
+
});
|
|
92
|
+
setCookie(i18nConfig.getLocaleCookieName(), locale, localeCookieOptions);
|
|
93
|
+
const enableI18nCookie = getCookieValue(cookieHeader, i18nConfig.getEnableI18nCookieName());
|
|
94
|
+
return {
|
|
95
|
+
locale,
|
|
96
|
+
region: getCookieValue(cookieHeader, i18nConfig.getRegionCookieName()) || void 0,
|
|
97
|
+
enableI18n: enableI18nCookie === void 0 ? true : enableI18nCookie === "true"
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
//#endregion
|
|
101
|
+
//#region src/functions/parseLocale.ts
|
|
102
|
+
const determineLocale = createIsomorphicFn().server(determineLocaleServer).client(determineLocaleClient);
|
|
103
|
+
/**
|
|
104
|
+
* Resolve the user's locale for the current TanStack Start request or browser.
|
|
105
|
+
*/
|
|
106
|
+
function parseLocale() {
|
|
107
|
+
const i18nConfig = getI18nConfig();
|
|
108
|
+
return determineLocale({
|
|
109
|
+
defaultLocale: i18nConfig.getDefaultLocale(),
|
|
110
|
+
locales: i18nConfig.getLocales(),
|
|
111
|
+
customMapping: i18nConfig.getCustomMapping()
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
function determineLocaleServer({ defaultLocale, locales, customMapping }) {
|
|
115
|
+
if (isConditionStoreInitialized()) {
|
|
116
|
+
const conditionStore = getConditionStore();
|
|
117
|
+
if (conditionStore.hasActiveScope()) return conditionStore.getLocale();
|
|
118
|
+
}
|
|
119
|
+
return resolveRequestConditions(getRequest(), {
|
|
120
|
+
defaultLocale,
|
|
121
|
+
locales,
|
|
122
|
+
customMapping
|
|
123
|
+
}).locale;
|
|
124
|
+
}
|
|
125
|
+
function determineLocaleClient({ defaultLocale, locales, customMapping }) {
|
|
126
|
+
const i18nConfig = getI18nConfig();
|
|
127
|
+
const localeCookieName = i18nConfig.getLocaleCookieName();
|
|
128
|
+
const candidates = [];
|
|
129
|
+
const cookie = getCookieValue(document.cookie, localeCookieName);
|
|
130
|
+
if (cookie) candidates.push(cookie);
|
|
131
|
+
if (candidates.length === 0) console.warn("gt-tanstack-start(client): no locales could be determined for this request");
|
|
132
|
+
return i18nConfig.resolveSupportedLocale(candidates, {
|
|
133
|
+
defaultLocale,
|
|
134
|
+
locales,
|
|
135
|
+
customMapping
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
//#endregion
|
|
139
|
+
//#region src/condition-store/AsyncLocalConditionStore.ts
|
|
140
|
+
const missingRequestScopeError = createDiagnosticMessage({
|
|
141
|
+
source: "gt-tanstack-start",
|
|
142
|
+
severity: "Error",
|
|
143
|
+
whatHappened: "Cannot read GT request state outside a request scope",
|
|
144
|
+
why: "the gt-tanstack-start request middleware has not initialized the ConditionStore for this request",
|
|
145
|
+
fix: "Register gtMiddleware from 'gt-tanstack-start/server' as global TanStack Start request middleware."
|
|
146
|
+
});
|
|
147
|
+
/**
|
|
148
|
+
* Read-only ConditionStore backed by request-scoped AsyncLocalStorage.
|
|
149
|
+
*/
|
|
150
|
+
var AsyncLocalConditionStore = class {
|
|
151
|
+
constructor(config) {
|
|
152
|
+
this.config = config;
|
|
153
|
+
this.storage = new AsyncLocalStorage();
|
|
154
|
+
this.getLocale = () => this.getConditions().locale;
|
|
155
|
+
this.getRegion = () => this.getConditions().region;
|
|
156
|
+
this.getEnableI18n = () => this.getConditions().enableI18n;
|
|
157
|
+
this.setLocale = (_locale) => {};
|
|
158
|
+
this.setRegion = (_region) => {};
|
|
159
|
+
this.setEnableI18n = (_enableI18n) => {};
|
|
160
|
+
}
|
|
161
|
+
run(request, callback) {
|
|
162
|
+
const conditions = resolveRequestConditions(request, this.config);
|
|
163
|
+
return this.storage.run(conditions, callback);
|
|
164
|
+
}
|
|
165
|
+
hasActiveScope() {
|
|
166
|
+
return this.storage.getStore() !== void 0;
|
|
167
|
+
}
|
|
168
|
+
getConditions() {
|
|
169
|
+
const conditions = this.storage.getStore();
|
|
170
|
+
if (!conditions) throw new Error(missingRequestScopeError);
|
|
171
|
+
return conditions;
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
//#endregion
|
|
175
|
+
//#region src/setup/initializeGT.ts
|
|
176
|
+
/** Initialize GT and its server request condition store. */
|
|
177
|
+
function initializeGT(config) {
|
|
178
|
+
initializeGT$1(config);
|
|
179
|
+
setConditionStore(new AsyncLocalConditionStore(config));
|
|
180
|
+
}
|
|
181
|
+
//#endregion
|
|
182
|
+
export { Branch, Currency, DateTime, Derive, GTProvider, LocaleSelector, Num, Plural, RelativeTime, T, Var, declareVar, decodeMsg, decodeOptions, decodeVars, derive, getTranslationsSnapshot, gtFallback, initializeGT, mFallback, msg, parseLocale, t, useCustomMapping, useDefaultLocale, useEnableI18n, useFormatLocales, useGT, useLocale, useLocaleSelector, useLocales, useMessages, useSetEnableI18n, useSetLocale, useTranslations };
|
|
183
|
+
|
|
184
|
+
//# sourceMappingURL=index.server.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.server.mjs","names":[],"sources":["../../core/dist/api-BOEGbEF6.mjs","../src/condition-store/singleton.ts","../src/functions/requestConditions.ts","../src/functions/parseLocale.ts","../src/condition-store/AsyncLocalConditionStore.ts","../src/setup/initializeGT.ts"],"sourcesContent":["//#region src/settings/settings.ts\nconst libraryDefaultLocale = \"en\";\nconst defaultTimeout = 6e4;\n//#endregion\n//#region src/logging/diagnostics.ts\nfunction ensureSentence(text) {\n\tconst trimmed = text.trim();\n\tif (!trimmed) return \"\";\n\treturn /[.!?)]$/.test(trimmed) ? trimmed : `${trimmed}.`;\n}\nfunction stripSentence(text) {\n\tconst trimmed = text.trim();\n\tlet end = trimmed.length;\n\twhile (end > 0) {\n\t\tconst char = trimmed[end - 1];\n\t\tif (char !== \".\" && char !== \"!\" && char !== \"?\") break;\n\t\tend -= 1;\n\t}\n\treturn trimmed.slice(0, end);\n}\nfunction lowercaseFirstWord(text) {\n\treturn text.replace(/^[A-Z][a-z]/, (match) => match.toLowerCase());\n}\nfunction formatDetails(details) {\n\tif (!details) return \"\";\n\tconst detailText = Array.isArray(details) ? details.join(\", \") : details;\n\tif (!detailText.trim()) return \"\";\n\treturn ensureSentence(`Details: ${detailText}`);\n}\nfunction formatDiagnosticErrorDetails(error) {\n\tif (error == null) return void 0;\n\treturn String(error);\n}\nfunction createDiagnosticMessage({ source, severity, whatHappened, reassurance, why, fix, wayOut, details, docsUrl }) {\n\tconst prefix = source ? severity ? `${source} ${severity}:` : `${source}:` : severity ? `${severity}:` : \"\";\n\tconst whatAndWhy = why ? `${stripSentence(whatHappened)} because ${lowercaseFirstWord(stripSentence(why))}` : whatHappened;\n\tconst shouldCombineWayOut = !!fix && !!wayOut && /^[a-z]/.test(stripSentence(wayOut));\n\tconst messageParts = [\n\t\twhatAndWhy,\n\t\treassurance,\n\t\tshouldCombineWayOut ? `${stripSentence(fix)}, or ${lowercaseFirstWord(stripSentence(wayOut))}` : fix,\n\t\tshouldCombineWayOut ? void 0 : wayOut,\n\t\tformatDetails(details)\n\t].filter((part) => !!part).map(ensureSentence);\n\tif (docsUrl) messageParts.push(`Learn more: ${docsUrl}`);\n\tconst message = messageParts.join(\" \");\n\treturn prefix ? `${prefix} ${message}` : message;\n}\n//#endregion\n//#region src/settings/settingsUrls.ts\nconst defaultCacheUrl = \"https://cdn.gtx.dev\";\nconst defaultBaseUrl = \"https://api2.gtx.dev\";\nconst defaultRuntimeApiUrl = \"https://runtime2.gtx.dev\";\n//#endregion\n//#region src/translate/api.ts\nconst API_VERSION = \"2026-03-06.v1\";\n//#endregion\nexport { createDiagnosticMessage as a, libraryDefaultLocale as c, defaultRuntimeApiUrl as i, defaultBaseUrl as n, formatDiagnosticErrorDetails as o, defaultCacheUrl as r, defaultTimeout as s, API_VERSION as t };\n\n//# sourceMappingURL=api-BOEGbEF6.mjs.map","import { createDiagnosticMessage } from 'generaltranslation/internal';\nimport { createGlobalSingleton } from 'gt-i18n/internal';\nimport type { AsyncLocalConditionStore } from './AsyncLocalConditionStore';\n\nconst conditionStoreNotInitializedError = createDiagnosticMessage({\n source: 'gt-tanstack-start',\n severity: 'Error',\n whatHappened: 'Cannot read GT server request state before initialization',\n why: 'initializeGT() has not initialized the TanStack Start server condition store',\n fix: \"Call initializeGT() from 'gt-tanstack-start' during application setup before using gtMiddleware or server APIs.\",\n});\n\nconst conditionStoreSingleton = createGlobalSingleton<AsyncLocalConditionStore>(\n {\n namespace: 'tanstackStart',\n key: 'conditionStore',\n source: 'gt-tanstack-start',\n notInitialized: () => conditionStoreNotInitializedError,\n }\n);\n\nexport const getConditionStore = conditionStoreSingleton.get;\nexport const setConditionStore = conditionStoreSingleton.set;\nexport const isConditionStoreInitialized =\n conditionStoreSingleton.isInitialized;\n","import { setCookie } from '@tanstack/react-start/server';\nimport {\n getI18nConfig,\n type I18nConfigParams,\n} from '@generaltranslation/react-core/pure';\nimport { createDiagnosticMessage } from 'generaltranslation/internal';\nimport { getCookieValue, parseAcceptLanguage } from 'gt-i18n/internal';\nimport type { RequestConditions } from '../condition-store/AsyncLocalConditionStore';\n\nexport const localeCookieOptions = {\n path: '/',\n sameSite: 'lax' as const,\n maxAge: 60 * 60 * 24 * 365,\n};\n\nconst noLocaleCandidatesWarning = createDiagnosticMessage({\n source: 'gt-tanstack-start',\n severity: 'Warning',\n whatHappened: 'No locale preference was found for the current request',\n reassurance: 'GT will use the configured default locale',\n why: 'neither the locale cookie nor the Accept-Language header supplied a supported locale candidate',\n});\n\nexport function resolveRequestConditions(\n request: Request,\n localeConfig?: I18nConfigParams\n): RequestConditions {\n const i18nConfig = getI18nConfig();\n const cookieHeader = request.headers.get('cookie');\n const localeCandidates: string[] = [];\n const cookieLocale = getCookieValue(\n cookieHeader,\n i18nConfig.getLocaleCookieName()\n );\n if (cookieLocale) localeCandidates.push(cookieLocale);\n localeCandidates.push(\n ...parseAcceptLanguage(request.headers.get('accept-language'))\n );\n\n if (localeCandidates.length === 0) {\n console.warn(noLocaleCandidatesWarning);\n }\n\n const locale = i18nConfig.resolveSupportedLocale(\n localeCandidates,\n localeConfig ?? {\n defaultLocale: i18nConfig.getDefaultLocale(),\n locales: i18nConfig.getLocales(),\n customMapping: i18nConfig.getCustomMapping(),\n }\n );\n\n setCookie(i18nConfig.getLocaleCookieName(), locale, localeCookieOptions);\n\n const enableI18nCookie = getCookieValue(\n cookieHeader,\n i18nConfig.getEnableI18nCookieName()\n );\n\n return {\n locale,\n region:\n getCookieValue(cookieHeader, i18nConfig.getRegionCookieName()) ||\n undefined,\n enableI18n:\n enableI18nCookie === undefined ? true : enableI18nCookie === 'true',\n };\n}\n","import { createIsomorphicFn } from '@tanstack/react-start';\nimport { getRequest } from '@tanstack/react-start/server';\nimport { getI18nConfig } from '@generaltranslation/react-core/pure';\nimport { getCookieValue } from 'gt-i18n/internal';\nimport type { LocaleResolverConfig } from 'gt-i18n/internal/types';\nimport {\n getConditionStore,\n isConditionStoreInitialized,\n} from '../condition-store/singleton';\nimport { resolveRequestConditions } from './requestConditions';\n\nexport const determineLocale = createIsomorphicFn()\n .server(determineLocaleServer)\n .client(determineLocaleClient);\n\n/**\n * Resolve the user's locale for the current TanStack Start request or browser.\n */\nexport function parseLocale(): string {\n const i18nConfig = getI18nConfig();\n return determineLocale({\n defaultLocale: i18nConfig.getDefaultLocale(),\n locales: i18nConfig.getLocales(),\n customMapping: i18nConfig.getCustomMapping(),\n });\n}\n\nfunction determineLocaleServer({\n defaultLocale,\n locales,\n customMapping,\n}: LocaleResolverConfig) {\n if (isConditionStoreInitialized()) {\n const conditionStore = getConditionStore();\n if (conditionStore.hasActiveScope()) {\n return conditionStore.getLocale();\n }\n }\n\n return resolveRequestConditions(getRequest(), {\n defaultLocale,\n locales,\n customMapping,\n }).locale;\n}\n\nfunction determineLocaleClient({\n defaultLocale,\n locales,\n customMapping,\n}: LocaleResolverConfig) {\n const i18nConfig = getI18nConfig();\n const localeCookieName = i18nConfig.getLocaleCookieName();\n const candidates: string[] = [];\n\n const cookie = getCookieValue(document.cookie, localeCookieName);\n if (cookie) candidates.push(cookie);\n\n if (candidates.length === 0) {\n console.warn(\n 'gt-tanstack-start(client): no locales could be determined for this request'\n );\n }\n\n return i18nConfig.resolveSupportedLocale(candidates, {\n defaultLocale,\n locales,\n customMapping,\n });\n}\n","import { AsyncLocalStorage } from 'node:async_hooks';\nimport type { I18nConfigParams } from '@generaltranslation/react-core/pure';\nimport { createDiagnosticMessage } from 'generaltranslation/internal';\nimport type { ReadonlyConditionStoreInterface } from 'gt-i18n/internal/types';\nimport { resolveRequestConditions } from '../functions/requestConditions';\n\nexport type RequestConditions = {\n locale: string;\n region?: string;\n enableI18n: boolean;\n};\n\nconst missingRequestScopeError = createDiagnosticMessage({\n source: 'gt-tanstack-start',\n severity: 'Error',\n whatHappened: 'Cannot read GT request state outside a request scope',\n why: 'the gt-tanstack-start request middleware has not initialized the ConditionStore for this request',\n fix: \"Register gtMiddleware from 'gt-tanstack-start/server' as global TanStack Start request middleware.\",\n});\n\n/**\n * Read-only ConditionStore backed by request-scoped AsyncLocalStorage.\n */\nexport class AsyncLocalConditionStore implements ReadonlyConditionStoreInterface {\n private readonly storage = new AsyncLocalStorage<RequestConditions>();\n\n constructor(private readonly config: I18nConfigParams) {}\n\n run<T>(request: Request, callback: () => T): T {\n const conditions = resolveRequestConditions(request, this.config);\n return this.storage.run(conditions, callback);\n }\n\n hasActiveScope(): boolean {\n return this.storage.getStore() !== undefined;\n }\n\n getLocale = (): string => this.getConditions().locale;\n\n getRegion = (): string | undefined => this.getConditions().region;\n\n getEnableI18n = (): boolean => this.getConditions().enableI18n;\n\n setLocale = (_locale: string): void => {};\n\n setRegion = (_region: string | undefined): void => {};\n\n setEnableI18n = (_enableI18n: boolean): void => {};\n\n private getConditions(): RequestConditions {\n const conditions = this.storage.getStore();\n if (!conditions) throw new Error(missingRequestScopeError);\n return conditions;\n }\n}\n","import { initializeGT as initializeReactGT } from 'gt-react';\nimport { AsyncLocalConditionStore } from '../condition-store/AsyncLocalConditionStore';\nimport { setConditionStore } from '../condition-store/singleton';\n\ntype InitializeGTParams = Parameters<typeof initializeReactGT>[0];\n\n/** Initialize GT and its server request condition store. */\nexport function initializeGT(config: InitializeGTParams): void {\n initializeReactGT(config);\n setConditionStore(new AsyncLocalConditionStore(config));\n}\n"],"mappings":";;;;;;;AAKA,SAAS,eAAe,MAAM;CAC7B,MAAM,UAAU,KAAK,MAAM;AAC3B,KAAI,CAAC,QAAS,QAAO;AACrB,QAAO,UAAU,KAAK,QAAQ,GAAG,UAAU,GAAG,QA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AU;AAC1C,MAAI,CAAC,WAAY,OAAM,IAAI,MAAM,yBAAyB;AAC1D,SAAO;;;;;;AC7CX,SAAgB,aAAa,QAAkC;AAC7D,gBAAkB,OAAO;AACzB,mBAAkB,IAAI,yBAAyB,OAAO,CAAC"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import * as _$_tanstack_react_start0 from "@tanstack/react-start";
|
|
2
|
+
import * as _$gt_i18n_types0 from "gt-i18n/types";
|
|
3
|
+
import { Message } from "gt-i18n/types";
|
|
4
|
+
|
|
5
|
+
//#region src/middleware/gtMiddleware.d.ts
|
|
6
|
+
/**
|
|
7
|
+
* Establish request-scoped GT conditions for SSR, server routes, and server
|
|
8
|
+
* functions.
|
|
9
|
+
*/
|
|
10
|
+
declare const gtMiddleware: _$_tanstack_react_start0.RequestMiddlewareAfterServer<{}, undefined, undefined>;
|
|
11
|
+
//#endregion
|
|
12
|
+
//#region src/functions/server.d.ts
|
|
13
|
+
/** Return the locale associated with the current server request. */
|
|
14
|
+
declare const getLocale: () => string;
|
|
15
|
+
/** Return whether internationalization is enabled for the current request. */
|
|
16
|
+
declare const getEnableI18n: () => boolean;
|
|
17
|
+
/** Return a string translation function for the current server request. */
|
|
18
|
+
declare const getGT: (messages?: Message[]) => Promise<_$gt_i18n_types0.SyncResolutionFunctionWithFallback>;
|
|
19
|
+
/** Return a registered-message translation function for the current request. */
|
|
20
|
+
declare const getMessages: () => Promise<_$gt_i18n_types0.MFunctionType>;
|
|
21
|
+
/** Return a dictionary translation function for the current server request. */
|
|
22
|
+
declare const getTranslations: (rootId?: string) => Promise<_$gt_i18n_types0.TFunctionType>;
|
|
23
|
+
//#endregion
|
|
24
|
+
export { getEnableI18n, getGT, getLocale, getMessages, getTranslations, gtMiddleware };
|
|
25
|
+
//# sourceMappingURL=server.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.d.mts","names":[],"sources":["../src/middleware/gtMiddleware.ts","../src/functions/server.ts"],"mappings":";;;;;;;;;cAOa,YAAA,EAEX,wBAAA,CAFuB,4BAAA;;;;cCGZ,SAAA;;cAKA,aAAA;ADRb;AAAA,cCaa,KAAA,GAAK,QAAA,GAAwC,OAAA,OAAS,OAAA,CAAF,gBAAA,CAAE,kCAAA;;cAYtD,WAAA,QAAW,OAAA,CAMtB,gBAAA,CANsB,aAAA;;cASX,eAAA,GAAe,MAAA,cAA4C,OAAA,CAOtE,gBAAA,CAPsE,aAAA"}
|
package/dist/server.mjs
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { createMiddleware, createServerOnlyFn } from "@tanstack/react-start";
|
|
2
|
+
import { createGlobalSingleton, getGTInternal, getMessagesInternal, getTranslationsInternal } from "gt-i18n/internal";
|
|
3
|
+
//#region ../core/dist/api-BOEGbEF6.mjs
|
|
4
|
+
function ensureSentence(text) {
|
|
5
|
+
const trimmed = text.trim();
|
|
6
|
+
if (!trimmed) return "";
|
|
7
|
+
return /[.!?)]$/.test(trimmed) ? trimmed : `${trimmed}.`;
|
|
8
|
+
}
|
|
9
|
+
function stripSentence(text) {
|
|
10
|
+
const trimmed = text.trim();
|
|
11
|
+
let end = trimmed.length;
|
|
12
|
+
while (end > 0) {
|
|
13
|
+
const char = trimmed[end - 1];
|
|
14
|
+
if (char !== "." && char !== "!" && char !== "?") break;
|
|
15
|
+
end -= 1;
|
|
16
|
+
}
|
|
17
|
+
return trimmed.slice(0, end);
|
|
18
|
+
}
|
|
19
|
+
function lowercaseFirstWord(text) {
|
|
20
|
+
return text.replace(/^[A-Z][a-z]/, (match) => match.toLowerCase());
|
|
21
|
+
}
|
|
22
|
+
function formatDetails(details) {
|
|
23
|
+
if (!details) return "";
|
|
24
|
+
const detailText = Array.isArray(details) ? details.join(", ") : details;
|
|
25
|
+
if (!detailText.trim()) return "";
|
|
26
|
+
return ensureSentence(`Details: ${detailText}`);
|
|
27
|
+
}
|
|
28
|
+
function createDiagnosticMessage({ source, severity, whatHappened, reassurance, why, fix, wayOut, details, docsUrl }) {
|
|
29
|
+
const prefix = source ? severity ? `${source} ${severity}:` : `${source}:` : severity ? `${severity}:` : "";
|
|
30
|
+
const whatAndWhy = why ? `${stripSentence(whatHappened)} because ${lowercaseFirstWord(stripSentence(why))}` : whatHappened;
|
|
31
|
+
const shouldCombineWayOut = !!fix && !!wayOut && /^[a-z]/.test(stripSentence(wayOut));
|
|
32
|
+
const messageParts = [
|
|
33
|
+
whatAndWhy,
|
|
34
|
+
reassurance,
|
|
35
|
+
shouldCombineWayOut ? `${stripSentence(fix)}, or ${lowercaseFirstWord(stripSentence(wayOut))}` : fix,
|
|
36
|
+
shouldCombineWayOut ? void 0 : wayOut,
|
|
37
|
+
formatDetails(details)
|
|
38
|
+
].filter((part) => !!part).map(ensureSentence);
|
|
39
|
+
if (docsUrl) messageParts.push(`Learn more: ${docsUrl}`);
|
|
40
|
+
const message = messageParts.join(" ");
|
|
41
|
+
return prefix ? `${prefix} ${message}` : message;
|
|
42
|
+
}
|
|
43
|
+
//#endregion
|
|
44
|
+
//#region src/condition-store/singleton.ts
|
|
45
|
+
const conditionStoreNotInitializedError = createDiagnosticMessage({
|
|
46
|
+
source: "gt-tanstack-start",
|
|
47
|
+
severity: "Error",
|
|
48
|
+
whatHappened: "Cannot read GT server request state before initialization",
|
|
49
|
+
why: "initializeGT() has not initialized the TanStack Start server condition store",
|
|
50
|
+
fix: "Call initializeGT() from 'gt-tanstack-start' during application setup before using gtMiddleware or server APIs."
|
|
51
|
+
});
|
|
52
|
+
const conditionStoreSingleton = createGlobalSingleton({
|
|
53
|
+
namespace: "tanstackStart",
|
|
54
|
+
key: "conditionStore",
|
|
55
|
+
source: "gt-tanstack-start",
|
|
56
|
+
notInitialized: () => conditionStoreNotInitializedError
|
|
57
|
+
});
|
|
58
|
+
const getConditionStore = conditionStoreSingleton.get;
|
|
59
|
+
conditionStoreSingleton.set;
|
|
60
|
+
conditionStoreSingleton.isInitialized;
|
|
61
|
+
//#endregion
|
|
62
|
+
//#region src/middleware/gtMiddleware.ts
|
|
63
|
+
/**
|
|
64
|
+
* Establish request-scoped GT conditions for SSR, server routes, and server
|
|
65
|
+
* functions.
|
|
66
|
+
*/
|
|
67
|
+
const gtMiddleware = createMiddleware().server(({ request, next }) => {
|
|
68
|
+
return getConditionStore().run(request, () => next());
|
|
69
|
+
});
|
|
70
|
+
//#endregion
|
|
71
|
+
//#region src/functions/server.ts
|
|
72
|
+
/** Return the locale associated with the current server request. */
|
|
73
|
+
const getLocale = createServerOnlyFn(() => {
|
|
74
|
+
return getConditionStore().getLocale();
|
|
75
|
+
});
|
|
76
|
+
/** Return whether internationalization is enabled for the current request. */
|
|
77
|
+
const getEnableI18n = createServerOnlyFn(() => {
|
|
78
|
+
return getConditionStore().getEnableI18n();
|
|
79
|
+
});
|
|
80
|
+
/** Return a string translation function for the current server request. */
|
|
81
|
+
const getGT = createServerOnlyFn(async (messages) => {
|
|
82
|
+
const conditionStore = getConditionStore();
|
|
83
|
+
return getGTInternal({
|
|
84
|
+
locale: conditionStore.getLocale(),
|
|
85
|
+
enableI18n: conditionStore.getEnableI18n()
|
|
86
|
+
}, messages);
|
|
87
|
+
});
|
|
88
|
+
/** Return a registered-message translation function for the current request. */
|
|
89
|
+
const getMessages = createServerOnlyFn(async () => {
|
|
90
|
+
const conditionStore = getConditionStore();
|
|
91
|
+
return getMessagesInternal({
|
|
92
|
+
locale: conditionStore.getLocale(),
|
|
93
|
+
enableI18n: conditionStore.getEnableI18n()
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
/** Return a dictionary translation function for the current server request. */
|
|
97
|
+
const getTranslations = createServerOnlyFn(async (rootId) => {
|
|
98
|
+
const conditionStore = getConditionStore();
|
|
99
|
+
return getTranslationsInternal({
|
|
100
|
+
locale: conditionStore.getLocale(),
|
|
101
|
+
enableI18n: conditionStore.getEnableI18n(),
|
|
102
|
+
rootId
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
//#endregion
|
|
106
|
+
export { getEnableI18n, getGT, getLocale, getMessages, getTranslations, gtMiddleware };
|
|
107
|
+
|
|
108
|
+
//# sourceMappingURL=server.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.mjs","names":[],"sources":["../../core/dist/api-BOEGbEF6.mjs","../src/condition-store/singleton.ts","../src/middleware/gtMiddleware.ts","../src/functions/server.ts"],"sourcesContent":["//#region src/settings/settings.ts\nconst libraryDefaultLocale = \"en\";\nconst defaultTimeout = 6e4;\n//#endregion\n//#region src/logging/diagnostics.ts\nfunction ensureSentence(text) {\n\tconst trimmed = text.trim();\n\tif (!trimmed) return \"\";\n\treturn /[.!?)]$/.test(trimmed) ? trimmed : `${trimmed}.`;\n}\nfunction stripSentence(text) {\n\tconst trimmed = text.trim();\n\tlet end = trimmed.length;\n\twhile (end > 0) {\n\t\tconst char = trimmed[end - 1];\n\t\tif (char !== \".\" && char !== \"!\" && char !== \"?\") break;\n\t\tend -= 1;\n\t}\n\treturn trimmed.slice(0, end);\n}\nfunction lowercaseFirstWord(text) {\n\treturn text.replace(/^[A-Z][a-z]/, (match) => match.toLowerCase());\n}\nfunction formatDetails(details) {\n\tif (!details) return \"\";\n\tconst detailText = Array.isArray(details) ? details.join(\", \") : details;\n\tif (!detailText.trim()) return \"\";\n\treturn ensureSentence(`Details: ${detailText}`);\n}\nfunction formatDiagnosticErrorDetails(error) {\n\tif (error == null) return void 0;\n\treturn String(error);\n}\nfunction createDiagnosticMessage({ source, severity, whatHappened, reassurance, why, fix, wayOut, details, docsUrl }) {\n\tconst prefix = source ? severity ? `${source} ${severity}:` : `${source}:` : severity ? `${severity}:` : \"\";\n\tconst whatAndWhy = why ? `${stripSentence(whatHappened)} because ${lowercaseFirstWord(stripSentence(why))}` : whatHappened;\n\tconst shouldCombineWayOut = !!fix && !!wayOut && /^[a-z]/.test(stripSentence(wayOut));\n\tconst messageParts = [\n\t\twhatAndWhy,\n\t\treassurance,\n\t\tshouldCombineWayOut ? `${stripSentence(fix)}, or ${lowercaseFirstWord(stripSentence(wayOut))}` : fix,\n\t\tshouldCombineWayOut ? void 0 : wayOut,\n\t\tformatDetails(details)\n\t].filter((part) => !!part).map(ensureSentence);\n\tif (docsUrl) messageParts.push(`Learn more: ${docsUrl}`);\n\tconst message = messageParts.join(\" \");\n\treturn prefix ? `${prefix} ${message}` : message;\n}\n//#endregion\n//#region src/settings/settingsUrls.ts\nconst defaultCacheUrl = \"https://cdn.gtx.dev\";\nconst defaultBaseUrl = \"https://api2.gtx.dev\";\nconst defaultRuntimeApiUrl = \"https://runtime2.gtx.dev\";\n//#endregion\n//#region src/translate/api.ts\nconst API_VERSION = \"2026-03-06.v1\";\n//#endregion\nexport { createDiagnosticMessage as a, libraryDefaultLocale as c, defaultRuntimeApiUrl as i, defaultBaseUrl as n, formatDiagnosticErrorDetails as o, defaultCacheUrl as r, defaultTimeout as s, API_VERSION as t };\n\n//# sourceMappingURL=api-BOEGbEF6.mjs.map","import { createDiagnosticMessage } from 'generaltranslation/internal';\nimport { createGlobalSingleton } from 'gt-i18n/internal';\nimport type { AsyncLocalConditionStore } from './AsyncLocalConditionStore';\n\nconst conditionStoreNotInitializedError = createDiagnosticMessage({\n source: 'gt-tanstack-start',\n severity: 'Error',\n whatHappened: 'Cannot read GT server request state before initialization',\n why: 'initializeGT() has not initialized the TanStack Start server condition store',\n fix: \"Call initializeGT() from 'gt-tanstack-start' during application setup before using gtMiddleware or server APIs.\",\n});\n\nconst conditionStoreSingleton = createGlobalSingleton<AsyncLocalConditionStore>(\n {\n namespace: 'tanstackStart',\n key: 'conditionStore',\n source: 'gt-tanstack-start',\n notInitialized: () => conditionStoreNotInitializedError,\n }\n);\n\nexport const getConditionStore = conditionStoreSingleton.get;\nexport const setConditionStore = conditionStoreSingleton.set;\nexport const isConditionStoreInitialized =\n conditionStoreSingleton.isInitialized;\n","import { createMiddleware } from '@tanstack/react-start';\nimport { getConditionStore } from '../condition-store/singleton';\n\n/**\n * Establish request-scoped GT conditions for SSR, server routes, and server\n * functions.\n */\nexport const gtMiddleware = createMiddleware().server(({ request, next }) => {\n return getConditionStore().run(request, () => next());\n});\n","import { createServerOnlyFn } from '@tanstack/react-start';\nimport {\n getGTInternal,\n getMessagesInternal,\n getTranslationsInternal,\n} from 'gt-i18n/internal';\nimport type { Message } from 'gt-i18n/types';\nimport { getConditionStore } from '../condition-store/singleton';\n\n/** Return the locale associated with the current server request. */\nexport const getLocale = createServerOnlyFn((): string => {\n return getConditionStore().getLocale();\n});\n\n/** Return whether internationalization is enabled for the current request. */\nexport const getEnableI18n = createServerOnlyFn((): boolean => {\n return getConditionStore().getEnableI18n();\n});\n\n/** Return a string translation function for the current server request. */\nexport const getGT = createServerOnlyFn(async (messages?: Message[]) => {\n const conditionStore = getConditionStore();\n return getGTInternal(\n {\n locale: conditionStore.getLocale(),\n enableI18n: conditionStore.getEnableI18n(),\n },\n messages\n );\n});\n\n/** Return a registered-message translation function for the current request. */\nexport const getMessages = createServerOnlyFn(async () => {\n const conditionStore = getConditionStore();\n return getMessagesInternal({\n locale: conditionStore.getLocale(),\n enableI18n: conditionStore.getEnableI18n(),\n });\n});\n\n/** Return a dictionary translation function for the current server request. */\nexport const getTranslations = createServerOnlyFn(async (rootId?: string) => {\n const conditionStore = getConditionStore();\n return getTranslationsInternal({\n locale: conditionStore.getLocale(),\n enableI18n: conditionStore.getEnableI18n(),\n rootId,\n });\n});\n"],"mappings":";;;AAKA,SAAS,eAAe,MAAM;CAC7B,MAAM,UAAU,KAAK,MAAM;AAC3B,KAAI,CAAC,QAAS,QAAO;AACrB,QAAO,UAAU,KAAK,QAAQ,GAAG,UAAU,GAAG,QAAQ;;AAEvD,SAAS,cAAc,MAAM;CAC5B,MAAM,UAAU,KAAK,MAAM;CAC3B,IAAI,MAAM,QAAQ;AAClB,QAAO,MAAM,GAAG;EACf,MAAM,OAAO,QAAQ,MAAM;AAC3B,MAAI,SAAS,OAAO,SAAS,OAAO,SAAS,IAAK;AAClD,SAAO;;AAER,QAAO,QAA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AwB;AAEvD,wBAAwB;;;;;;;ACjB1B,MAAa,eAAe,kBAAkB,CAAC,QAAQ,EAAE,SAAS,WAAW;AAC3E,QAAO,mBAAmB,CAAC,IAAI,eAAe,MAAM,CAAC;EACrD;;;;ACCF,MAAa,YAAY,yBAAiC;AACxD,QAAO,mBAAmB,CAAC,WAAW;EACtC;;AAGF,MAAa,gBAAgB,yBAAkC;AAC7D,QAAO,mBAAmB,CAAC,eAAe;EAC1C;;AAGF,MAAa,QAAQ,mBAAmB,OAAO,aAAyB;CACtE,MAAM,iBAAiB,mBAAmB;AAC1C,QAAO,cACL;EACE,QAAQ,eAAe,WAAW;EAClC,YAAY,eAAe,eAAe;EAC3C,EACD,SACD;EACD;;AAGF,MAAa,cAAc,mBAAmB,YAAY;CACxD,MAAM,iBAAiB,mBAAmB;AAC1C,QAAO,oBAAoB;EACzB,QAAQ,eAAe,WAAW;EAClC,YAAY,eAAe,eAAe;EAC3C,CAAC;EACF;;AAGF,MAAa,kBAAkB,mBAAmB,OAAO,WAAoB;CAC3E,MAAM,iBAAiB,mBAAmB;AAC1C,QAAO,wBAAwB;EAC7B,QAAQ,eAAe,WAAW;EAClC,YAAY,eAAe,eAAe;EAC1C;EACD,CAAC;EACF"}
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "gt-tanstack-start",
|
|
3
|
-
"version": "11.0.
|
|
3
|
+
"version": "11.0.12",
|
|
4
4
|
"description": "TanStack Start integration for General Translation",
|
|
5
|
-
"main": "./dist/index.
|
|
6
|
-
"module": "./dist/index.mjs",
|
|
7
|
-
"types": "./dist/index.d.
|
|
5
|
+
"main": "./dist/index.server.mjs",
|
|
6
|
+
"module": "./dist/index.server.mjs",
|
|
7
|
+
"types": "./dist/index.server.d.mts",
|
|
8
8
|
"files": [
|
|
9
9
|
"dist",
|
|
10
10
|
"CHANGELOG.md"
|
|
@@ -16,9 +16,9 @@
|
|
|
16
16
|
"@tanstack/react-start": ">=1.159.0"
|
|
17
17
|
},
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"gt-react": "11.0.
|
|
19
|
+
"gt-react": "11.0.12",
|
|
20
|
+
"@generaltranslation/react-core": "11.0.12",
|
|
20
21
|
"gt-i18n": "1.0.7",
|
|
21
|
-
"@generaltranslation/react-core": "11.0.11",
|
|
22
22
|
"generaltranslation": "9.0.3"
|
|
23
23
|
},
|
|
24
24
|
"repository": {
|
|
@@ -41,15 +41,32 @@
|
|
|
41
41
|
},
|
|
42
42
|
"exports": {
|
|
43
43
|
".": {
|
|
44
|
-
"
|
|
45
|
-
"
|
|
46
|
-
|
|
44
|
+
"browser": {
|
|
45
|
+
"import": {
|
|
46
|
+
"types": "./dist/index.client.d.mts",
|
|
47
|
+
"default": "./dist/index.client.mjs"
|
|
48
|
+
},
|
|
49
|
+
"default": "./dist/index.client.mjs"
|
|
47
50
|
},
|
|
48
51
|
"import": {
|
|
49
|
-
"types": "./dist/index.d.mts",
|
|
50
|
-
"default": "./dist/index.mjs"
|
|
52
|
+
"types": "./dist/index.server.d.mts",
|
|
53
|
+
"default": "./dist/index.server.mjs"
|
|
51
54
|
},
|
|
52
|
-
"default": "./dist/index.mjs"
|
|
55
|
+
"default": "./dist/index.server.mjs"
|
|
56
|
+
},
|
|
57
|
+
"./server": {
|
|
58
|
+
"import": {
|
|
59
|
+
"types": "./dist/server.d.mts",
|
|
60
|
+
"default": "./dist/server.mjs"
|
|
61
|
+
},
|
|
62
|
+
"default": "./dist/server.mjs"
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
"typesVersions": {
|
|
66
|
+
"*": {
|
|
67
|
+
"server": [
|
|
68
|
+
"./dist/server.d.mts"
|
|
69
|
+
]
|
|
53
70
|
}
|
|
54
71
|
},
|
|
55
72
|
"keywords": [
|
package/dist/index.cjs
DELETED
|
@@ -1,260 +0,0 @@
|
|
|
1
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
let _tanstack_react_start = require("@tanstack/react-start");
|
|
3
|
-
let _tanstack_react_start_server = require("@tanstack/react-start/server");
|
|
4
|
-
let _generaltranslation_react_core_pure = require("@generaltranslation/react-core/pure");
|
|
5
|
-
let gt_i18n_internal = require("gt-i18n/internal");
|
|
6
|
-
let gt_react = require("gt-react");
|
|
7
|
-
//#region src/functions/parseLocale.ts
|
|
8
|
-
const determineLocale = (0, _tanstack_react_start.createIsomorphicFn)().server(determineLocaleServer).client(determineLocaleClient);
|
|
9
|
-
/**
|
|
10
|
-
* Resolve the user's locale for the current TanStack Start request or browser.
|
|
11
|
-
*/
|
|
12
|
-
function parseLocale() {
|
|
13
|
-
const i18nConfig = (0, _generaltranslation_react_core_pure.getI18nConfig)();
|
|
14
|
-
return determineLocale({
|
|
15
|
-
defaultLocale: i18nConfig.getDefaultLocale(),
|
|
16
|
-
locales: i18nConfig.getLocales(),
|
|
17
|
-
customMapping: i18nConfig.getCustomMapping()
|
|
18
|
-
});
|
|
19
|
-
}
|
|
20
|
-
function determineLocaleServer({ defaultLocale, locales, customMapping }) {
|
|
21
|
-
const i18nConfig = (0, _generaltranslation_react_core_pure.getI18nConfig)();
|
|
22
|
-
const localeCookieName = i18nConfig.getLocaleCookieName();
|
|
23
|
-
const candidates = [];
|
|
24
|
-
const cookie = (0, _tanstack_react_start_server.getCookie)(localeCookieName);
|
|
25
|
-
if (cookie) candidates.push(cookie);
|
|
26
|
-
candidates.push(...(0, gt_i18n_internal.parseAcceptLanguage)((0, _tanstack_react_start_server.getRequestHeader)("accept-language")));
|
|
27
|
-
if (candidates.length === 0) console.warn("gt-tanstack-start(server): no locales could be determined for this request");
|
|
28
|
-
const locale = i18nConfig.resolveSupportedLocale(candidates, {
|
|
29
|
-
defaultLocale,
|
|
30
|
-
locales,
|
|
31
|
-
customMapping
|
|
32
|
-
});
|
|
33
|
-
(0, _tanstack_react_start_server.setCookie)(localeCookieName, locale, {
|
|
34
|
-
path: "/",
|
|
35
|
-
sameSite: "lax",
|
|
36
|
-
maxAge: 3600 * 24 * 365
|
|
37
|
-
});
|
|
38
|
-
return locale;
|
|
39
|
-
}
|
|
40
|
-
function determineLocaleClient({ defaultLocale, locales, customMapping }) {
|
|
41
|
-
const i18nConfig = (0, _generaltranslation_react_core_pure.getI18nConfig)();
|
|
42
|
-
const localeCookieName = i18nConfig.getLocaleCookieName();
|
|
43
|
-
const candidates = [];
|
|
44
|
-
const cookie = (0, gt_i18n_internal.getCookieValue)(document.cookie, localeCookieName);
|
|
45
|
-
if (cookie) candidates.push(cookie);
|
|
46
|
-
if (candidates.length === 0) console.warn("gt-tanstack-start(client): no locales could be determined for this request");
|
|
47
|
-
return i18nConfig.resolveSupportedLocale(candidates, {
|
|
48
|
-
defaultLocale,
|
|
49
|
-
locales,
|
|
50
|
-
customMapping
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
//#endregion
|
|
54
|
-
Object.defineProperty(exports, "Branch", {
|
|
55
|
-
enumerable: true,
|
|
56
|
-
get: function() {
|
|
57
|
-
return gt_react.Branch;
|
|
58
|
-
}
|
|
59
|
-
});
|
|
60
|
-
Object.defineProperty(exports, "Currency", {
|
|
61
|
-
enumerable: true,
|
|
62
|
-
get: function() {
|
|
63
|
-
return gt_react.Currency;
|
|
64
|
-
}
|
|
65
|
-
});
|
|
66
|
-
Object.defineProperty(exports, "DateTime", {
|
|
67
|
-
enumerable: true,
|
|
68
|
-
get: function() {
|
|
69
|
-
return gt_react.DateTime;
|
|
70
|
-
}
|
|
71
|
-
});
|
|
72
|
-
Object.defineProperty(exports, "Derive", {
|
|
73
|
-
enumerable: true,
|
|
74
|
-
get: function() {
|
|
75
|
-
return gt_react.Derive;
|
|
76
|
-
}
|
|
77
|
-
});
|
|
78
|
-
Object.defineProperty(exports, "GTProvider", {
|
|
79
|
-
enumerable: true,
|
|
80
|
-
get: function() {
|
|
81
|
-
return gt_react.GTProvider;
|
|
82
|
-
}
|
|
83
|
-
});
|
|
84
|
-
Object.defineProperty(exports, "LocaleSelector", {
|
|
85
|
-
enumerable: true,
|
|
86
|
-
get: function() {
|
|
87
|
-
return gt_react.LocaleSelector;
|
|
88
|
-
}
|
|
89
|
-
});
|
|
90
|
-
Object.defineProperty(exports, "Num", {
|
|
91
|
-
enumerable: true,
|
|
92
|
-
get: function() {
|
|
93
|
-
return gt_react.Num;
|
|
94
|
-
}
|
|
95
|
-
});
|
|
96
|
-
Object.defineProperty(exports, "Plural", {
|
|
97
|
-
enumerable: true,
|
|
98
|
-
get: function() {
|
|
99
|
-
return gt_react.Plural;
|
|
100
|
-
}
|
|
101
|
-
});
|
|
102
|
-
Object.defineProperty(exports, "RelativeTime", {
|
|
103
|
-
enumerable: true,
|
|
104
|
-
get: function() {
|
|
105
|
-
return gt_react.RelativeTime;
|
|
106
|
-
}
|
|
107
|
-
});
|
|
108
|
-
Object.defineProperty(exports, "T", {
|
|
109
|
-
enumerable: true,
|
|
110
|
-
get: function() {
|
|
111
|
-
return gt_react.T;
|
|
112
|
-
}
|
|
113
|
-
});
|
|
114
|
-
Object.defineProperty(exports, "Var", {
|
|
115
|
-
enumerable: true,
|
|
116
|
-
get: function() {
|
|
117
|
-
return gt_react.Var;
|
|
118
|
-
}
|
|
119
|
-
});
|
|
120
|
-
Object.defineProperty(exports, "declareVar", {
|
|
121
|
-
enumerable: true,
|
|
122
|
-
get: function() {
|
|
123
|
-
return gt_react.declareVar;
|
|
124
|
-
}
|
|
125
|
-
});
|
|
126
|
-
Object.defineProperty(exports, "decodeMsg", {
|
|
127
|
-
enumerable: true,
|
|
128
|
-
get: function() {
|
|
129
|
-
return gt_react.decodeMsg;
|
|
130
|
-
}
|
|
131
|
-
});
|
|
132
|
-
Object.defineProperty(exports, "decodeOptions", {
|
|
133
|
-
enumerable: true,
|
|
134
|
-
get: function() {
|
|
135
|
-
return gt_react.decodeOptions;
|
|
136
|
-
}
|
|
137
|
-
});
|
|
138
|
-
Object.defineProperty(exports, "decodeVars", {
|
|
139
|
-
enumerable: true,
|
|
140
|
-
get: function() {
|
|
141
|
-
return gt_react.decodeVars;
|
|
142
|
-
}
|
|
143
|
-
});
|
|
144
|
-
Object.defineProperty(exports, "derive", {
|
|
145
|
-
enumerable: true,
|
|
146
|
-
get: function() {
|
|
147
|
-
return gt_react.derive;
|
|
148
|
-
}
|
|
149
|
-
});
|
|
150
|
-
Object.defineProperty(exports, "getTranslationsSnapshot", {
|
|
151
|
-
enumerable: true,
|
|
152
|
-
get: function() {
|
|
153
|
-
return gt_react.getTranslationsSnapshot;
|
|
154
|
-
}
|
|
155
|
-
});
|
|
156
|
-
Object.defineProperty(exports, "gtFallback", {
|
|
157
|
-
enumerable: true,
|
|
158
|
-
get: function() {
|
|
159
|
-
return gt_react.gtFallback;
|
|
160
|
-
}
|
|
161
|
-
});
|
|
162
|
-
Object.defineProperty(exports, "initializeGT", {
|
|
163
|
-
enumerable: true,
|
|
164
|
-
get: function() {
|
|
165
|
-
return gt_react.initializeGT;
|
|
166
|
-
}
|
|
167
|
-
});
|
|
168
|
-
Object.defineProperty(exports, "mFallback", {
|
|
169
|
-
enumerable: true,
|
|
170
|
-
get: function() {
|
|
171
|
-
return gt_react.mFallback;
|
|
172
|
-
}
|
|
173
|
-
});
|
|
174
|
-
Object.defineProperty(exports, "msg", {
|
|
175
|
-
enumerable: true,
|
|
176
|
-
get: function() {
|
|
177
|
-
return gt_react.msg;
|
|
178
|
-
}
|
|
179
|
-
});
|
|
180
|
-
exports.parseLocale = parseLocale;
|
|
181
|
-
Object.defineProperty(exports, "t", {
|
|
182
|
-
enumerable: true,
|
|
183
|
-
get: function() {
|
|
184
|
-
return gt_react.t;
|
|
185
|
-
}
|
|
186
|
-
});
|
|
187
|
-
Object.defineProperty(exports, "useCustomMapping", {
|
|
188
|
-
enumerable: true,
|
|
189
|
-
get: function() {
|
|
190
|
-
return gt_react.useCustomMapping;
|
|
191
|
-
}
|
|
192
|
-
});
|
|
193
|
-
Object.defineProperty(exports, "useDefaultLocale", {
|
|
194
|
-
enumerable: true,
|
|
195
|
-
get: function() {
|
|
196
|
-
return gt_react.useDefaultLocale;
|
|
197
|
-
}
|
|
198
|
-
});
|
|
199
|
-
Object.defineProperty(exports, "useEnableI18n", {
|
|
200
|
-
enumerable: true,
|
|
201
|
-
get: function() {
|
|
202
|
-
return gt_react.useEnableI18n;
|
|
203
|
-
}
|
|
204
|
-
});
|
|
205
|
-
Object.defineProperty(exports, "useFormatLocales", {
|
|
206
|
-
enumerable: true,
|
|
207
|
-
get: function() {
|
|
208
|
-
return gt_react.useFormatLocales;
|
|
209
|
-
}
|
|
210
|
-
});
|
|
211
|
-
Object.defineProperty(exports, "useGT", {
|
|
212
|
-
enumerable: true,
|
|
213
|
-
get: function() {
|
|
214
|
-
return gt_react.useGT;
|
|
215
|
-
}
|
|
216
|
-
});
|
|
217
|
-
Object.defineProperty(exports, "useLocale", {
|
|
218
|
-
enumerable: true,
|
|
219
|
-
get: function() {
|
|
220
|
-
return gt_react.useLocale;
|
|
221
|
-
}
|
|
222
|
-
});
|
|
223
|
-
Object.defineProperty(exports, "useLocaleSelector", {
|
|
224
|
-
enumerable: true,
|
|
225
|
-
get: function() {
|
|
226
|
-
return gt_react.useLocaleSelector;
|
|
227
|
-
}
|
|
228
|
-
});
|
|
229
|
-
Object.defineProperty(exports, "useLocales", {
|
|
230
|
-
enumerable: true,
|
|
231
|
-
get: function() {
|
|
232
|
-
return gt_react.useLocales;
|
|
233
|
-
}
|
|
234
|
-
});
|
|
235
|
-
Object.defineProperty(exports, "useMessages", {
|
|
236
|
-
enumerable: true,
|
|
237
|
-
get: function() {
|
|
238
|
-
return gt_react.useMessages;
|
|
239
|
-
}
|
|
240
|
-
});
|
|
241
|
-
Object.defineProperty(exports, "useSetEnableI18n", {
|
|
242
|
-
enumerable: true,
|
|
243
|
-
get: function() {
|
|
244
|
-
return gt_react.useSetEnableI18n;
|
|
245
|
-
}
|
|
246
|
-
});
|
|
247
|
-
Object.defineProperty(exports, "useSetLocale", {
|
|
248
|
-
enumerable: true,
|
|
249
|
-
get: function() {
|
|
250
|
-
return gt_react.useSetLocale;
|
|
251
|
-
}
|
|
252
|
-
});
|
|
253
|
-
Object.defineProperty(exports, "useTranslations", {
|
|
254
|
-
enumerable: true,
|
|
255
|
-
get: function() {
|
|
256
|
-
return gt_react.useTranslations;
|
|
257
|
-
}
|
|
258
|
-
});
|
|
259
|
-
|
|
260
|
-
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":[],"sources":["../src/functions/parseLocale.ts"],"sourcesContent":["import { createIsomorphicFn } from '@tanstack/react-start';\nimport {\n getRequestHeader,\n getCookie,\n setCookie,\n} from '@tanstack/react-start/server';\nimport { getI18nConfig } from '@generaltranslation/react-core/pure';\nimport { getCookieValue, parseAcceptLanguage } from 'gt-i18n/internal';\nimport type { LocaleResolverConfig } from 'gt-i18n/internal/types';\n\nexport const determineLocale = createIsomorphicFn()\n .server(determineLocaleServer)\n .client(determineLocaleClient);\n\n/**\n * Resolve the user's locale for the current TanStack Start request or browser.\n */\nexport function parseLocale(): string {\n const i18nConfig = getI18nConfig();\n return determineLocale({\n defaultLocale: i18nConfig.getDefaultLocale(),\n locales: i18nConfig.getLocales(),\n customMapping: i18nConfig.getCustomMapping(),\n });\n}\n\nfunction determineLocaleServer({\n defaultLocale,\n locales,\n customMapping,\n}: LocaleResolverConfig) {\n const i18nConfig = getI18nConfig();\n const localeCookieName = i18nConfig.getLocaleCookieName();\n const candidates: string[] = [];\n\n const cookie = getCookie(localeCookieName);\n if (cookie) candidates.push(cookie);\n\n candidates.push(...parseAcceptLanguage(getRequestHeader('accept-language')));\n\n if (candidates.length === 0) {\n console.warn(\n 'gt-tanstack-start(server): no locales could be determined for this request'\n );\n }\n\n const locale = i18nConfig.resolveSupportedLocale(candidates, {\n defaultLocale,\n locales,\n customMapping,\n });\n\n setCookie(localeCookieName, locale, {\n path: '/',\n sameSite: 'lax',\n maxAge: 60 * 60 * 24 * 365,\n });\n\n return locale;\n}\n\nfunction determineLocaleClient({\n defaultLocale,\n locales,\n customMapping,\n}: LocaleResolverConfig) {\n const i18nConfig = getI18nConfig();\n const localeCookieName = i18nConfig.getLocaleCookieName();\n const candidates: string[] = [];\n\n const cookie = getCookieValue(document.cookie, localeCookieName);\n if (cookie) candidates.push(cookie);\n\n if (candidates.length === 0) {\n console.warn(\n 'gt-tanstack-start(client): no locales could be determined for this request'\n );\n }\n\n return i18nConfig.resolveSupportedLocale(candidates, {\n defaultLocale,\n locales,\n customMapping,\n });\n}\n"],"mappings":";;;;;;;AAUA,MAAa,mBAAA,GAAA,sBAAA,qBAAsC,CAChD,OAAO,sBAAsB,CAC7B,OAAO,sBAAsB;;;;AAKhC,SAAgB,cAAsB;CACpC,MAAM,cAAA,GAAA,oCAAA,gBAA4B;AAClC,QAAO,gBAAgB;EACrB,eAAe,WAAW,kBAAkB;EAC5C,SAAS,WAAW,YAAY;EAChC,eAAe,WAAW,kBAAkB;EAC7C,CAAC;;AAGJ,SAAS,sBAAsB,EAC7B,eACA,SACA,iBACuB;CACvB,MAAM,cAAA,GAAA,oCAAA,gBAA4B;CAClC,MAAM,mBAAmB,WAAW,qBAAqB;CACzD,MAAM,aAAuB,EAAE;CAE/B,MAAM,UAAA,GAAA,6BAAA,WAAmB,iBAAiB;AAC1C,KAAI,OAAQ,YAAW,KAAK,OAAO;AAEnC,YAAW,KAAK,IAAA,GAAA,iBAAA,sBAAA,GAAA,6BAAA,kBAAwC,kBAAkB,CAAC,CAAC;AAE5E,KAAI,WAAW,WAAW,EACxB,SAAQ,KACN,6EACD;CAGH,MAAM,SAAS,WAAW,uBAAuB,YAAY;EAC3D;EACA;EACA;EACD,CAAC;AAEF,EAAA,GAAA,6BAAA,WAAU,kBAAkB,QAAQ;EAClC,MAAM;EACN,UAAU;EACV,QAAQ,OAAU,KAAK;EACxB,CAAC;AAEF,QAAO;;AAGT,SAAS,sBAAsB,EAC7B,eACA,SACA,iBACuB;CACvB,MAAM,cAAA,GAAA,oCAAA,gBAA4B;CAClC,MAAM,mBAAmB,WAAW,qBAAqB;CACzD,MAAM,aAAuB,EAAE;CAE/B,MAAM,UAAA,GAAA,iBAAA,gBAAwB,SAAS,QAAQ,iBAAiB;AAChE,KAAI,OAAQ,YAAW,KAAK,OAAO;AAEnC,KAAI,WAAW,WAAW,EACxB,SAAQ,KACN,6EACD;AAGH,QAAO,WAAW,uBAAuB,YAAY;EACnD;EACA;EACA;EACD,CAAC"}
|
package/dist/index.d.cts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.cts","names":[],"sources":["../src/functions/parseLocale.ts"],"mappings":";;;;;;iBAiBgB,WAAA,CAAA"}
|
package/dist/index.d.mts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/functions/parseLocale.ts"],"mappings":";;;;;iBAiBgB,WAAA,CAAA"}
|
package/dist/index.mjs
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import { createIsomorphicFn } from "@tanstack/react-start";
|
|
2
|
-
import { getCookie, getRequestHeader, setCookie } from "@tanstack/react-start/server";
|
|
3
|
-
import { getI18nConfig } from "@generaltranslation/react-core/pure";
|
|
4
|
-
import { getCookieValue, parseAcceptLanguage } from "gt-i18n/internal";
|
|
5
|
-
import { Branch, Currency, DateTime, Derive, GTProvider, LocaleSelector, Num, Plural, RelativeTime, T, Var, declareVar, decodeMsg, decodeOptions, decodeVars, derive, getTranslationsSnapshot, gtFallback, initializeGT, mFallback, msg, t, useCustomMapping, useDefaultLocale, useEnableI18n, useFormatLocales, useGT, useLocale, useLocaleSelector, useLocales, useMessages, useSetEnableI18n, useSetLocale, useTranslations } from "gt-react";
|
|
6
|
-
//#region src/functions/parseLocale.ts
|
|
7
|
-
const determineLocale = createIsomorphicFn().server(determineLocaleServer).client(determineLocaleClient);
|
|
8
|
-
/**
|
|
9
|
-
* Resolve the user's locale for the current TanStack Start request or browser.
|
|
10
|
-
*/
|
|
11
|
-
function parseLocale() {
|
|
12
|
-
const i18nConfig = getI18nConfig();
|
|
13
|
-
return determineLocale({
|
|
14
|
-
defaultLocale: i18nConfig.getDefaultLocale(),
|
|
15
|
-
locales: i18nConfig.getLocales(),
|
|
16
|
-
customMapping: i18nConfig.getCustomMapping()
|
|
17
|
-
});
|
|
18
|
-
}
|
|
19
|
-
function determineLocaleServer({ defaultLocale, locales, customMapping }) {
|
|
20
|
-
const i18nConfig = getI18nConfig();
|
|
21
|
-
const localeCookieName = i18nConfig.getLocaleCookieName();
|
|
22
|
-
const candidates = [];
|
|
23
|
-
const cookie = getCookie(localeCookieName);
|
|
24
|
-
if (cookie) candidates.push(cookie);
|
|
25
|
-
candidates.push(...parseAcceptLanguage(getRequestHeader("accept-language")));
|
|
26
|
-
if (candidates.length === 0) console.warn("gt-tanstack-start(server): no locales could be determined for this request");
|
|
27
|
-
const locale = i18nConfig.resolveSupportedLocale(candidates, {
|
|
28
|
-
defaultLocale,
|
|
29
|
-
locales,
|
|
30
|
-
customMapping
|
|
31
|
-
});
|
|
32
|
-
setCookie(localeCookieName, locale, {
|
|
33
|
-
path: "/",
|
|
34
|
-
sameSite: "lax",
|
|
35
|
-
maxAge: 3600 * 24 * 365
|
|
36
|
-
});
|
|
37
|
-
return locale;
|
|
38
|
-
}
|
|
39
|
-
function determineLocaleClient({ defaultLocale, locales, customMapping }) {
|
|
40
|
-
const i18nConfig = getI18nConfig();
|
|
41
|
-
const localeCookieName = i18nConfig.getLocaleCookieName();
|
|
42
|
-
const candidates = [];
|
|
43
|
-
const cookie = getCookieValue(document.cookie, localeCookieName);
|
|
44
|
-
if (cookie) candidates.push(cookie);
|
|
45
|
-
if (candidates.length === 0) console.warn("gt-tanstack-start(client): no locales could be determined for this request");
|
|
46
|
-
return i18nConfig.resolveSupportedLocale(candidates, {
|
|
47
|
-
defaultLocale,
|
|
48
|
-
locales,
|
|
49
|
-
customMapping
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
//#endregion
|
|
53
|
-
export { Branch, Currency, DateTime, Derive, GTProvider, LocaleSelector, Num, Plural, RelativeTime, T, Var, declareVar, decodeMsg, decodeOptions, decodeVars, derive, getTranslationsSnapshot, gtFallback, initializeGT, mFallback, msg, parseLocale, t, useCustomMapping, useDefaultLocale, useEnableI18n, useFormatLocales, useGT, useLocale, useLocaleSelector, useLocales, useMessages, useSetEnableI18n, useSetLocale, useTranslations };
|
|
54
|
-
|
|
55
|
-
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":[],"sources":["../src/functions/parseLocale.ts"],"sourcesContent":["import { createIsomorphicFn } from '@tanstack/react-start';\nimport {\n getRequestHeader,\n getCookie,\n setCookie,\n} from '@tanstack/react-start/server';\nimport { getI18nConfig } from '@generaltranslation/react-core/pure';\nimport { getCookieValue, parseAcceptLanguage } from 'gt-i18n/internal';\nimport type { LocaleResolverConfig } from 'gt-i18n/internal/types';\n\nexport const determineLocale = createIsomorphicFn()\n .server(determineLocaleServer)\n .client(determineLocaleClient);\n\n/**\n * Resolve the user's locale for the current TanStack Start request or browser.\n */\nexport function parseLocale(): string {\n const i18nConfig = getI18nConfig();\n return determineLocale({\n defaultLocale: i18nConfig.getDefaultLocale(),\n locales: i18nConfig.getLocales(),\n customMapping: i18nConfig.getCustomMapping(),\n });\n}\n\nfunction determineLocaleServer({\n defaultLocale,\n locales,\n customMapping,\n}: LocaleResolverConfig) {\n const i18nConfig = getI18nConfig();\n const localeCookieName = i18nConfig.getLocaleCookieName();\n const candidates: string[] = [];\n\n const cookie = getCookie(localeCookieName);\n if (cookie) candidates.push(cookie);\n\n candidates.push(...parseAcceptLanguage(getRequestHeader('accept-language')));\n\n if (candidates.length === 0) {\n console.warn(\n 'gt-tanstack-start(server): no locales could be determined for this request'\n );\n }\n\n const locale = i18nConfig.resolveSupportedLocale(candidates, {\n defaultLocale,\n locales,\n customMapping,\n });\n\n setCookie(localeCookieName, locale, {\n path: '/',\n sameSite: 'lax',\n maxAge: 60 * 60 * 24 * 365,\n });\n\n return locale;\n}\n\nfunction determineLocaleClient({\n defaultLocale,\n locales,\n customMapping,\n}: LocaleResolverConfig) {\n const i18nConfig = getI18nConfig();\n const localeCookieName = i18nConfig.getLocaleCookieName();\n const candidates: string[] = [];\n\n const cookie = getCookieValue(document.cookie, localeCookieName);\n if (cookie) candidates.push(cookie);\n\n if (candidates.length === 0) {\n console.warn(\n 'gt-tanstack-start(client): no locales could be determined for this request'\n );\n }\n\n return i18nConfig.resolveSupportedLocale(candidates, {\n defaultLocale,\n locales,\n customMapping,\n });\n}\n"],"mappings":";;;;;;AAUA,MAAa,kBAAkB,oBAAoB,CAChD,OAAO,sBAAsB,CAC7B,OAAO,sBAAsB;;;;AAKhC,SAAgB,cAAsB;CACpC,MAAM,aAAa,eAAe;AAClC,QAAO,gBAAgB;EACrB,eAAe,WAAW,kBAAkB;EAC5C,SAAS,WAAW,YAAY;EAChC,eAAe,WAAW,kBAAkB;EAC7C,CAAC;;AAGJ,SAAS,sBAAsB,EAC7B,eACA,SACA,iBACuB;CACvB,MAAM,aAAa,eAAe;CAClC,MAAM,mBAAmB,WAAW,qBAAqB;CACzD,MAAM,aAAuB,EAAE;CAE/B,MAAM,SAAS,UAAU,iBAAiB;AAC1C,KAAI,OAAQ,YAAW,KAAK,OAAO;AAEnC,YAAW,KAAK,GAAG,oBAAoB,iBAAiB,kBAAkB,CAAC,CAAC;AAE5E,KAAI,WAAW,WAAW,EACxB,SAAQ,KACN,6EACD;CAGH,MAAM,SAAS,WAAW,uBAAuB,YAAY;EAC3D;EACA;EACA;EACD,CAAC;AAEF,WAAU,kBAAkB,QAAQ;EAClC,MAAM;EACN,UAAU;EACV,QAAQ,OAAU,KAAK;EACxB,CAAC;AAEF,QAAO;;AAGT,SAAS,sBAAsB,EAC7B,eACA,SACA,iBACuB;CACvB,MAAM,aAAa,eAAe;CAClC,MAAM,mBAAmB,WAAW,qBAAqB;CACzD,MAAM,aAAuB,EAAE;CAE/B,MAAM,SAAS,eAAe,SAAS,QAAQ,iBAAiB;AAChE,KAAI,OAAQ,YAAW,KAAK,OAAO;AAEnC,KAAI,WAAW,WAAW,EACxB,SAAQ,KACN,6EACD;AAGH,QAAO,WAAW,uBAAuB,YAAY;EACnD;EACA;EACA;EACD,CAAC"}
|