Skip to content

Commit

Permalink
Allow to configure what files to exclude from transformation in the l…
Browse files Browse the repository at this point in the history
…ocales folder
  • Loading branch information
cibernox committed Jun 5, 2022
1 parent c993bf0 commit a18411c
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 9 deletions.
9 changes: 4 additions & 5 deletions sveltekit-plugin.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ const transformYaml = async (content, { filename }) => {
return `export default ${JSON.stringify(load(content, { filename }))}`;
}

const languageFileRegex = /^[[a-z]{2}(-[a-zA-Z]{2})?\.([a-z]+)$/;

const stdTransformers = {
// order matters as it defines which extensions are tried first
// when looking for a matching file for a locale
Expand All @@ -45,6 +43,7 @@ const stdTransformers = {
function svelteIntlPrecompile(localesRoot, prefixOrOptions) {
const {
prefix = '$locales',
exclude,
transformers: customTransformers,
} = typeof prefixOrOptions === 'string'
? { prefix: prefixOrOptions }
Expand All @@ -62,9 +61,9 @@ function svelteIntlPrecompile(localesRoot, prefixOrOptions) {
];

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

// add register calls for each found locale
for (const file of languageFiles) {
Expand Down
1 change: 1 addition & 0 deletions sveltekit-plugin.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ type Transformer = (

interface Options {
prefix?: string
exclude?: RegExp | ((filename: string) => boolean)
transformers?: Record<string, Transformer>
}

Expand Down
6 changes: 2 additions & 4 deletions sveltekit-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ const transformYaml = async (content, { filename }) => {
return `export default ${JSON.stringify(load(content, { filename }))}`;
}

const languageFileRegex = /^[[a-z]{2}(-[a-zA-Z]{2})?\.([a-z]+)$/;

const stdTransformers = {
// order matters as it defines which extensions are tried first
// when looking for a matching file for a locale
Expand Down Expand Up @@ -61,8 +59,8 @@ function svelteIntlPrecompile(localesRoot, prefixOrOptions) {

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

// add register calls for each found locale
for (const file of languageFiles) {
Expand Down

0 comments on commit a18411c

Please sign in to comment.