diff options
author | 2024-01-02 13:08:17 +0100 | |
---|---|---|
committer | 2024-01-02 12:08:17 +0000 | |
commit | 8cc3d6aa46f438d668516539c34b48ad748ade39 (patch) | |
tree | 64ebb4c2398a5366125fe16e58c3ff0586954b06 | |
parent | 9f6453cf4972ac28eec4f07a1373feaa295c8864 (diff) | |
download | astro-8cc3d6aa46f438d668516539c34b48ad748ade39.tar.gz astro-8cc3d6aa46f438d668516539c34b48ad748ade39.tar.zst astro-8cc3d6aa46f438d668516539c34b48ad748ade39.zip |
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
-rw-r--r-- | .changeset/plenty-dingos-relax.md | 5 | ||||
-rw-r--r-- | packages/astro/e2e/fixtures/i18n/astro.config.mjs | 20 | ||||
-rw-r--r-- | packages/astro/e2e/fixtures/i18n/package.json | 8 | ||||
-rw-r--r-- | packages/astro/e2e/fixtures/i18n/src/pages/index.astro | 7 | ||||
-rw-r--r-- | packages/astro/e2e/i18n.test.js | 34 | ||||
-rw-r--r-- | packages/astro/src/i18n/index.ts | 14 | ||||
-rw-r--r-- | packages/astro/src/i18n/vite-plugin-i18n.ts | 2 | ||||
-rw-r--r-- | pnpm-lock.yaml | 6 |
8 files changed, 90 insertions, 6 deletions
diff --git a/.changeset/plenty-dingos-relax.md b/.changeset/plenty-dingos-relax.md new file mode 100644 index 000000000..4bb328ee1 --- /dev/null +++ b/.changeset/plenty-dingos-relax.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Implement i18n's `getLocaleByPath` function diff --git a/packages/astro/e2e/fixtures/i18n/astro.config.mjs b/packages/astro/e2e/fixtures/i18n/astro.config.mjs new file mode 100644 index 000000000..2fa926e94 --- /dev/null +++ b/packages/astro/e2e/fixtures/i18n/astro.config.mjs @@ -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", + ], + }], + }, +}); diff --git a/packages/astro/e2e/fixtures/i18n/package.json b/packages/astro/e2e/fixtures/i18n/package.json new file mode 100644 index 000000000..3d28c6bf0 --- /dev/null +++ b/packages/astro/e2e/fixtures/i18n/package.json @@ -0,0 +1,8 @@ +{ + "name": "@e2e/i18n", + "version": "0.0.0", + "private": true, + "dependencies": { + "astro": "workspace:*" + } +} diff --git a/packages/astro/e2e/fixtures/i18n/src/pages/index.astro b/packages/astro/e2e/fixtures/i18n/src/pages/index.astro new file mode 100644 index 000000000..5c3de3888 --- /dev/null +++ b/packages/astro/e2e/fixtures/i18n/src/pages/index.astro @@ -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" --> diff --git a/packages/astro/e2e/i18n.test.js b/packages/astro/e2e/i18n.test.js new file mode 100644 index 000000000..e7d74a551 --- /dev/null +++ b/packages/astro/e2e/i18n.test.js @@ -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'); + }); +}); diff --git a/packages/astro/src/i18n/index.ts b/packages/astro/src/i18n/index.ts index 947689d51..694419991 100644 --- a/packages/astro/src/i18n/index.ts +++ b/packages/astro/src/i18n/index.ts @@ -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; } diff --git a/packages/astro/src/i18n/vite-plugin-i18n.ts b/packages/astro/src/i18n/vite-plugin-i18n.ts index cd4c3f854..f01116f6c 100644 --- a/packages/astro/src/i18n/vite-plugin-i18n.ts +++ b/packages/astro/src/i18n/vite-plugin-i18n.ts @@ -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); `; } }, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 0f9aa7139..4fe84084b 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -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': |