Files
Rental-operations-platform/contracts/phase9/implementation/hreflang-generator_v1.0.ts
T

22 lines
799 B
TypeScript

import manifest from '../localization/ROUTE_MANIFEST_v1.0.json';
type Locale = 'en' | 'fr' | 'ar';
export function localizedPath(routeId: string, locale: Locale): string {
const route = manifest.routes.find((item) => item.id === routeId);
if (!route) throw new Error(`Unknown route: ${routeId}`);
const slug = route.slugs[locale];
return slug ? `/${locale}/${slug}` : `/${locale}`;
}
export function hreflangLinks(origin: string, routeId: string) {
const alternates = manifest.locales.map((locale) => ({
rel: 'alternate',
hreflang: locale,
href: new URL(localizedPath(routeId, locale as Locale), origin).href,
}));
alternates.push({
rel: 'alternate',
hreflang: 'x-default',
href: new URL(localizedPath(routeId, 'en'), origin).href,
});
return alternates;
}