Skip to content

Commit

Permalink
[ci] format
Browse files Browse the repository at this point in the history
  • Loading branch information
lilnasy authored and astrobot-houston committed Jan 24, 2024
1 parent 267c5aa commit 919bbe4
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 25 deletions.
3 changes: 1 addition & 2 deletions packages/astro/src/core/errors/errors-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions packages/astro/src/i18n/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ interface GetLocalesRelativeUrlList extends GetLocaleOptions {
format: AstroConfig['build']['format'];
routing?: RoutingStrategies;
defaultLocale: string;
};
}

export function getLocaleRelativeUrlList({
base,
Expand Down Expand Up @@ -124,7 +124,7 @@ export function getLocaleRelativeUrlList({
}

interface GetLocalesAbsoluteUrlList extends GetLocalesRelativeUrlList {
site?: string
site?: string;
}

export function getLocaleAbsoluteUrlList({ site, ...rest }: GetLocalesAbsoluteUrlList) {
Expand Down Expand Up @@ -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" +
"If there isn't one already, please create an issue.\n" +
"https://astro.build/issues"
'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'
);
}
}
5 changes: 1 addition & 4 deletions packages/astro/src/i18n/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
26 changes: 22 additions & 4 deletions packages/astro/src/i18n/vite-plugin-i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)};`;
}
},
Expand Down
63 changes: 54 additions & 9 deletions packages/astro/src/virtual-modules/i18n.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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,
});

/**
*
Expand All @@ -56,23 +68,56 @@ 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`.
* @param options Customise the generated path
*
* 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`.
* @param options Customise the generated path
*
* 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
Expand Down

0 comments on commit 919bbe4

Please sign in to comment.