81 lines
2.4 KiB
TypeScript
81 lines
2.4 KiB
TypeScript
/* ============================================================================
|
|||
|
|
LOCALISED PATHS
|
||
|
|
----------------------------------------------------------------------------
|
||
|
|
Thin, typed wrappers over the pure helpers the component library already
|
||
|
|
ships, plus the section-level helpers the route modules need. Pure and
|
||
|
|
import-light: safe in a loader, in a `meta` function and during SSR.
|
||
|
|
========================================================================= */
|
||
|
|
|
||
|
|
import {
|
||
|
|
buildLocalizedHref,
|
||
|
|
getCounterpartLocale,
|
||
|
|
getLocaleFromPathname,
|
||
|
|
isCurrentPath,
|
||
|
|
localizePath,
|
||
|
|
stripLocale,
|
||
|
|
type CounterpartPathOptions,
|
||
|
|
type Locale,
|
||
|
|
} from "../components/ui/LanguageSwitcher/locale-path";
|
||
|
|
import {
|
||
|
|
ROUTE_SECTION_GROUPS,
|
||
|
|
ROUTE_SLUGS,
|
||
|
|
ROUTE_SLUG_KEYS,
|
||
|
|
sectionSlugPath,
|
||
|
|
type RouteSectionGroupId,
|
||
|
|
type RouteSlugKey,
|
||
|
|
} from "../lib/route-slugs";
|
||
|
|
|
||
|
|
export {
|
||
|
|
buildLocalizedHref,
|
||
|
|
getCounterpartLocale,
|
||
|
|
getLocaleFromPathname,
|
||
|
|
isCurrentPath,
|
||
|
|
ROUTE_SECTION_GROUPS,
|
||
|
|
ROUTE_SLUGS,
|
||
|
|
ROUTE_SLUG_KEYS,
|
||
|
|
sectionSlugPath,
|
||
|
|
stripLocale,
|
||
|
|
};
|
||
|
|
export type { CounterpartPathOptions, RouteSectionGroupId, RouteSlugKey };
|
||
|
|
|
||
|
|
/**
|
||
|
|
* The locale-prefixed URL for a path. Accepts an already-prefixed path, an
|
||
|
|
* unprefixed one, or a bare "/".
|
||
|
|
* ("/contact", "ar") -> "/ar/contact" ("/en/contact", "ar") -> "/ar/contact"
|
||
|
|
*/
|
||
|
|
export function localizedPath(path: string, locale: Locale): string {
|
||
|
|
return localizePath(path, locale);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* The same page in the other language, query string and fragment preserved.
|
||
|
|
* This is what the header's language switch links to.
|
||
|
|
* ("/en/news-and-updates?page=2", "ar") -> "/ar/news-and-updates?page=2"
|
||
|
|
*/
|
||
|
|
export function alternatePath(
|
||
|
|
pathname: string,
|
||
|
|
targetLocale: Locale,
|
||
|
|
options: CounterpartPathOptions = {},
|
||
|
|
): string {
|
||
|
|
return buildLocalizedHref(pathname, targetLocale, options);
|
||
|
|
}
|
||
|
|
|
||
|
|
/** The locale-prefixed URL of one of the ten sections. */
|
||
|
|
export function sectionPath(key: RouteSlugKey, locale: Locale): string {
|
||
|
|
return localizePath(sectionSlugPath(key), locale);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Both language URLs for the page currently at `pathname`, in the order
|
||
|
|
* `SUPPORTED_LOCALES` declares. Used to emit hreflang alternates.
|
||
|
|
*/
|
||
|
|
export function localeAlternates(
|
||
|
|
pathname: string,
|
||
|
|
options: CounterpartPathOptions = {},
|
||
|
|
): { locale: Locale; path: string }[] {
|
||
|
|
return (["en", "ar"] as const).map((locale) => ({
|
||
|
|
locale,
|
||
|
|
path: buildLocalizedHref(pathname, locale, options),
|
||
|
|
}));
|
||
|
|
}
|