@better-translate/tanstack-router 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +5 -0
- package/dist/chunk-LRCKROLX.js +194 -0
- package/dist/index.cjs +206 -0
- package/dist/index.d.cts +22 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.js +16 -0
- package/dist/navigation.cjs +400 -0
- package/dist/navigation.d.cts +46 -0
- package/dist/navigation.d.ts +46 -0
- package/dist/navigation.js +232 -0
- package/dist/server.cjs +121 -0
- package/dist/server.d.cts +30 -0
- package/dist/server.d.ts +30 -0
- package/dist/server.js +98 -0
- package/package.json +67 -0
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
import {
|
|
2
|
+
isAbsoluteHref,
|
|
3
|
+
isPathnameInScope,
|
|
4
|
+
localizePathname,
|
|
5
|
+
parseRouteTemplate,
|
|
6
|
+
splitHrefString,
|
|
7
|
+
stripLocaleFromPathname
|
|
8
|
+
} from "./chunk-LRCKROLX.js";
|
|
9
|
+
|
|
10
|
+
// src/navigation.tsx
|
|
11
|
+
import { createElement } from "react";
|
|
12
|
+
function createNavigationFunctions({
|
|
13
|
+
Link: LinkComponent,
|
|
14
|
+
routing,
|
|
15
|
+
useLocation: useInjectedLocation,
|
|
16
|
+
useNavigate: useInjectedNavigate,
|
|
17
|
+
useParams: useInjectedParams,
|
|
18
|
+
useRouter: useInjectedRouter
|
|
19
|
+
}) {
|
|
20
|
+
const parsedTemplate = parseRouteTemplate(routing.routeTemplate);
|
|
21
|
+
function getPathname({
|
|
22
|
+
href,
|
|
23
|
+
locale = routing.defaultLocale
|
|
24
|
+
}) {
|
|
25
|
+
return localizePathname(href, locale, routing);
|
|
26
|
+
}
|
|
27
|
+
const Link = ((rawProps) => {
|
|
28
|
+
const localizedOptions = getLocalizedOptions(
|
|
29
|
+
rawProps,
|
|
30
|
+
useInjectedLocation().pathname,
|
|
31
|
+
resolveActiveLocale(
|
|
32
|
+
useInjectedParams(),
|
|
33
|
+
routing,
|
|
34
|
+
parsedTemplate.localeParamName
|
|
35
|
+
),
|
|
36
|
+
routing,
|
|
37
|
+
parsedTemplate.localeParamName,
|
|
38
|
+
parsedTemplate.localeSegment,
|
|
39
|
+
parsedTemplate.isRequired
|
|
40
|
+
);
|
|
41
|
+
return createElement(
|
|
42
|
+
LinkComponent,
|
|
43
|
+
localizedOptions
|
|
44
|
+
);
|
|
45
|
+
});
|
|
46
|
+
function usePathname() {
|
|
47
|
+
return stripLocaleFromPathname(useInjectedLocation().pathname, routing);
|
|
48
|
+
}
|
|
49
|
+
function useLocale() {
|
|
50
|
+
return resolveActiveLocale(
|
|
51
|
+
useInjectedParams(),
|
|
52
|
+
routing,
|
|
53
|
+
parsedTemplate.localeParamName
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
function useNavigate() {
|
|
57
|
+
const navigate = useInjectedNavigate();
|
|
58
|
+
const pathname = useInjectedLocation().pathname;
|
|
59
|
+
const activeLocale = useLocale();
|
|
60
|
+
return ((options) => navigate(
|
|
61
|
+
getLocalizedOptions(
|
|
62
|
+
options,
|
|
63
|
+
pathname,
|
|
64
|
+
activeLocale,
|
|
65
|
+
routing,
|
|
66
|
+
parsedTemplate.localeParamName,
|
|
67
|
+
parsedTemplate.localeSegment,
|
|
68
|
+
parsedTemplate.isRequired
|
|
69
|
+
)
|
|
70
|
+
));
|
|
71
|
+
}
|
|
72
|
+
function useRouter() {
|
|
73
|
+
const router = useInjectedRouter();
|
|
74
|
+
const pathname = useInjectedLocation().pathname;
|
|
75
|
+
const activeLocale = useLocale();
|
|
76
|
+
return {
|
|
77
|
+
...router,
|
|
78
|
+
buildLocation(options) {
|
|
79
|
+
return router.buildLocation(
|
|
80
|
+
getLocalizedOptions(
|
|
81
|
+
options,
|
|
82
|
+
pathname,
|
|
83
|
+
activeLocale,
|
|
84
|
+
routing,
|
|
85
|
+
parsedTemplate.localeParamName,
|
|
86
|
+
parsedTemplate.localeSegment,
|
|
87
|
+
parsedTemplate.isRequired
|
|
88
|
+
)
|
|
89
|
+
);
|
|
90
|
+
},
|
|
91
|
+
navigate(options) {
|
|
92
|
+
return router.navigate(
|
|
93
|
+
getLocalizedOptions(
|
|
94
|
+
options,
|
|
95
|
+
pathname,
|
|
96
|
+
activeLocale,
|
|
97
|
+
routing,
|
|
98
|
+
parsedTemplate.localeParamName,
|
|
99
|
+
parsedTemplate.localeSegment,
|
|
100
|
+
parsedTemplate.isRequired
|
|
101
|
+
)
|
|
102
|
+
);
|
|
103
|
+
},
|
|
104
|
+
preloadRoute(options) {
|
|
105
|
+
return router.preloadRoute?.(
|
|
106
|
+
getLocalizedOptions(
|
|
107
|
+
options,
|
|
108
|
+
pathname,
|
|
109
|
+
activeLocale,
|
|
110
|
+
routing,
|
|
111
|
+
parsedTemplate.localeParamName,
|
|
112
|
+
parsedTemplate.localeSegment,
|
|
113
|
+
parsedTemplate.isRequired
|
|
114
|
+
)
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
return {
|
|
120
|
+
Link,
|
|
121
|
+
getPathname,
|
|
122
|
+
useLocale,
|
|
123
|
+
useNavigate,
|
|
124
|
+
usePathname,
|
|
125
|
+
useRouter
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
function getLocalizedOptions(options, currentPathname, activeLocale, routing, localeParamName, localeSegment, isRequired) {
|
|
129
|
+
const nextLocale = options.locale ?? activeLocale;
|
|
130
|
+
const localized = {
|
|
131
|
+
...options
|
|
132
|
+
};
|
|
133
|
+
delete localized.locale;
|
|
134
|
+
if (!shouldLocalizeTarget(
|
|
135
|
+
options.to,
|
|
136
|
+
currentPathname,
|
|
137
|
+
routing,
|
|
138
|
+
localeParamName,
|
|
139
|
+
localeSegment
|
|
140
|
+
)) {
|
|
141
|
+
return localized;
|
|
142
|
+
}
|
|
143
|
+
if (typeof options.to === "string" && shouldRewriteTo(options.to) && !pathContainsLocaleParam(options.to, localeParamName)) {
|
|
144
|
+
localized.to = localizePathname(
|
|
145
|
+
options.to,
|
|
146
|
+
nextLocale,
|
|
147
|
+
routing
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
localized.params = localizeParams(
|
|
151
|
+
options.params,
|
|
152
|
+
localeParamName,
|
|
153
|
+
isRequired ? nextLocale : nextLocale === routing.defaultLocale ? void 0 : nextLocale
|
|
154
|
+
);
|
|
155
|
+
return localized;
|
|
156
|
+
}
|
|
157
|
+
function localizeParams(params, localeParamName, locale) {
|
|
158
|
+
if (params === true || params === void 0) {
|
|
159
|
+
return (current) => mergeLocaleParam(current, localeParamName, locale);
|
|
160
|
+
}
|
|
161
|
+
if (typeof params === "function") {
|
|
162
|
+
return (current) => mergeLocaleParam(
|
|
163
|
+
params(
|
|
164
|
+
current
|
|
165
|
+
),
|
|
166
|
+
localeParamName,
|
|
167
|
+
locale
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
if (params && typeof params === "object") {
|
|
171
|
+
return mergeLocaleParam(
|
|
172
|
+
params,
|
|
173
|
+
localeParamName,
|
|
174
|
+
locale
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
return params;
|
|
178
|
+
}
|
|
179
|
+
function mergeLocaleParam(params, localeParamName, locale) {
|
|
180
|
+
const nextParams = {
|
|
181
|
+
...params ?? {}
|
|
182
|
+
};
|
|
183
|
+
if (locale === void 0) {
|
|
184
|
+
delete nextParams[localeParamName];
|
|
185
|
+
return nextParams;
|
|
186
|
+
}
|
|
187
|
+
nextParams[localeParamName] = locale;
|
|
188
|
+
return nextParams;
|
|
189
|
+
}
|
|
190
|
+
function shouldLocalizeTarget(to, currentPathname, routing, localeParamName, localeSegment) {
|
|
191
|
+
if (typeof to !== "string" || to === "" || to === "." || to.startsWith(".")) {
|
|
192
|
+
return isPathnameInScope(currentPathname, routing);
|
|
193
|
+
}
|
|
194
|
+
if (to.startsWith("#") || to.startsWith("?")) {
|
|
195
|
+
return isPathnameInScope(currentPathname, routing);
|
|
196
|
+
}
|
|
197
|
+
if (isAbsoluteHref(to)) {
|
|
198
|
+
return false;
|
|
199
|
+
}
|
|
200
|
+
const { pathname } = splitHrefString(to);
|
|
201
|
+
const normalizedPathname = stripLocalePlaceholder(
|
|
202
|
+
pathname,
|
|
203
|
+
localeParamName,
|
|
204
|
+
localeSegment
|
|
205
|
+
);
|
|
206
|
+
return isPathnameInScope(normalizedPathname, routing);
|
|
207
|
+
}
|
|
208
|
+
function pathContainsLocaleParam(to, localeParamName) {
|
|
209
|
+
return to.split("/").some((s) => s === `$${localeParamName}` || s === `{$${localeParamName}}`);
|
|
210
|
+
}
|
|
211
|
+
function shouldRewriteTo(to) {
|
|
212
|
+
return !to.startsWith(".") && !to.startsWith("#") && !to.startsWith("?") && !isAbsoluteHref(to);
|
|
213
|
+
}
|
|
214
|
+
function stripLocalePlaceholder(pathname, localeParamName, localeSegment) {
|
|
215
|
+
const localeTokens = /* @__PURE__ */ new Set([
|
|
216
|
+
localeSegment,
|
|
217
|
+
`$${localeParamName}`,
|
|
218
|
+
`{$${localeParamName}}`
|
|
219
|
+
]);
|
|
220
|
+
const segments = pathname.split("/").filter(Boolean).filter((segment) => !localeTokens.has(segment));
|
|
221
|
+
return segments.length === 0 ? "/" : `/${segments.join("/")}`;
|
|
222
|
+
}
|
|
223
|
+
function resolveActiveLocale(params, routing, localeParamName) {
|
|
224
|
+
const localeParam = params[localeParamName];
|
|
225
|
+
if (typeof localeParam === "string" && routing.locales.includes(localeParam)) {
|
|
226
|
+
return localeParam;
|
|
227
|
+
}
|
|
228
|
+
return routing.defaultLocale;
|
|
229
|
+
}
|
|
230
|
+
export {
|
|
231
|
+
createNavigationFunctions
|
|
232
|
+
};
|
package/dist/server.cjs
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/server.ts
|
|
21
|
+
var server_exports = {};
|
|
22
|
+
__export(server_exports, {
|
|
23
|
+
createServerHelpers: () => createServerHelpers,
|
|
24
|
+
getRequestConfig: () => getRequestConfig,
|
|
25
|
+
setRequestLocale: () => setRequestLocale
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(server_exports);
|
|
28
|
+
var import_react = require("react");
|
|
29
|
+
var import_server = require("@better-translate/core/server");
|
|
30
|
+
function getRequestConfig(factory) {
|
|
31
|
+
return factory;
|
|
32
|
+
}
|
|
33
|
+
function setRequestLocale(locale) {
|
|
34
|
+
(0, import_server.setRequestLocale)(locale);
|
|
35
|
+
}
|
|
36
|
+
function createServerHelpers(requestConfig) {
|
|
37
|
+
const readRequestConfig = (0, import_react.cache)(async () => {
|
|
38
|
+
const resolved = await requestConfig();
|
|
39
|
+
const translator = resolved.translator;
|
|
40
|
+
return {
|
|
41
|
+
locale: resolved.locale,
|
|
42
|
+
translator
|
|
43
|
+
};
|
|
44
|
+
});
|
|
45
|
+
async function getResolvedLocale(options) {
|
|
46
|
+
const { locale: configLocale, translator } = await readRequestConfig();
|
|
47
|
+
return (0, import_server.resolveRequestLocale)(translator, {
|
|
48
|
+
locale: options?.config?.locale ?? options?.locale,
|
|
49
|
+
requestLocale: (0, import_server.getRequestLocale)(),
|
|
50
|
+
configLocale
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
async function getDirection(options) {
|
|
54
|
+
const [{ translator }, locale] = await Promise.all([
|
|
55
|
+
readRequestConfig(),
|
|
56
|
+
getResolvedLocale(options)
|
|
57
|
+
]);
|
|
58
|
+
return translator.getDirection({
|
|
59
|
+
config: typeof options?.config?.rtl === "boolean" ? {
|
|
60
|
+
rtl: options.config.rtl
|
|
61
|
+
} : void 0,
|
|
62
|
+
locale
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
async function isRtl(options) {
|
|
66
|
+
return await getDirection(options) === "rtl";
|
|
67
|
+
}
|
|
68
|
+
return {
|
|
69
|
+
async getAvailableLanguages() {
|
|
70
|
+
return (await readRequestConfig()).translator.getAvailableLanguages();
|
|
71
|
+
},
|
|
72
|
+
getDirection,
|
|
73
|
+
async getLocale() {
|
|
74
|
+
return getResolvedLocale();
|
|
75
|
+
},
|
|
76
|
+
async getMessages() {
|
|
77
|
+
const [{ translator }, locale] = await Promise.all([
|
|
78
|
+
readRequestConfig(),
|
|
79
|
+
getResolvedLocale()
|
|
80
|
+
]);
|
|
81
|
+
const loadedMessages = await translator.loadLocale(locale);
|
|
82
|
+
const cachedMessages = translator.getMessages()[locale];
|
|
83
|
+
if (loadedMessages) {
|
|
84
|
+
return loadedMessages;
|
|
85
|
+
}
|
|
86
|
+
if (cachedMessages) {
|
|
87
|
+
return cachedMessages;
|
|
88
|
+
}
|
|
89
|
+
throw new Error(
|
|
90
|
+
`Locale "${locale}" messages are not available. Preload them or register a loader in your Better Translate core config.`
|
|
91
|
+
);
|
|
92
|
+
},
|
|
93
|
+
isRtl,
|
|
94
|
+
async getTranslations(options) {
|
|
95
|
+
const [{ translator }, locale] = await Promise.all([
|
|
96
|
+
readRequestConfig(),
|
|
97
|
+
getResolvedLocale(options)
|
|
98
|
+
]);
|
|
99
|
+
await translator.loadLocale(locale);
|
|
100
|
+
return ((...args) => {
|
|
101
|
+
const [key, translateOptions] = args;
|
|
102
|
+
return translator.t(
|
|
103
|
+
key,
|
|
104
|
+
{
|
|
105
|
+
...translateOptions ?? {},
|
|
106
|
+
locale
|
|
107
|
+
}
|
|
108
|
+
);
|
|
109
|
+
});
|
|
110
|
+
},
|
|
111
|
+
async getTranslator() {
|
|
112
|
+
return (await readRequestConfig()).translator;
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
117
|
+
0 && (module.exports = {
|
|
118
|
+
createServerHelpers,
|
|
119
|
+
getRequestConfig,
|
|
120
|
+
setRequestLocale
|
|
121
|
+
});
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { ConfiguredTranslator, TranslationMessages, TranslationLanguageMetadata, TranslationConfig, TranslationDirection, DeepPartialMessages } from '@better-translate/core';
|
|
2
|
+
|
|
3
|
+
type AnyTranslator = ConfiguredTranslator<any, TranslationMessages>;
|
|
4
|
+
type InferTranslatorLocale<TTranslator> = TTranslator extends ConfiguredTranslator<infer TLocale, TranslationMessages> ? TLocale : never;
|
|
5
|
+
type InferTranslatorMessages<TTranslator> = TTranslator extends ConfiguredTranslator<string, infer TMessages> ? TMessages : TranslationMessages;
|
|
6
|
+
interface BetterTranslateRequestConfig<TTranslator extends AnyTranslator, TLocale extends InferTranslatorLocale<TTranslator> = InferTranslatorLocale<TTranslator>> {
|
|
7
|
+
locale?: TLocale;
|
|
8
|
+
translator: TTranslator;
|
|
9
|
+
}
|
|
10
|
+
type RequestConfigFactory<TTranslator extends AnyTranslator, TLocale extends InferTranslatorLocale<TTranslator> = InferTranslatorLocale<TTranslator>> = () => BetterTranslateRequestConfig<TTranslator, TLocale> | Promise<BetterTranslateRequestConfig<TTranslator, TLocale>>;
|
|
11
|
+
type RequestDirectionOptions<TLocale extends string> = {
|
|
12
|
+
locale?: TLocale;
|
|
13
|
+
config?: TranslationConfig<TLocale>;
|
|
14
|
+
};
|
|
15
|
+
interface ServerHelpers<TRequestLocale extends string, TTranslator extends AnyTranslator> {
|
|
16
|
+
getAvailableLanguages(): Promise<readonly TranslationLanguageMetadata<InferTranslatorLocale<TTranslator>>[]>;
|
|
17
|
+
getDirection(options?: RequestDirectionOptions<InferTranslatorLocale<TTranslator>>): Promise<TranslationDirection>;
|
|
18
|
+
getLocale(): Promise<TRequestLocale>;
|
|
19
|
+
getMessages(): Promise<DeepPartialMessages<InferTranslatorMessages<TTranslator>> | InferTranslatorMessages<TTranslator>>;
|
|
20
|
+
isRtl(options?: RequestDirectionOptions<InferTranslatorLocale<TTranslator>>): Promise<boolean>;
|
|
21
|
+
getTranslations(options?: {
|
|
22
|
+
locale?: InferTranslatorLocale<TTranslator>;
|
|
23
|
+
}): Promise<TTranslator["t"]>;
|
|
24
|
+
getTranslator(): Promise<TTranslator>;
|
|
25
|
+
}
|
|
26
|
+
declare function getRequestConfig<TTranslator extends AnyTranslator, TLocale extends InferTranslatorLocale<TTranslator> = InferTranslatorLocale<TTranslator>>(factory: RequestConfigFactory<TTranslator, TLocale>): RequestConfigFactory<TTranslator, TLocale>;
|
|
27
|
+
declare function setRequestLocale<TLocale extends string>(locale: TLocale | undefined): void;
|
|
28
|
+
declare function createServerHelpers<TTranslator extends AnyTranslator, TLocale extends InferTranslatorLocale<TTranslator> = InferTranslatorLocale<TTranslator>>(requestConfig: RequestConfigFactory<TTranslator, TLocale>): ServerHelpers<TLocale, TTranslator>;
|
|
29
|
+
|
|
30
|
+
export { type BetterTranslateRequestConfig, type RequestConfigFactory, type ServerHelpers, createServerHelpers, getRequestConfig, setRequestLocale };
|
package/dist/server.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { ConfiguredTranslator, TranslationMessages, TranslationLanguageMetadata, TranslationConfig, TranslationDirection, DeepPartialMessages } from '@better-translate/core';
|
|
2
|
+
|
|
3
|
+
type AnyTranslator = ConfiguredTranslator<any, TranslationMessages>;
|
|
4
|
+
type InferTranslatorLocale<TTranslator> = TTranslator extends ConfiguredTranslator<infer TLocale, TranslationMessages> ? TLocale : never;
|
|
5
|
+
type InferTranslatorMessages<TTranslator> = TTranslator extends ConfiguredTranslator<string, infer TMessages> ? TMessages : TranslationMessages;
|
|
6
|
+
interface BetterTranslateRequestConfig<TTranslator extends AnyTranslator, TLocale extends InferTranslatorLocale<TTranslator> = InferTranslatorLocale<TTranslator>> {
|
|
7
|
+
locale?: TLocale;
|
|
8
|
+
translator: TTranslator;
|
|
9
|
+
}
|
|
10
|
+
type RequestConfigFactory<TTranslator extends AnyTranslator, TLocale extends InferTranslatorLocale<TTranslator> = InferTranslatorLocale<TTranslator>> = () => BetterTranslateRequestConfig<TTranslator, TLocale> | Promise<BetterTranslateRequestConfig<TTranslator, TLocale>>;
|
|
11
|
+
type RequestDirectionOptions<TLocale extends string> = {
|
|
12
|
+
locale?: TLocale;
|
|
13
|
+
config?: TranslationConfig<TLocale>;
|
|
14
|
+
};
|
|
15
|
+
interface ServerHelpers<TRequestLocale extends string, TTranslator extends AnyTranslator> {
|
|
16
|
+
getAvailableLanguages(): Promise<readonly TranslationLanguageMetadata<InferTranslatorLocale<TTranslator>>[]>;
|
|
17
|
+
getDirection(options?: RequestDirectionOptions<InferTranslatorLocale<TTranslator>>): Promise<TranslationDirection>;
|
|
18
|
+
getLocale(): Promise<TRequestLocale>;
|
|
19
|
+
getMessages(): Promise<DeepPartialMessages<InferTranslatorMessages<TTranslator>> | InferTranslatorMessages<TTranslator>>;
|
|
20
|
+
isRtl(options?: RequestDirectionOptions<InferTranslatorLocale<TTranslator>>): Promise<boolean>;
|
|
21
|
+
getTranslations(options?: {
|
|
22
|
+
locale?: InferTranslatorLocale<TTranslator>;
|
|
23
|
+
}): Promise<TTranslator["t"]>;
|
|
24
|
+
getTranslator(): Promise<TTranslator>;
|
|
25
|
+
}
|
|
26
|
+
declare function getRequestConfig<TTranslator extends AnyTranslator, TLocale extends InferTranslatorLocale<TTranslator> = InferTranslatorLocale<TTranslator>>(factory: RequestConfigFactory<TTranslator, TLocale>): RequestConfigFactory<TTranslator, TLocale>;
|
|
27
|
+
declare function setRequestLocale<TLocale extends string>(locale: TLocale | undefined): void;
|
|
28
|
+
declare function createServerHelpers<TTranslator extends AnyTranslator, TLocale extends InferTranslatorLocale<TTranslator> = InferTranslatorLocale<TTranslator>>(requestConfig: RequestConfigFactory<TTranslator, TLocale>): ServerHelpers<TLocale, TTranslator>;
|
|
29
|
+
|
|
30
|
+
export { type BetterTranslateRequestConfig, type RequestConfigFactory, type ServerHelpers, createServerHelpers, getRequestConfig, setRequestLocale };
|
package/dist/server.js
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
// src/server.ts
|
|
2
|
+
import { cache } from "react";
|
|
3
|
+
import {
|
|
4
|
+
getRequestLocale as getStoredRequestLocale,
|
|
5
|
+
resolveRequestLocale,
|
|
6
|
+
setRequestLocale as setStoredRequestLocale
|
|
7
|
+
} from "@better-translate/core/server";
|
|
8
|
+
function getRequestConfig(factory) {
|
|
9
|
+
return factory;
|
|
10
|
+
}
|
|
11
|
+
function setRequestLocale(locale) {
|
|
12
|
+
setStoredRequestLocale(locale);
|
|
13
|
+
}
|
|
14
|
+
function createServerHelpers(requestConfig) {
|
|
15
|
+
const readRequestConfig = cache(async () => {
|
|
16
|
+
const resolved = await requestConfig();
|
|
17
|
+
const translator = resolved.translator;
|
|
18
|
+
return {
|
|
19
|
+
locale: resolved.locale,
|
|
20
|
+
translator
|
|
21
|
+
};
|
|
22
|
+
});
|
|
23
|
+
async function getResolvedLocale(options) {
|
|
24
|
+
const { locale: configLocale, translator } = await readRequestConfig();
|
|
25
|
+
return resolveRequestLocale(translator, {
|
|
26
|
+
locale: options?.config?.locale ?? options?.locale,
|
|
27
|
+
requestLocale: getStoredRequestLocale(),
|
|
28
|
+
configLocale
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
async function getDirection(options) {
|
|
32
|
+
const [{ translator }, locale] = await Promise.all([
|
|
33
|
+
readRequestConfig(),
|
|
34
|
+
getResolvedLocale(options)
|
|
35
|
+
]);
|
|
36
|
+
return translator.getDirection({
|
|
37
|
+
config: typeof options?.config?.rtl === "boolean" ? {
|
|
38
|
+
rtl: options.config.rtl
|
|
39
|
+
} : void 0,
|
|
40
|
+
locale
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
async function isRtl(options) {
|
|
44
|
+
return await getDirection(options) === "rtl";
|
|
45
|
+
}
|
|
46
|
+
return {
|
|
47
|
+
async getAvailableLanguages() {
|
|
48
|
+
return (await readRequestConfig()).translator.getAvailableLanguages();
|
|
49
|
+
},
|
|
50
|
+
getDirection,
|
|
51
|
+
async getLocale() {
|
|
52
|
+
return getResolvedLocale();
|
|
53
|
+
},
|
|
54
|
+
async getMessages() {
|
|
55
|
+
const [{ translator }, locale] = await Promise.all([
|
|
56
|
+
readRequestConfig(),
|
|
57
|
+
getResolvedLocale()
|
|
58
|
+
]);
|
|
59
|
+
const loadedMessages = await translator.loadLocale(locale);
|
|
60
|
+
const cachedMessages = translator.getMessages()[locale];
|
|
61
|
+
if (loadedMessages) {
|
|
62
|
+
return loadedMessages;
|
|
63
|
+
}
|
|
64
|
+
if (cachedMessages) {
|
|
65
|
+
return cachedMessages;
|
|
66
|
+
}
|
|
67
|
+
throw new Error(
|
|
68
|
+
`Locale "${locale}" messages are not available. Preload them or register a loader in your Better Translate core config.`
|
|
69
|
+
);
|
|
70
|
+
},
|
|
71
|
+
isRtl,
|
|
72
|
+
async getTranslations(options) {
|
|
73
|
+
const [{ translator }, locale] = await Promise.all([
|
|
74
|
+
readRequestConfig(),
|
|
75
|
+
getResolvedLocale(options)
|
|
76
|
+
]);
|
|
77
|
+
await translator.loadLocale(locale);
|
|
78
|
+
return ((...args) => {
|
|
79
|
+
const [key, translateOptions] = args;
|
|
80
|
+
return translator.t(
|
|
81
|
+
key,
|
|
82
|
+
{
|
|
83
|
+
...translateOptions ?? {},
|
|
84
|
+
locale
|
|
85
|
+
}
|
|
86
|
+
);
|
|
87
|
+
});
|
|
88
|
+
},
|
|
89
|
+
async getTranslator() {
|
|
90
|
+
return (await readRequestConfig()).translator;
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
export {
|
|
95
|
+
createServerHelpers,
|
|
96
|
+
getRequestConfig,
|
|
97
|
+
setRequestLocale
|
|
98
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@better-translate/tanstack-router",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "TanStack Router integration package for Better Translate.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"main": "./dist/index.cjs",
|
|
9
|
+
"module": "./dist/index.js",
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"import": "./dist/index.js",
|
|
15
|
+
"require": "./dist/index.cjs"
|
|
16
|
+
},
|
|
17
|
+
"./navigation": {
|
|
18
|
+
"types": "./dist/navigation.d.ts",
|
|
19
|
+
"import": "./dist/navigation.js",
|
|
20
|
+
"require": "./dist/navigation.cjs"
|
|
21
|
+
},
|
|
22
|
+
"./server": {
|
|
23
|
+
"types": "./dist/server.d.ts",
|
|
24
|
+
"import": "./dist/server.js",
|
|
25
|
+
"require": "./dist/server.cjs"
|
|
26
|
+
},
|
|
27
|
+
"./package.json": "./package.json"
|
|
28
|
+
},
|
|
29
|
+
"files": ["dist", "LICENSE", "README.md"],
|
|
30
|
+
"scripts": {
|
|
31
|
+
"build": "tsup src/index.ts src/navigation.tsx src/server.ts --format esm,cjs --dts --clean",
|
|
32
|
+
"check-types": "tsc --noEmit",
|
|
33
|
+
"test": "bun test tests/runtime",
|
|
34
|
+
"test:types": "tsc -p tests/types/tsconfig.json --noEmit"
|
|
35
|
+
},
|
|
36
|
+
"publishConfig": {
|
|
37
|
+
"access": "public"
|
|
38
|
+
},
|
|
39
|
+
"repository": {
|
|
40
|
+
"type": "git",
|
|
41
|
+
"url": "git+https://github.com/jralvarenga/better-translate.git",
|
|
42
|
+
"directory": "packages/tanstack-router"
|
|
43
|
+
},
|
|
44
|
+
"homepage": "https://github.com/jralvarenga/better-translate/tree/main/packages/tanstack-router#readme",
|
|
45
|
+
"bugs": {
|
|
46
|
+
"url": "https://github.com/jralvarenga/better-translate/issues"
|
|
47
|
+
},
|
|
48
|
+
"dependencies": {
|
|
49
|
+
"@better-translate/core": "workspace:^"
|
|
50
|
+
},
|
|
51
|
+
"peerDependencies": {
|
|
52
|
+
"@tanstack/react-router": "^1.167.3",
|
|
53
|
+
"react": "^19.0.0",
|
|
54
|
+
"react-dom": "^19.0.0"
|
|
55
|
+
},
|
|
56
|
+
"devDependencies": {
|
|
57
|
+
"@repo/typescript-config": "*"
|
|
58
|
+
},
|
|
59
|
+
"keywords": [
|
|
60
|
+
"i18n",
|
|
61
|
+
"l10n",
|
|
62
|
+
"tanstack-router",
|
|
63
|
+
"tanstack-start",
|
|
64
|
+
"translations",
|
|
65
|
+
"typescript"
|
|
66
|
+
]
|
|
67
|
+
}
|