mirror of
https://gitlab.com/PronounsPage/PronounsPage.git
synced 2025-09-27 23:13:01 -04:00

the #shared alias used by Nuxt cannot be easily disabled and to prevent breackage with jiti, we make use of it
48 lines
1.7 KiB
TypeScript
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];
|
|
};
|