Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement i18n's getLocaleByPath function #9504

Merged
merged 6 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/plenty-dingos-relax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Implement i18n's `getLocaleByPath` function
20 changes: 20 additions & 0 deletions packages/astro/e2e/fixtures/i18n/astro.config.mjs
Original file line number Diff line number Diff line change
@@ -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 changes: 8 additions & 0 deletions packages/astro/e2e/fixtures/i18n/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@e2e/i18n",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
}
}
7 changes: 7 additions & 0 deletions packages/astro/e2e/fixtures/i18n/src/pages/index.astro
Original file line number Diff line number Diff line change
@@ -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 changes: 34 additions & 0 deletions packages/astro/e2e/i18n.test.js
Original file line number Diff line number Diff line change
@@ -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');
});
});
14 changes: 9 additions & 5 deletions packages/astro/src/i18n/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/i18n/vite-plugin-i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
`;
}
},
Expand Down
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.