Skip to content

Commit

Permalink
feat: Validate locale returned from i18n/request.ts (#1695)
Browse files Browse the repository at this point in the history
  • Loading branch information
amannn authored Feb 4, 2025
1 parent bae1131 commit 9e73cbe
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 23 deletions.
4 changes: 0 additions & 4 deletions packages/next-intl/src/routing/defineRouting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import type {
Locales,
Pathnames
} from './types.js';
import validateLocales from './validateLocales.js';

export default function defineRouting<
const AppLocales extends Locales,
Expand All @@ -20,8 +19,5 @@ export default function defineRouting<
AppDomains
>
) {
if (process.env.NODE_ENV !== 'production') {
validateLocales(config.locales);
}
return config;
}
16 changes: 0 additions & 16 deletions packages/next-intl/src/routing/validateLocales.tsx

This file was deleted.

4 changes: 4 additions & 0 deletions packages/next-intl/src/server/react-server/getConfig.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import {getRequestLocale} from './RequestLocale.js';
import createRequestConfig from './createRequestConfig.js';
import type {GetRequestConfigParams} from './getRequestConfig.js';
import validateLocale from './validateLocale.js';

// This is automatically inherited by `NextIntlClientProvider` if
// the component is rendered from a Server Component
Expand Down Expand Up @@ -60,6 +61,9 @@ See also: https://next-intl.dev/docs/usage/configuration#i18n-request
'No locale was returned from `getRequestConfig`.\n\nSee https://next-intl.dev/docs/usage/configuration#i18n-request'
);
}
if (process.env.NODE_ENV !== 'production') {
validateLocale(result.locale);
}

return result;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';
import validateLocales from './validateLocales.js';
import validateLocale from './validateLocale.js';

describe('accepts valid formats', () => {
let consoleErrorSpy: ReturnType<typeof vi.spyOn>;
Expand Down Expand Up @@ -34,7 +34,7 @@ describe('accepts valid formats', () => {
// Somehow tolerated by Intl.Locale
'english'
])('accepts: %s', (locale) => {
validateLocales([locale]);
validateLocale(locale);
expect(consoleErrorSpy).not.toHaveBeenCalled();
});
});
Expand Down Expand Up @@ -65,7 +65,7 @@ describe('warns for invalid formats', () => {
'en US',
'en.US'
])('rejects: %s', (locale) => {
validateLocales([locale]);
validateLocale(locale);
expect(consoleErrorSpy).toHaveBeenCalled();
});
});
12 changes: 12 additions & 0 deletions packages/next-intl/src/server/react-server/validateLocale.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default function validateLocale(locale: string) {
try {
const constructed = new Intl.Locale(locale);
if (!constructed.language) {
throw new Error('Language is required');
}
} catch {
console.error(
`An invalid locale was provided: "${locale}"\nPlease ensure you're using a valid Unicode locale identifier (e.g. "en-US").`
);
}
}

0 comments on commit 9e73cbe

Please sign in to comment.