Skip to content

Commit a18411c

Browse files
committed
Allow to configure what files to exclude from transformation in the locales folder
1 parent c993bf0 commit a18411c

File tree

3 files changed

+7
-9
lines changed

3 files changed

+7
-9
lines changed

Diff for: sveltekit-plugin.cjs

+4-5
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ const transformYaml = async (content, { filename }) => {
2525
return `export default ${JSON.stringify(load(content, { filename }))}`;
2626
}
2727

28-
const languageFileRegex = /^[[a-z]{2}(-[a-zA-Z]{2})?\.([a-z]+)$/;
29-
3028
const stdTransformers = {
3129
// order matters as it defines which extensions are tried first
3230
// when looking for a matching file for a locale
@@ -45,6 +43,7 @@ const stdTransformers = {
4543
function svelteIntlPrecompile(localesRoot, prefixOrOptions) {
4644
const {
4745
prefix = '$locales',
46+
exclude,
4847
transformers: customTransformers,
4948
} = typeof prefixOrOptions === 'string'
5049
? { prefix: prefixOrOptions }
@@ -62,9 +61,9 @@ function svelteIntlPrecompile(localesRoot, prefixOrOptions) {
6261
];
6362

6463
const availableLocales = [];
65-
const filesInLocalesFolder = await fs.readdir(localesRoot)
66-
// Only files named after standard language codes (e.g `en.json` or `en-US.json`) are valid.s
67-
const languageFiles = filesInLocalesFolder.filter(name => languageFileRegex.test(name));
64+
const filesInLocalesFolder = await fs.readdir(localesRoot);
65+
const excludeFn = typeof exclude === 'function' ? exclude : (exclude instanceof RegExp ? (s) => exclude.test(s) : () => false);
66+
const languageFiles = filesInLocalesFolder.filter(name => !excludeFn(name));
6867

6968
// add register calls for each found locale
7069
for (const file of languageFiles) {

Diff for: sveltekit-plugin.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ type Transformer = (
55

66
interface Options {
77
prefix?: string
8+
exclude?: RegExp | ((filename: string) => boolean)
89
transformers?: Record<string, Transformer>
910
}
1011

Diff for: sveltekit-plugin.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ const transformYaml = async (content, { filename }) => {
2323
return `export default ${JSON.stringify(load(content, { filename }))}`;
2424
}
2525

26-
const languageFileRegex = /^[[a-z]{2}(-[a-zA-Z]{2})?\.([a-z]+)$/;
27-
2826
const stdTransformers = {
2927
// order matters as it defines which extensions are tried first
3028
// when looking for a matching file for a locale
@@ -61,8 +59,8 @@ function svelteIntlPrecompile(localesRoot, prefixOrOptions) {
6159

6260
const availableLocales = [];
6361
const filesInLocalesFolder = await fs.readdir(localesRoot)
64-
// Only files named after standard language codes (e.g `en.json` or `en-US.json`) are valid.s
65-
const languageFiles = filesInLocalesFolder.filter(name => languageFileRegex.test(name));
62+
const excludeFn = typeof exclude === 'function' ? exclude : (exclude instanceof RegExp ? (s) => exclude.test(s) : () => false);
63+
const languageFiles = filesInLocalesFolder.filter(name => !excludeFn(name));
6664

6765
// add register calls for each found locale
6866
for (const file of languageFiles) {

0 commit comments

Comments
 (0)