@inlang/paraglide-js
Version:
[](https://inlang.com)
85 lines • 3.01 kB
TypeScript
/**
* Lower-level URL localization function, primarily used in server contexts.
*
* This function is designed for server-side usage where you need precise control
* over URL localization, such as in middleware or request handlers. It works with
* URL objects and always returns absolute URLs.
*
* For client-side UI components, use `localizeHref()` instead, which provides
* a more convenient API with relative paths and automatic locale detection.
*
* @example
* ```typescript
* // Server middleware example
* app.use((req, res, next) => {
* const url = new URL(req.url, `${req.protocol}://${req.headers.host}`);
* const localized = localizeUrl(url, { locale: "de" });
*
* if (localized.href !== url.href) {
* return res.redirect(localized.href);
* }
* next();
* });
* ```
*
* @example
* ```typescript
* // Using with URL patterns
* const url = new URL("https://example.com/about");
* localizeUrl(url, { locale: "de" });
* // => URL("https://example.com/de/about")
*
* // Using with domain-based localization
* const url = new URL("https://example.com/store");
* localizeUrl(url, { locale: "de" });
* // => URL("https://de.example.com/store")
* ```
*
* @param {string | URL} url - The URL to localize. If string, must be absolute.
* @param {Object} [options] - Options for localization
* @param {string} [options.locale] - Target locale. If not provided, uses getLocale()
* @returns {URL} The localized URL, always absolute
*/
export function localizeUrl(url: string | URL, options?: {
locale?: string | undefined;
}): URL;
/**
* Low-level URL de-localization function, primarily used in server contexts.
*
* This function is designed for server-side usage where you need precise control
* over URL de-localization, such as in middleware or request handlers. It works with
* URL objects and always returns absolute URLs.
*
* For client-side UI components, use `deLocalizeHref()` instead, which provides
* a more convenient API with relative paths.
*
* @example
* ```typescript
* // Server middleware example
* app.use((req, res, next) => {
* const url = new URL(req.url, `${req.protocol}://${req.headers.host}`);
* const baseUrl = deLocalizeUrl(url);
*
* // Store the base URL for later use
* req.baseUrl = baseUrl;
* next();
* });
* ```
*
* @example
* ```typescript
* // Using with URL patterns
* const url = new URL("https://example.com/de/about");
* deLocalizeUrl(url); // => URL("https://example.com/about")
*
* // Using with domain-based localization
* const url = new URL("https://de.example.com/store");
* deLocalizeUrl(url); // => URL("https://example.com/store")
* ```
*
* @param {string | URL} url - The URL to de-localize. If string, must be absolute.
* @returns {URL} The de-localized URL, always absolute
*/
export function deLocalizeUrl(url: string | URL): URL;
export function aggregateGroups(match: any): Record<string, string | null | undefined>;
//# sourceMappingURL=localize-url.d.ts.map