-
-
Notifications
You must be signed in to change notification settings - Fork 245
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Move locale validation to
defineRouting
(#1560)
- Loading branch information
Showing
5 changed files
with
92 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest'; | ||
import validateLocales from './validateLocales.tsx'; | ||
|
||
describe('accepts valid formats', () => { | ||
let consoleErrorSpy: ReturnType<typeof vi.spyOn>; | ||
|
||
beforeEach(() => { | ||
consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); | ||
}); | ||
|
||
afterEach(() => { | ||
consoleErrorSpy.mockRestore(); | ||
}); | ||
|
||
it.each([ | ||
'en', | ||
'en-US', | ||
'EN-US', | ||
'en-us', | ||
'en-GB', | ||
'zh-Hans-CN', | ||
'es-419', | ||
'en-Latn', | ||
'zh-Hans', | ||
'en-US-u-ca-buddhist', | ||
'en-x-private1', | ||
'en-US-u-nu-thai', | ||
'ar-u-nu-arab', | ||
'en-t-m0-true', | ||
'zh-Hans-CN-x-private1-private2', | ||
'en-US-u-ca-gregory-nu-latn', | ||
'en-US-x-usd', | ||
|
||
// Somehow tolerated by Intl.Locale | ||
'english' | ||
])('accepts: %s', (locale) => { | ||
validateLocales([locale]); | ||
expect(consoleErrorSpy).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe('warns for invalid formats', () => { | ||
let consoleErrorSpy: ReturnType<typeof vi.spyOn>; | ||
|
||
beforeEach(() => { | ||
consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); | ||
}); | ||
|
||
afterEach(() => { | ||
consoleErrorSpy.mockRestore(); | ||
}); | ||
|
||
it.each([ | ||
'en_US', | ||
'en-', | ||
'e-US', | ||
'en-USA', | ||
'und', | ||
'123', | ||
'-en', | ||
'en--US', | ||
'toolongstring', | ||
'en-US-', | ||
'@#$', | ||
'en US', | ||
'en.US' | ||
])('rejects: %s', (locale) => { | ||
validateLocales([locale]); | ||
expect(consoleErrorSpy).toHaveBeenCalled(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import type {Locales} from './types.tsx'; | ||
|
||
export default function validateLocales(locales: Locales) { | ||
for (const locale of locales) { | ||
try { | ||
const constructed = new Intl.Locale(locale); | ||
if (!constructed.language) { | ||
throw new Error('Language is required'); | ||
} | ||
} catch { | ||
console.error( | ||
`Found invalid locale within provided \`locales\`: "${locale}"\nPlease ensure you're using a valid Unicode locale identifier (e.g. "en-US").` | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters