This commit is contained in:
2026-07-31 23:21:51 +02:00
parent f6b4b36df8
commit f14f2cfd98
294 changed files with 5880 additions and 0 deletions
+71
View File
@@ -0,0 +1,71 @@
import { isValidLocale } from './helpers.js';
/**
* Builds a locale-prefixed path for a document in a target locale.
*
* Always prefixes the locale: /{locale} or /{locale}/{slug}. The home slug
* collapses to the locale root. Returns undefined if the document has no slug
* in the target locale (caller decides fallback behavior).
*
* @example
* buildLocalizedPath({ slugs: { pl: 'strona-glowna', en: 'home' }, locale: 'en', config })
* // → '/en' (home slug collapses to root)
*
* buildLocalizedPath({ slugs: { pl: 'o-nas', en: 'about' }, locale: 'en', config })
* // → '/en/about'
*
* buildLocalizedPath({
* slugs: { pl: 'moj-post', en: 'my-post' },
* prefix: { pl: 'artykuly', en: 'articles' },
* locale: 'en',
* config,
* })
* // → '/en/articles/my-post'
*/ export function buildLocalizedPath({ config, homeSlug = 'home', locale, prefix, slugs }) {
if (!isValidLocale(locale, config)) {
return undefined;
}
const slug = slugs[locale];
if (!slug) {
return undefined;
}
if (prefix) {
const segment = prefix[locale];
// No archive slug in this locale means the entry is unreachable there —
// there's no path to point at, so hreflang should omit it rather than
// invent /en/moj-post.
if (!segment) {
return undefined;
}
return `/${locale}/${segment}/${slug}`;
}
if (slug === homeSlug) {
return `/${locale}`;
}
return `/${locale}/${slug}`;
}
/**
* Resolves the equivalent path for the same document in a different locale —
* the language-switcher use case (/pl/strona-glowna → /en/home).
*
* Never dead-ends on a 404. When the document has no slug in the target locale,
* falls back to the archive it belongs to (/en/articles) if there is one, and
* to the locale root otherwise — the closest place the visitor would want.
*/ export function switchLocalePath({ config, homeSlug = 'home', prefix, slugs, targetLocale }) {
const path = buildLocalizedPath({
config,
homeSlug,
locale: targetLocale,
prefix,
slugs
});
if (path) {
return path;
}
const archiveSegment = prefix?.[targetLocale];
if (archiveSegment) {
return `/${targetLocale}/${archiveSegment}`;
}
return `/${targetLocale}`;
}
//# sourceMappingURL=localizedPath.js.map