Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(i18n): virtual module APIs #8878

Merged
merged 2 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions packages/astro/client.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,12 @@ declare module 'astro:i18n' {
type I18nModule = typeof import('./dist/i18n/index.js');

// TODO: documentation
export const getI18nBaseUrl: (locale: string) => string;
export const getLocaleRelativeUrl: (locale: string) => string;
export const getLocaleAbsoluteUrl: (locale: string) => string;

// TODO: documentation
export const getLocalesBaseUrl: () => string[];
export const getLocaleRelativeUrlList: () => string[];
export const getLocaleAbsoluteUrlList: () => string[];
}

declare module 'astro:middleware' {
Expand Down
45 changes: 42 additions & 3 deletions packages/astro/src/i18n/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,29 @@ import { AstroError } from '../core/errors/index.js';
import { MissingLocale } from '../core/errors/errors-data.js';
import { shouldAppendForwardSlash } from '../core/build/util.js';
import type { AstroConfig } from '../@types/astro.js';
import { joinPaths } from '@astrojs/internal-helpers/path';

type GetI18nBaseUrl = {
type GetLocaleRelativeUrl = {
locale: string;
base: string;
locales: string[];
trailingSlash: AstroConfig['trailingSlash'];
format: AstroConfig['build']['format'];
};

type GetLocaleAbsoluteUrl = GetLocaleRelativeUrl & {
site: AstroConfig['site'];
};
/**
* The base URL
*/
export function getI18nBaseUrl({ locale, base, locales, trailingSlash, format }: GetI18nBaseUrl) {
export function getLocaleRelativeUrl({
locale,
base,
locales,
trailingSlash,
format,
}: GetLocaleRelativeUrl) {
if (!locales.includes(locale)) {
throw new AstroError({
...MissingLocale,
Expand All @@ -29,14 +40,31 @@ export function getI18nBaseUrl({ locale, base, locales, trailingSlash, format }:
}
}

/**
* The absolute URL
*/
export function getLocaleAbsoluteUrl({ site, ...rest }: GetLocaleAbsoluteUrl) {
const locale = getLocaleRelativeUrl(rest);
if (site) {
return joinPaths(site, locale);
} else {
return locale;
}
}

type GetLocalesBaseUrl = {
base: string;
locales: string[];
trailingSlash: AstroConfig['trailingSlash'];
format: AstroConfig['build']['format'];
};

export function getLocalesBaseUrl({ base, locales, trailingSlash, format }: GetLocalesBaseUrl) {
export function getLocaleRelativeUrlList({
base,
locales,
trailingSlash,
format,
}: GetLocalesBaseUrl) {
return locales.map((locale) => {
const normalizedLocale = normalizeLocale(locale);
if (shouldAppendForwardSlash(trailingSlash, format)) {
Expand All @@ -47,6 +75,17 @@ export function getLocalesBaseUrl({ base, locales, trailingSlash, format }: GetL
});
}

export function getLocaleAbsoluteUrlList({ site, ...rest }: GetLocaleAbsoluteUrl) {
const locales = getLocaleRelativeUrlList(rest);
return locales.map((locale) => {
if (site) {
return joinPaths(site, locale);
} else {
return locale;
}
});
}

/**
*
* Given a locale, this function:
Expand Down
15 changes: 12 additions & 3 deletions packages/astro/src/i18n/vite-plugin-i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,26 @@ export default function astroInternalization({ settings }: AstroInternalization)
load(id) {
if (id === resolvedVirtualModuleId) {
return `
import { getI18nBaseUrl as getI18nBaseUrlInternal, getLocalesBaseUrl as _getLocalesBaseUrl } from "astro/i18n";
import {
getLocaleRelativeUrl as _getLocaleRelativeUrl,
getLocaleRelativeUrlList as _getLocaleRelativeUrlList,
getLocaleAbsoluteUrl as _getLocaleAbsoluteUrl,
getLocaleAbsoluteUrlList as _getLocaleAbsoluteUrlList,

} from "astro/i18n";

const defaultLocale = ${JSON.stringify(settings.config.experimental.i18n!.defaultLocale)};
const locales = ${JSON.stringify(settings.config.experimental.i18n!.locales)};
const fallback = ${JSON.stringify(settings.config.experimental.i18n!.fallback)};
const base = ${JSON.stringify(settings.config.base)};
const trailingSlash = ${JSON.stringify(settings.config.trailingSlash)};
const format = ${JSON.stringify(settings.config.build.format)};
const site = ${JSON.stringify(settings.config.site)};

export const getI18nBaseUrl = (locale) => getI18nBaseUrlInternal({ locale, base, locales, trailingSlash, format });
export const getLocalesBaseUrl = () => _getLocalesBaseUrl({ base, locales, trailingSlash, format });
export const getLocaleRelativeUrl = (locale) => _getLocaleRelativeUrl({ locale, base, locales, trailingSlash, format });
export const getLocaleRelativeUrlList = () => _getLocaleRelativeUrlList({ base, locales, trailingSlash, format });
export const getLocaleAbsoluteUrl = (locale) => _getLocaleAbsoluteUrl({ locale, base, locales, trailingSlash, format, site });
export const getLocaleAbsoluteUrlList = () => _getLocaleAbsoluteUrlList({ base, locales, trailingSlash, format, site });
`;
}
},
Expand Down
Loading
Loading