PronounsPage/shared/domain.ts
Valentyne Stigloher 10180aa6a3 (refactor) use #shared alias instead of ~~/shared
the #shared alias used by Nuxt cannot be easily disabled and to prevent breackage with jiti, we make use of it
2025-08-17 18:56:02 +02:00

48 lines
1.7 KiB
TypeScript

import buildLocaleList from '#shared/buildLocaleList.ts';
import localeDescriptions from '~~/locale/locales.ts';
export const getLocaleUrls = (domainBase: string | undefined): Record<string, string> => {
if (domainBase) {
return Object.fromEntries([
['_', domainBase.replace('{locale}', '')
.replace('/.', '/')
.replace('-.', '.')],
['no', domainBase.replace('{locale}', 'no')],
...localeDescriptions.map((localeDescription) => {
return [localeDescription.code, domainBase.replace('{locale}', localeDescription.code)];
}),
]);
}
return Object.fromEntries([
['_', 'https://pronouns.page'],
['no', 'https://no.pronouns.page'],
...localeDescriptions.map((localeDescription) => {
return [localeDescription.code, localeDescription.url];
}),
]);
};
export const getUrlForLocaleBase = (locale: string, domainBase: string) => {
return getLocaleUrls(domainBase)[locale];
};
export const getUrlsForAllLocalesBase = (
locale: string,
includeUnpublished: boolean = false,
domainBase: string,
) => {
const locales = buildLocaleList(locale, includeUnpublished);
const urls = getLocaleUrls(domainBase);
const codes = ['_', ...Object.values(locales).map((localeDescription) => localeDescription.code)];
return codes.map((code) => urls[code]);
};
export const getLocaleForUrlBase = (url: URL, domainBase: string): string | undefined => {
const match = Object.entries(getLocaleUrls(domainBase)).find(([_, origin]) => url.origin === origin);
if (!match) {
return undefined;
}
return match[0];
};