gulp-locale-filter
Gulp plugin that filters the files in the stream based on locale or language codes in the file paths, optionally
renaming the files to their base name, without the code. Use this to e.g. localize the output of a build process,
by filtering to only the file variants that are relevant for the target locale.
When identifying the locale of a file, the plugin evaluates each segment in the relative file path, stopping when it
finds the first segment, if any, that matches, or is postfixed with, a language or locale code, or if a default base
name is specified, matches that. This means, that given a file path such as ./foo/en-us/da-dk.html
, the locale would
be identified as en-us
. If the file path contains no locale info, the file is considered a base file, and is only
passed through if there exist no file variants matching the target locale. If both a matching language and locale variant
is found, the locale variant will take precedence. Note that this plugin does not replace files in the stream - it
simply filters the files that pass through, such that only the best matches are let through, and optionally renamed.
Locale codes are expected to be in the IEFT language tag format,
and must be composed of a single primary 2-letter language subtag, an optional 4-letter script subtag, and a 2-letter
region subtag. Language codes are expected to be a 2-letters. Note that the matching is case-sensitive, meaning that
the casing of the target locale code specified in the filter
config must be the same as the casing of locale codes
in the file paths.
You may also want to look at the plugins:
-
gulp-tree-filter for filtering files based using include and exclude globs defined in config files located within the folder tree.
Use this to e.g. prevent localizable content from being extracted from unfinished features.
-
gulp-locale-filter for filtering files based on locale or language codes in the file path.
Use this to e.g. include only the locale config files that are relevant for the target locale when creating a localized build.
-
gulp-replace for replacing text content in files.
Use this to e.g. replace placeholder such as {{locale}}
in templates and CSS files with the actual target locale code when creating a localized build.
Examples
Assume we have the following Gulp task for localizing the contents of a folder:
var targetLocaleCode = "da-dk";
var pluginConfig = { };
gulp.task("localize", function ()
{
return gulp
.src("./sources/**")
.pipe(localeFilter(pluginConfig).filter({
localeCode: targetLocaleCode,
renameToBaseName: false
}))
.pipe(gulp.dest("./artifacts/" + targetLocaleCode));
});
Files or folders with a locale postfix
Given a sources
folder containing the files:
foo.html
bar.html
bar.en-us.html
bar.da-dk.html
The artifacts/da-dk
folder would, after running the task, contain only the files:
foo.html
bar.da-dk.html
Or, if the renameToBaseName
plugin option is enabled:
foo.html
bar.html // this is actually 'bar.da-dk.html', renamed to its base name
Locale folders
Given a sources
folder containing locale-specific subfolders:
- foo
foo.html
- da-dk
bar.html
- en-us
bar.html
The artifacts/da-dk
folder would, after running the task, contain only the files:
- foo
foo.html
- da-dk
bar.html
Or, if the renameToBaseName
plugin option is enabled and defaultBaseName
is set to "locale":
- foo
foo.html
- locale // this is actually 'da-dk', renamed to its default base name
bar.html
Locale files
Given a sources
folder containing locale-specific files:
foo.html
da-dk.html
en-us.html
The artifacts/da-dk
folder would, after running the task, contain only the files:
foo.html
da-dk.html
Or, if the renameToBaseName
option is enabled and defaultBaseName
is set to locale
:
foo.html
locale.html // this is actually 'da-dk.html', renamed to its default base name
More options
This plugin can support more scenarios than the above examples, including e.g. filtering based on language codes.
Please review, and carefully consider the implications of, the available configuration options and default values.
See also the example in the repository, which demonstrates how this works.
How to use the plugin
Install the plugin as a dev dependency:
npm install gulp-locale-filter --save-dev
Use the plugin:
var localeFilter = require("gulp-locale-filter");
var pluginConfig = { };
.pipe(localeFilter(pluginConfig).filter(filterConfig))
Plugin config
The following is the interface for the config object, that may optionally be passed to the plugin function.
interface IPluginConfig
{
matchLocaleFolders?: boolean|RegExp;
matchLocaleFiles?: boolean|RegExp;
matchLocalePostfixes?: boolean|RegExp;
matchLanguageFolders?: boolean|RegExp;
matchLanguageFiles?: boolean|RegExp;
matchLanguagePostfixes?: boolean|RegExp;
matchOnlyIfBaseNameExists?: boolean;
defaultBaseName?: string;
fileNameExtensions?: string[];
cache?: boolean;
debug?: boolean;
}
The export
command
Example:
var targetLocaleCode = "da-dk";
gulp.task("localize", function ()
{
return gulp
.src("./sources/**")
.pipe(localeFilter(pluginConfig).filter(
{
localeCode: targetLocaleCode,
renameToBaseName: true
}))
.pipe(gulp.dest("./artifacts/" + targetLocaleCode));
});
The filter
config
The following is the interface for the config object, that may optionally be passed to the filter
function.
interface IFilterCommandConfig
{
localeCode?: string;
renameToBaseName?: boolean;
}
Enjoy, and please report any issues in the issue tracker :-)