Skip to content

Commit

Permalink
chore: fix other broken cases inside source code
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico committed Nov 28, 2023
1 parent bb9a520 commit dab8bef
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
3 changes: 2 additions & 1 deletion packages/astro/src/core/endpoint/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type {
APIContext,
EndpointHandler,
EndpointOutput,
Locales,
MiddlewareEndpointHandler,
MiddlewareHandler,
Params,
Expand Down Expand Up @@ -30,7 +31,7 @@ type CreateAPIContext = {
site?: string;
props: Record<string, any>;
adapterName?: string;
locales: string[] | undefined;
locales: Locales | undefined;
routingStrategy: 'prefix-always' | 'prefix-other-locales' | undefined;
defaultLocale: string | undefined;
};
Expand Down
25 changes: 19 additions & 6 deletions packages/astro/src/core/render/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export interface RenderContext {
params: Params;
props: Props;
locals?: object;
locales: string[] | undefined;
locales: Locales | undefined;
defaultLocale: string | undefined;
routingStrategy: 'prefix-always' | 'prefix-other-locales' | undefined;
}
Expand Down Expand Up @@ -198,15 +198,22 @@ export function computePreferredLocale(request: Request, locales: Locales): stri
return result;
}

export function computePreferredLocaleList(request: Request, locales: Locales) {
export function computePreferredLocaleList(request: Request, locales: Locales): string[] {
const acceptHeader = request.headers.get('Accept-Language');
let result: string[] = [];
if (acceptHeader) {
const browserLocaleList = sortAndFilterLocales(parseLocale(acceptHeader), locales);

// SAFETY: bang operator is safe because checked by the previous condition
if (browserLocaleList.length === 1 && browserLocaleList.at(0)!.locale === '*') {
return locales;
return locales.map((locale) => {
if (typeof locale === 'string') {
return locale;
} else {
// SAFETY: codes is never empty
return locale.codes.at(0)!;
}
});
} else if (browserLocaleList.length > 0) {
for (const browserLocale of browserLocaleList) {
for (const loopLocale of locales) {
Expand All @@ -231,15 +238,21 @@ export function computePreferredLocaleList(request: Request, locales: Locales) {

export function computeCurrentLocale(
request: Request,
locales: string[],
locales: Locales,
routingStrategy: 'prefix-always' | 'prefix-other-locales' | undefined,
defaultLocale: string | undefined
): undefined | string {
const requestUrl = new URL(request.url);
for (const segment of requestUrl.pathname.split('/')) {
for (const locale of locales) {
if (normalizeTheLocale(locale) === normalizeTheLocale(segment)) {
return locale;
if (typeof locale === 'string') {
if (normalizeTheLocale(locale) === normalizeTheLocale(segment)) {
return locale;
}
} else {
if (locale.path === segment) {
return locale.codes.at(0);
}
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion packages/astro/src/core/render/result.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type {
AstroGlobal,
AstroGlobalPartial,
Locales,
Params,
SSRElement,
SSRLoadedRenderer,
Expand Down Expand Up @@ -50,7 +51,7 @@ export interface CreateResultArgs {
status: number;
locals: App.Locals;
cookies?: AstroCookies;
locales: string[] | undefined;
locales: Locales | undefined;
defaultLocale: string | undefined;
routingStrategy: 'prefix-always' | 'prefix-other-locales' | undefined;
}
Expand Down

0 comments on commit dab8bef

Please sign in to comment.