Skip to content

Commit 8cc3d6a

Browse files
authoredJan 2, 2024
Implement i18n's getLocaleByPath function (#9504)
* Implement getLocaleByPath function * Fix param naming in getLocaleByPath function for users * Add changeset * Change changeset to patch astro * Add i18n getLocaleByPath e2e test * Add astro e2e i18n in pnpm-lock.yaml
1 parent 9f6453c commit 8cc3d6a

File tree

8 files changed

+90
-6
lines changed

8 files changed

+90
-6
lines changed
 

‎.changeset/plenty-dingos-relax.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Implement i18n's `getLocaleByPath` function
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { defineConfig } from 'astro/config';
2+
3+
// https://astro.build/config
4+
export default defineConfig({
5+
i18n: {
6+
defaultLocale: "en",
7+
locales: [
8+
"en",
9+
"fr",
10+
"es",
11+
{
12+
path: "portugues",
13+
codes: [
14+
"pt-AO",
15+
"pt",
16+
"pt-BR",
17+
],
18+
}],
19+
},
20+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "@e2e/i18n",
3+
"version": "0.0.0",
4+
"private": true,
5+
"dependencies": {
6+
"astro": "workspace:*"
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
import { getLocaleByPath } from "astro:i18n";
3+
---
4+
5+
<p>Locale: {getLocaleByPath("en")}</p> <!-- will log "en" -->
6+
<p>Locale: {getLocaleByPath("fr")}</p> <!-- will log "fr" -->
7+
<p>Locale: {getLocaleByPath("portugues")}</p> <!-- will log "pt-AO" -->

‎packages/astro/e2e/i18n.test.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { expect } from '@playwright/test';
2+
import { testFactory } from './test-utils.js';
3+
4+
const test = testFactory({
5+
root: './fixtures/i18n/',
6+
devToolbar: {
7+
enabled: false,
8+
},
9+
});
10+
11+
let devServer;
12+
13+
test.beforeAll(async ({ astro }) => {
14+
devServer = await astro.startDevServer();
15+
});
16+
17+
test.afterAll(async () => {
18+
await devServer.stop();
19+
});
20+
21+
test.describe('i18n', () => {
22+
test('getLocaleByPath', async ({ page, astro }) => {
23+
await page.goto(astro.resolveUrl('/'));
24+
25+
const p1 = page.locator('p').nth(0);
26+
await expect(p1).toHaveText('Locale: en');
27+
28+
const p2 = page.locator('p').nth(1);
29+
await expect(p2).toHaveText('Locale: fr');
30+
31+
const p3 = page.locator('p').nth(2);
32+
await expect(p3).toHaveText('Locale: pt-AO');
33+
});
34+
});

‎packages/astro/src/i18n/index.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -157,17 +157,21 @@ export function getPathByLocale(locale: string, locales: Locales) {
157157
/**
158158
* An utility function that retrieves the preferred locale that correspond to a path.
159159
*
160-
* @param locale
160+
* @param path
161161
* @param locales
162162
*/
163163
export function getLocaleByPath(path: string, locales: Locales): string | undefined {
164164
for (const locale of locales) {
165165
if (typeof locale !== 'string') {
166-
// the first code is the one that user usually wants
167-
const code = locale.codes.at(0);
168-
return code;
166+
if (locale.path === path) {
167+
// the first code is the one that user usually wants
168+
const code = locale.codes.at(0);
169+
return code;
170+
}
171+
}
172+
else if (locale === path) {
173+
return locale;
169174
}
170-
1;
171175
}
172176
return undefined;
173177
}

‎packages/astro/src/i18n/vite-plugin-i18n.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export default function astroInternationalization({
6262
export const getAbsoluteLocaleUrlList = (path = "", opts) => _getLocaleAbsoluteUrlList({ base, path, trailingSlash, format, site, ...i18n, ...opts });
6363
6464
export const getPathByLocale = (locale) => _getPathByLocale(locale, i18n.locales);
65-
export const getLocaleByPath = (locale) => _getLocaleByPath(locale, i18n.locales);
65+
export const getLocaleByPath = (path) => _getLocaleByPath(path, i18n.locales);
6666
`;
6767
}
6868
},

‎pnpm-lock.yaml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
Please sign in to comment.