0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-01-06 22:10:10 -05:00

[ci] format

This commit is contained in:
Arsh 2024-01-24 15:42:42 +00:00 committed by astrobot-houston
parent 267c5aa2c7
commit 919bbe4a89
5 changed files with 84 additions and 25 deletions

View file

@ -1042,11 +1042,10 @@ export const UnhandledRejection = {
export const i18nNotEnabled = {
name: 'i18nNotEnabled',
title: 'i18n Not Enabled',
message: "The `astro:i18n` module can not be used without enabling i18n in your Astro config.",
message: 'The `astro:i18n` module can not be used without enabling i18n in your Astro config.',
hint: 'See https://docs.astro.build/en/guides/internationalization for a guide on setting up i18n.',
} satisfies ErrorData;
/**
* @docs
* @kind heading

View file

@ -91,7 +91,7 @@ interface GetLocalesRelativeUrlList extends GetLocaleOptions {
format: AstroConfig['build']['format'];
routing?: RoutingStrategies;
defaultLocale: string;
};
}
export function getLocaleRelativeUrlList({
base,
@ -124,7 +124,7 @@ export function getLocaleRelativeUrlList({
}
interface GetLocalesAbsoluteUrlList extends GetLocalesRelativeUrlList {
site?: string
site?: string;
}
export function getLocaleAbsoluteUrlList({ site, ...rest }: GetLocalesAbsoluteUrlList) {
@ -245,10 +245,10 @@ function peekCodePathToUse(locales: Locales, locale: string): undefined | string
class Unreachable extends Error {
constructor() {
super(
"Astro encountered an unexpected line of code.\n" +
"In most cases, this is not your fault, but a bug in astro code.\n" +
'Astro encountered an unexpected line of code.\n' +
'In most cases, this is not your fault, but a bug in astro code.\n' +
"If there isn't one already, please create an issue.\n" +
"https://astro.build/issues"
'https://astro.build/issues'
);
}
}

View file

@ -35,10 +35,7 @@ export function createI18nMiddleware(
return async (context, next) => {
const routeData: RouteData | undefined = Reflect.get(context.request, routeDataSymbol);
// If the route we're processing is not a page, then we ignore it
if (
routeData?.type !== 'page' &&
routeData?.type !== 'fallback'
) {
if (routeData?.type !== 'page' && routeData?.type !== 'fallback') {
return await next();
}

View file

@ -11,26 +11,44 @@ type AstroInternationalization = {
settings: AstroSettings;
};
export interface I18nInternalConfig extends Pick<AstroConfig, 'base' | 'site' | 'trailingSlash'>, NonNullable<AstroConfig['i18n']>, Pick<AstroConfig["build"], "format"> {}
export interface I18nInternalConfig
extends Pick<AstroConfig, 'base' | 'site' | 'trailingSlash'>,
NonNullable<AstroConfig['i18n']>,
Pick<AstroConfig['build'], 'format'> {}
export default function astroInternationalization({
settings,
}: AstroInternationalization): vite.Plugin {
const { base, build: { format }, i18n, site, trailingSlash } = settings.config;
const {
base,
build: { format },
i18n,
site,
trailingSlash,
} = settings.config;
return {
name: 'astro:i18n',
enforce: 'pre',
async resolveId(id) {
if (id === virtualModuleId) {
if (i18n === undefined) throw new AstroError(AstroErrorData.i18nNotEnabled);
return this.resolve("astro/virtual-modules/i18n.js");
return this.resolve('astro/virtual-modules/i18n.js');
}
if (id === configId) return resolvedConfigId;
},
load(id) {
if (id === resolvedConfigId) {
const { defaultLocale, locales, routing, fallback } = i18n!;
const config: I18nInternalConfig = { base, format, site, trailingSlash, defaultLocale, locales, routing, fallback };
const config: I18nInternalConfig = {
base,
format,
site,
trailingSlash,
defaultLocale,
locales,
routing,
fallback,
};
return `export default ${JSON.stringify(config)};`;
}
},

View file

@ -1,9 +1,10 @@
import * as I18nInternals from "../i18n/index.js";
import type { I18nInternalConfig } from "../i18n/vite-plugin-i18n.js";
export { normalizeTheLocale, toCodes, toPaths } from "../i18n/index.js";
import * as I18nInternals from '../i18n/index.js';
import type { I18nInternalConfig } from '../i18n/vite-plugin-i18n.js';
export { normalizeTheLocale, toCodes, toPaths } from '../i18n/index.js';
// @ts-expect-error
import config from "astro-internal:i18n-config"
const { trailingSlash, format, site, defaultLocale, locales, routing } = config as I18nInternalConfig;
import config from 'astro-internal:i18n-config';
const { trailingSlash, format, site, defaultLocale, locales, routing } =
config as I18nInternalConfig;
const base = import.meta.env.BASE_URL;
export type GetLocaleOptions = I18nInternals.GetLocaleOptions;
@ -29,7 +30,18 @@ export type GetLocaleOptions = I18nInternals.GetLocaleOptions;
* getRelativeLocaleUrl("es_US", "getting-started", { prependWith: "blog", normalizeLocale: false }); // /blog/es_US/getting-started
* ```
*/
export const getRelativeLocaleUrl = (locale: string, path?: string, options?: GetLocaleOptions) => I18nInternals.getLocaleRelativeUrl({ locale, path, base, trailingSlash, format, defaultLocale, locales, routing, ...options });
export const getRelativeLocaleUrl = (locale: string, path?: string, options?: GetLocaleOptions) =>
I18nInternals.getLocaleRelativeUrl({
locale,
path,
base,
trailingSlash,
format,
defaultLocale,
locales,
routing,
...options,
});
/**
*
@ -56,7 +68,19 @@ export const getRelativeLocaleUrl = (locale: string, path?: string, options?: Ge
* getAbsoluteLocaleUrl("es_US", "getting-started", { prependWith: "blog", normalizeLocale: false }); // https://example.com/blog/es_US/getting-started
* ```
*/
export const getAbsoluteLocaleUrl = (locale: string, path = "", options?: GetLocaleOptions) => I18nInternals.getLocaleAbsoluteUrl({ locale, path, base, trailingSlash, format, site, defaultLocale, locales, routing, ...options });
export const getAbsoluteLocaleUrl = (locale: string, path = '', options?: GetLocaleOptions) =>
I18nInternals.getLocaleAbsoluteUrl({
locale,
path,
base,
trailingSlash,
format,
site,
defaultLocale,
locales,
routing,
...options,
});
/**
* @param path An optional path to add after the `locale`.
@ -64,7 +88,17 @@ export const getAbsoluteLocaleUrl = (locale: string, path = "", options?: GetLoc
*
* Works like `getRelativeLocaleUrl` but it emits the relative URLs for ALL locales:
*/
export const getRelativeLocaleUrlList = (path?: string, options?: GetLocaleOptions) => I18nInternals.getLocaleRelativeUrlList({ base, path, trailingSlash, format, defaultLocale, locales, routing, ...options });
export const getRelativeLocaleUrlList = (path?: string, options?: GetLocaleOptions) =>
I18nInternals.getLocaleRelativeUrlList({
base,
path,
trailingSlash,
format,
defaultLocale,
locales,
routing,
...options,
});
/**
* @param path An optional path to add after the `locale`.
@ -72,7 +106,18 @@ export const getRelativeLocaleUrlList = (path?: string, options?: GetLocaleOptio
*
* Works like `getAbsoluteLocaleUrl` but it emits the absolute URLs for ALL locales:
*/
export const getAbsoluteLocaleUrlList = (path?: string, options?: GetLocaleOptions) => I18nInternals.getLocaleAbsoluteUrlList({ site, base, path, trailingSlash, format, defaultLocale, locales, routing, ...options });
export const getAbsoluteLocaleUrlList = (path?: string, options?: GetLocaleOptions) =>
I18nInternals.getLocaleAbsoluteUrlList({
site,
base,
path,
trailingSlash,
format,
defaultLocale,
locales,
routing,
...options,
});
/**
* A function that return the `path` associated to a locale (defined as code). It's particularly useful in case you decide