Skip to content

Commit

Permalink
fix(i18n): fix change locale from navigate (#669)
Browse files Browse the repository at this point in the history
  • Loading branch information
aralroca authored Dec 7, 2024
1 parent 15e1aab commit a2dfd20
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 14 deletions.
10 changes: 0 additions & 10 deletions packages/brisa/src/utils/get-locale-from-request/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,6 @@ describe('utils', () => {
expect(locale).toBe('ru');
});

it('should return req.i18n.locale when exist', () => {
const request = extendRequestContext({
originalRequest: new Request('https://example.com/ru'),
});
request.i18n = { locale: 'en' } as any;
const locale = getLocaleFromRequest(request);

expect(locale).toBe('en');
});

it('should return default locale if not locale', () => {
const request = extendRequestContext({
originalRequest: new Request('https://example.com'),
Expand Down
2 changes: 0 additions & 2 deletions packages/brisa/src/utils/get-locale-from-request/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import { getConstants } from '@/constants';
import type { I18nConfig, RequestContext } from '@/types';

export default function getLocaleFromRequest(request: RequestContext): string {
if (request.i18n?.locale) return request.i18n.locale;

const { I18N_CONFIG = {} as I18nConfig, LOCALES_SET } = getConstants();
const { pathname } = new URL(request.finalURL);
const [, locale] = pathname.split('/');
Expand Down
13 changes: 13 additions & 0 deletions packages/brisa/src/utils/handle-i18n/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,19 @@ describe.each(BASE_PATHS)('handleI18n util %s', (basePath) => {
expect(req.i18n.locale).toBe('ru');
});

it('should not redirect to force another language different than the accept-language header', () => {
const req = extendRequestContext({
originalRequest: new Request('http://localhost:3000/ru/foo', {
headers: {
'Accept-Language': 'en-GB,en;q=0.9',
},
}),
});
req.finalURL = 'http://localhost:3000/ru/foo';
const { response } = handleI18n(req);
expect(response).toBeUndefined();
});

it('should redirect to the last browser language as default locale if there is no locale in the URL', () => {
const req = extendRequestContext({
originalRequest: new Request('https://example.com', {
Expand Down
43 changes: 42 additions & 1 deletion packages/brisa/src/utils/resolve-action/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ let mockLog: ReturnType<typeof spyOn>;
const getReq = (url = 'http://localhost', ...params: any) =>
extendRequestContext({
originalRequest: new Request(url, {
method: 'POST',
headers: {
'x-action': 'some-action',
},
Expand Down Expand Up @@ -432,7 +433,7 @@ describe('utils', () => {
normalizeHTML(`<div>
Test <some-web-component-to-transfer-store></some-web-component-to-transfer-store>
</div>
<script id="init-S">window._S=[["foo","bar"]]</script>`),
<script>window._S=[["foo","bar"]];for(let [k, v] of _S) _s?.set?.(k, v)</script>`),
);
});

Expand Down Expand Up @@ -535,6 +536,46 @@ describe('utils', () => {
expect(response.headers.get('X-Mode')).toBe('reactivity');
});

it('should be possible to navigate to another i18n locale + accept-language header', async () => {
globalThis.mockConstants = {
...(getConstants() ?? {}),
PAGES_DIR,
BUILD_DIR,
SRC_DIR: BUILD_DIR,
ASSETS_DIR,
LOCALES_SET: new Set(['en', 'es', 'ca']),
I18N_CONFIG: {
locales: ['en', 'es', 'ca'],
defaultLocale: 'en',
},
};
const error = new Error('/ca');
error.name = 'navigate:native';

const req = getReq('http://localhost/en');
req.i18n = {
defaultLocale: 'en',
locales: ['en', 'es', 'ca'],
locale: 'en',
t: (v) => v,
pages: {},
overrideMessages: () => {},
};
req.headers.set(
'accept-language',
'en,es-ES;q=0.9,es;q=0.8,de-CH;q=0.7,de;q=0.6,pt;q=0.5',
);
const response = await resolveAction({
req,
error,
actionId: 'a1_1',
component: () => <div />,
});

expect(response.headers.get('X-Navigate')).toBe('/ca');
expect(response.headers.get('X-Mode')).toBe('native');
});

it('should navigate with trailing slash', async () => {
globalThis.mockConstants = {
...(getConstants() ?? {}),
Expand Down
6 changes: 5 additions & 1 deletion packages/brisa/src/utils/transfer-store-service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ export default async function transferStoreService(req: RequestContext) {
const isFormData = contentType?.includes('multipart/form-data');
const formData = isFormData && bodyAvailable ? await req.formData() : null;
const encryptedKeys = new Set<string>();
const body = !isFormData && bodyAvailable ? await reqClone.json() : null;
const body =
!isFormData && bodyAvailable
? await reqClone.json().catch(() => null)
: null;

const originalStoreEntries = formData
? JSON.parse(formData.get('x-s')?.toString() ?? '[]')
: body?.['x-s'];
Expand Down

0 comments on commit a2dfd20

Please sign in to comment.