Skip to content

Commit

Permalink
chore: add new test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico committed Nov 28, 2023
1 parent bc5a0b5 commit 098a676
Show file tree
Hide file tree
Showing 13 changed files with 316 additions and 19 deletions.
2 changes: 1 addition & 1 deletion packages/astro/src/core/render/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ export function computePreferredLocale(request: Request, locales: Locales): stri
} else {
for (const currentCode of currentLocale.codes) {
if (normalizeTheLocale(currentCode) === normalizeTheLocale(firstResult.locale)) {
result = currentCode;
result = currentLocale.path;
}
}
}
Expand Down
13 changes: 8 additions & 5 deletions packages/astro/src/core/routing/manifest/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,9 @@ export function createRouteManifest(

// we loop over the remaining routes and add them to the default locale
for (const route of setRoutes) {
if (route.type !== 'page') {
continue;
}
const currentRoutes = routesByLocale.get(i18n.defaultLocale);
if (currentRoutes) {
currentRoutes.push(route);
Expand Down Expand Up @@ -584,12 +587,12 @@ export function createRouteManifest(
let fallback = Object.entries(i18n.fallback);

if (fallback.length > 0) {
for (const [_fallbackFromLocale, fallbackToLocale] of fallback) {
for (const [fallbackFromLocale, fallbackToLocale] of fallback) {
let fallbackToRoutes;
const fallbackFromLocale = getPathByLocale(_fallbackFromLocale, i18n.locales);
if (!fallbackFromLocale) {
continue;
}
// const fallbackFromLocale = getPathByLocale(_fallbackFromLocale, i18n.locales);
// if (!fallbackFromLocale) {
// continue;
// }
if (fallbackToLocale === i18n.defaultLocale) {
fallbackToRoutes = routesByLocale.get(i18n.defaultLocale);
} else {
Expand Down
17 changes: 16 additions & 1 deletion packages/astro/src/vite-plugin-astro-server/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { preload } from './index.js';
import { getComponentMetadata } from './metadata.js';
import { handle404Response, writeSSRResult, writeWebResponse } from './response.js';
import { getScriptsForURL } from './scripts.js';
import { normalizeTheLocale } from '../i18n/index.js';

const clientLocalsSymbol = Symbol.for('astro.locals');

Expand Down Expand Up @@ -185,7 +186,21 @@ export async function handleRoute({
.split('/')
.filter(Boolean)
.some((segment) => {
return locales.includes(segment);
let found = false;
for (const locale of locales) {
if (typeof locale === 'string') {
if (normalizeTheLocale(locale) === normalizeTheLocale(segment)) {
found = true;
break;
}
} else {
if (locale.path === segment) {
found = true;
break;
}
}
}
return found;
});
// Even when we have `config.base`, the pathname is still `/` because it gets stripped before
if (!pathNameHasLocale && pathname !== '/') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ export default defineConfig({
i18n: {
defaultLocale: 'en',
locales: [
'en', 'pt', 'it'
'en', 'pt', 'it', {
path: "spanish",
codes: ["es", "es-ar"]
}
],
routingStrategy: "prefix-always"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
export function getStaticPaths() {
return [
{params: {id: '1'}, props: { content: "Lo siento" }},
{params: {id: '2'}, props: { content: "Eat Something" }},
{params: {id: '3'}, props: { content: "How are you?" }},
];
}
const { content } = Astro.props;
---
<html>
<head>
<title>Astro</title>
</head>
<body>
{content}
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
const currentLocale = Astro.currentLocale;
---
<html>
<head>
<title>Astro</title>
</head>
<body>
Espanol
Current Locale: {currentLocale ? currentLocale : "none"}
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ export default defineConfig({
i18n: {
defaultLocale: 'en',
locales: [
'en', 'pt', 'it'
'en', 'pt', 'it', {
path: "spanish",
codes: ["es", "es-ar"]
}
],
routingStrategy: "prefix-always"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
export function getStaticPaths() {
return [
{params: {id: '1'}, props: { content: "Lo siento" }},
{params: {id: '2'}, props: { content: "Eat Something" }},
{params: {id: '3'}, props: { content: "How are you?" }},
];
}
const { content } = Astro.props;
---
<html>
<head>
<title>Astro</title>
</head>
<body>
{content}
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
export function getStaticPaths() {
return [
{params: {id: '1'}, props: { content: "Lo siento" }},
{params: {id: '2'}, props: { content: "Eat Something" }},
{params: {id: '3'}, props: { content: "How are you?" }},
];
}
const { content } = Astro.props;
---
<html>
<head>
<title>Astro</title>
</head>
<body>
{content}
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
const currentLocale = Astro.currentLocale;
---
<html>
<head>
<title>Astro</title>
</head>
<body>
Espanol
Current Locale: {currentLocale ? currentLocale : "none"}
</body>
</html>
Loading

0 comments on commit 098a676

Please sign in to comment.