Skip to content

Commit

Permalink
Fix get relative locale (#13045)
Browse files Browse the repository at this point in the history
Co-authored-by: Emanuele Stoppa <[email protected]>
  • Loading branch information
mtwilliams-code and ematipico authored Jan 24, 2025
1 parent 821d642 commit c7f1366
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/seven-avocados-confess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes a bug where the some utility functions of the `astro:i18n` virtual module would return an incorrect result when `trailingSlash` is set to `never`
14 changes: 11 additions & 3 deletions packages/astro/src/i18n/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,17 @@ export function getLocaleRelativeUrl({
}
pathsToJoin.push(path);

let relativePath: string;
if (shouldAppendForwardSlash(trailingSlash, format)) {
return appendForwardSlash(joinPaths(...pathsToJoin));
relativePath = appendForwardSlash(joinPaths(...pathsToJoin));
} else {
return joinPaths(...pathsToJoin);
relativePath = joinPaths(...pathsToJoin);
}

if (relativePath === '') {
return '/';
}
return relativePath;
}

/**
Expand All @@ -124,7 +130,9 @@ export function getLocaleAbsoluteUrl({ site, isBuild, ...rest }: GetLocaleAbsolu
const base = domains[locale];
url = joinPaths(base, localeUrl.replace(`/${rest.locale}`, ''));
} else {
if (site) {
if (localeUrl === '/') {
url = site || '/';
} else if (site) {
url = joinPaths(site, localeUrl);
} else {
url = localeUrl;
Expand Down
71 changes: 69 additions & 2 deletions packages/astro/test/units/i18n/astro_i18n.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import {
getLocaleRelativeUrl,
getLocaleRelativeUrlList,
} from '../../../dist/i18n/index.js';
import { toRoutingStrategy } from '../../../dist/i18n/utils.js';
import { parseLocale } from '../../../dist/i18n/utils.js';
import { parseLocale, toRoutingStrategy } from '../../../dist/i18n/utils.js';

describe('getLocaleRelativeUrl', () => {
it('should correctly return the URL with the base', () => {
Expand Down Expand Up @@ -126,6 +125,39 @@ describe('getLocaleRelativeUrl', () => {
}),
'/es/',
);

assert.equal(
getLocaleRelativeUrl({
locale: 'en',
base: '/',
...config.experimental.i18n,
trailingSlash: 'never',
format: 'file',
}),
'/',
);

assert.equal(
getLocaleRelativeUrl({
locale: 'es',
base: '/',
...config.experimental.i18n,
trailingSlash: 'never',
format: 'file',
}),
'/es',
);

assert.equal(
getLocaleRelativeUrl({
locale: 'en',
base: '/',
...config.experimental.i18n,
trailingSlash: 'never',
format: 'directory',
}),
'/',
);
});

it('should correctly handle the trailing slash', () => {
Expand Down Expand Up @@ -1228,6 +1260,8 @@ describe('getLocaleAbsoluteUrl', () => {
'https://example.com/blog/es/',
);
});
});
describe('with [prefix-other-locales]', () => {
it('should correctly return the URL without base', () => {
/**
*
Expand Down Expand Up @@ -1283,6 +1317,39 @@ describe('getLocaleAbsoluteUrl', () => {
}),
'https://example.com/italiano/',
);
assert.equal(
getLocaleAbsoluteUrl({
locale: 'en',
base: '/',
...config.experimental.i18n,
trailingSlash: 'never',
format: 'directory',
site: 'https://example.com',
}),
'https://example.com',
);
assert.equal(
getLocaleAbsoluteUrl({
locale: 'es',
base: '/',
...config.experimental.i18n,
trailingSlash: 'never',
format: 'directory',
site: 'https://example.com',
}),
'https://example.com/es',
);
assert.equal(
getLocaleAbsoluteUrl({
locale: 'it-VA',
base: '/',
...config.experimental.i18n,
trailingSlash: 'never',
format: 'directory',
site: 'https://example.com',
}),
'https://example.com/italiano',
);
});

it('should correctly handle the trailing slash', () => {
Expand Down

0 comments on commit c7f1366

Please sign in to comment.