Skip to content

Commit

Permalink
feat(i18n): rename APIs and add new ones
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico committed Oct 20, 2023
1 parent 81abf24 commit b019067
Show file tree
Hide file tree
Showing 5 changed files with 526 additions and 33 deletions.
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
52 changes: 49 additions & 3 deletions packages/astro/src/i18n/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,27 @@ import { MissingLocale } from '../core/errors/errors-data.js';
import { shouldAppendForwardSlash } from '../core/build/util.js';
import type { AstroConfig } from '../@types/astro.js';

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 +39,35 @@ export function getI18nBaseUrl({ locale, base, locales, trailingSlash, format }:
}
}

/**
* The absolute URL
*/
export function getLocaleAbsoluteUrl({ site, ...rest }: GetLocaleAbsoluteUrl) {
const locale = getLocaleRelativeUrl(rest);
if (site) {
if (site.endsWith('/') || locale.startsWith('/')) {
return `${site}${locale}`;
} else {
return `${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 +78,21 @@ export function getLocalesBaseUrl({ base, locales, trailingSlash, format }: GetL
});
}

export function getLocaleAbsoluteUrlList({ site, ...rest }: GetLocaleAbsoluteUrl) {
const locales = getLocaleRelativeUrlList(rest);
return locales.map((locale) => {
if (site) {
if (site.endsWith('/') || locale.startsWith('/')) {
return `${site}${locale}`;
} else {
return `${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

0 comments on commit b019067

Please sign in to comment.