mirror of
https://github.com/withastro/astro.git
synced 2025-04-07 23:41:43 -05:00
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
This commit is contained in:
parent
9f6453cf49
commit
8cc3d6aa46
8 changed files with 90 additions and 6 deletions
5
.changeset/plenty-dingos-relax.md
Normal file
5
.changeset/plenty-dingos-relax.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'astro': patch
|
||||
---
|
||||
|
||||
Implement i18n's `getLocaleByPath` function
|
20
packages/astro/e2e/fixtures/i18n/astro.config.mjs
Normal file
20
packages/astro/e2e/fixtures/i18n/astro.config.mjs
Normal file
|
@ -0,0 +1,20 @@
|
|||
import { defineConfig } from 'astro/config';
|
||||
|
||||
// https://astro.build/config
|
||||
export default defineConfig({
|
||||
i18n: {
|
||||
defaultLocale: "en",
|
||||
locales: [
|
||||
"en",
|
||||
"fr",
|
||||
"es",
|
||||
{
|
||||
path: "portugues",
|
||||
codes: [
|
||||
"pt-AO",
|
||||
"pt",
|
||||
"pt-BR",
|
||||
],
|
||||
}],
|
||||
},
|
||||
});
|
8
packages/astro/e2e/fixtures/i18n/package.json
Normal file
8
packages/astro/e2e/fixtures/i18n/package.json
Normal file
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"name": "@e2e/i18n",
|
||||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
"astro": "workspace:*"
|
||||
}
|
||||
}
|
7
packages/astro/e2e/fixtures/i18n/src/pages/index.astro
Normal file
7
packages/astro/e2e/fixtures/i18n/src/pages/index.astro
Normal file
|
@ -0,0 +1,7 @@
|
|||
---
|
||||
import { getLocaleByPath } from "astro:i18n";
|
||||
---
|
||||
|
||||
<p>Locale: {getLocaleByPath("en")}</p> <!-- will log "en" -->
|
||||
<p>Locale: {getLocaleByPath("fr")}</p> <!-- will log "fr" -->
|
||||
<p>Locale: {getLocaleByPath("portugues")}</p> <!-- will log "pt-AO" -->
|
34
packages/astro/e2e/i18n.test.js
Normal file
34
packages/astro/e2e/i18n.test.js
Normal file
|
@ -0,0 +1,34 @@
|
|||
import { expect } from '@playwright/test';
|
||||
import { testFactory } from './test-utils.js';
|
||||
|
||||
const test = testFactory({
|
||||
root: './fixtures/i18n/',
|
||||
devToolbar: {
|
||||
enabled: false,
|
||||
},
|
||||
});
|
||||
|
||||
let devServer;
|
||||
|
||||
test.beforeAll(async ({ astro }) => {
|
||||
devServer = await astro.startDevServer();
|
||||
});
|
||||
|
||||
test.afterAll(async () => {
|
||||
await devServer.stop();
|
||||
});
|
||||
|
||||
test.describe('i18n', () => {
|
||||
test('getLocaleByPath', async ({ page, astro }) => {
|
||||
await page.goto(astro.resolveUrl('/'));
|
||||
|
||||
const p1 = page.locator('p').nth(0);
|
||||
await expect(p1).toHaveText('Locale: en');
|
||||
|
||||
const p2 = page.locator('p').nth(1);
|
||||
await expect(p2).toHaveText('Locale: fr');
|
||||
|
||||
const p3 = page.locator('p').nth(2);
|
||||
await expect(p3).toHaveText('Locale: pt-AO');
|
||||
});
|
||||
});
|
|
@ -157,17 +157,21 @@ export function getPathByLocale(locale: string, locales: Locales) {
|
|||
/**
|
||||
* An utility function that retrieves the preferred locale that correspond to a path.
|
||||
*
|
||||
* @param locale
|
||||
* @param path
|
||||
* @param locales
|
||||
*/
|
||||
export function getLocaleByPath(path: string, locales: Locales): string | undefined {
|
||||
for (const locale of locales) {
|
||||
if (typeof locale !== 'string') {
|
||||
// the first code is the one that user usually wants
|
||||
const code = locale.codes.at(0);
|
||||
return code;
|
||||
if (locale.path === path) {
|
||||
// the first code is the one that user usually wants
|
||||
const code = locale.codes.at(0);
|
||||
return code;
|
||||
}
|
||||
}
|
||||
else if (locale === path) {
|
||||
return locale;
|
||||
}
|
||||
1;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
|
|
@ -62,7 +62,7 @@ export default function astroInternationalization({
|
|||
export const getAbsoluteLocaleUrlList = (path = "", opts) => _getLocaleAbsoluteUrlList({ base, path, trailingSlash, format, site, ...i18n, ...opts });
|
||||
|
||||
export const getPathByLocale = (locale) => _getPathByLocale(locale, i18n.locales);
|
||||
export const getLocaleByPath = (locale) => _getLocaleByPath(locale, i18n.locales);
|
||||
export const getLocaleByPath = (path) => _getLocaleByPath(path, i18n.locales);
|
||||
`;
|
||||
}
|
||||
},
|
||||
|
|
6
pnpm-lock.yaml
generated
6
pnpm-lock.yaml
generated
|
@ -1046,6 +1046,12 @@ importers:
|
|||
specifier: ^10.19.2
|
||||
version: 10.19.3
|
||||
|
||||
packages/astro/e2e/fixtures/i18n:
|
||||
dependencies:
|
||||
astro:
|
||||
specifier: workspace:*
|
||||
version: link:../../..
|
||||
|
||||
packages/astro/e2e/fixtures/lit-component:
|
||||
dependencies:
|
||||
'@astrojs/lit':
|
||||
|
|
Loading…
Add table
Reference in a new issue