Lint HTML and template source files for hardcoded (untranslated) strings
npm install i18n-lint



i18n-lint is a tool for detecting hardcoded (untranslated) strings in HTML and template source files. It can be used a CLI utility, or as library.i18n-lint detects instances where a HTML element's text node or certain attributes look like a hardcoded string.
See
Install using npm:
``shell`
$ npm install -g jwarby/i18n-lint
Installing globally will give you access to the i18n-lint binary from anywhere.
See
The CLI program is called i18n-lint, and will be available once i18n-lint has been installed globally.
Usage:
`shell`
$ i18n-lint [OPTIONS]
#### Program help and information
- Run i18n-lint --help or i18n-lint -h to display help output and then exiti18n-lint --version
- Run or i18n-lint -V to display version and then exitman i18n-lint
- Run on systems which support man to view the Linux manual page
#### Linting files
To lint a file, call i18n-lint :
`shell`
$ i18n-lint some_file.html
You can use a glob pattern too:
`shell`
$ i18n-lint app/views/*/.html
#### Options
##### -h, --help
Display help and then exit
##### -V, --version
Display version information and then exit
##### -t, --template-delimiters
Set the template delimiters which the source files use. The value should be the start and
end delimiters, separated by a comma. For example, if running
i18n-lint against template files which use a Mustache-like syntax, use the following:
`shell`
$ i18n-lint -t "{{,}}" views/*/.hbs
Similarly, but for EJS-syntax:
`shell`
$ i18n-lint -t "<%,%>" views/*/.ejs
##### -a, --attributes alt,placeholder,title
###### default:
A comma-separated list of which HTML attributes should be checked.
##### -i, --ignore-tags style,script
###### default:
A comma-separated list of HTML tags to ignore when searching for hardcoded strings.
##### -r, --reporter default
###### default:
The reporter to use when outputting information. The reporters follow the same structure as
JSHint reporters, and the i18n-lint library reports error in the same manner as JSHint - thisi18n-lint
means you can use any existing JSHint reporters as reporters for !
There are 3 built-in reporters that get shipped with i18n-lint: default, unix and json.
To write your own reporters, look to lib/reporters/*.js as a starting point.
##### --exclude
A comma-separated list of file patterns to exclude, such as 'docs/,ignored.html'.
##### --color/--no-color
Maintain/turn off colored output. For more info, see
#### Exit Status
- 0: if everything went OK, and no hardcoded strings were found1
- : if hardcoded strings were found64
- : command-line usage error, e.g. no input files provided ([EX_USAGE])66
- : cannot open input, e.g. input files I/O error, specified reporter file does not exist ([EX_NOINPUT])70
- : internal software error ([EX_SOFTWARE])
#### Colored Output
To maintain colored output, run i18n-lint with the --color flag:
`shell`
$ i18n-lint --color */.html | less -R
To use i18n-lint as a library, install it locally and require it in your projects:
`shell`
$ npm install --save-dev i18n-lint
`javascript
var I18nLint = require('i18n-lint');
var errors = I18nLint('some_file.ejs', {
templateDelimiters: [['<%','%>']],
attributes: ['title', 'alt', 'data-custom-attr']
});
`
> Note: prior to v1.1.0, only a single set of template delimiters could
> be passed. However, the legacy single-depth API is still supported, ie:
> templateDelimiters: ['<%', '%>'] will still work (but is not recommended).
If you want to scan a string instead of reading in a file, you can use the scan function:
`javascript
var I18nLint = require('i18n-lint');
var context = '
\ncontent not translated
';var options = {
// ...snip...
};
var errors = I18nLint.scan(context, options);
`
The scan function can also accept a fileName parameter:
`javascript
var I18nLint = require('i18n-lint');
var stdin = '';
// Read stdin stream
// ...snip...
// Once stdin has finished...
var errors = I18nLint(context, options, 'stdin');
`
This allows more meaningful output when the reporters print a filename.
#### Options
Options are passed as an object, as the second parameter to i18n-lint.
##### templateDelimitersArray
###### type: , default: []
Specify the start and end template delimiters which the source files use. We can specify
multiple delimiters if needed. For example, when linting EJS files:
`javascript`
I18nLint('file.ejs', {
templateDelimiters: [['<%', '%>']]
});
##### attributesArray
###### type: , default: ['alt', 'placeholder', 'title']
Specify which HTML attributes to check when searching for hardcoded strings.
##### ignoreTagsArray
###### type: , default: ['style', 'script', 'pre', 'code']
An array of tags which should be ignored when searching for hardcoded strings.
#### Using Reporters
When using i18n-lint as a library, you can still use the reporters:
`javascript
console.log(I18nLint.reporters);
// {
// default: [Function]
// }
var reporter = I18nLint.reporters.default;
var errors = I18nLint('file.html', {});
reporter(errors);
`
There are currently 3 built-in reporters: default, unix and json.
To use other reporters, simply require them:
`javascript
var I18nLint = require('i18n-lint');
var reporter = require('i18n-lint-awesome-reporter');
reporter(I18nLint('file.html', {}));
`
#### Error Format
`javascript`
{
file: '', // string, file which contains the errors,
error: {
id: String, // usually '(error)'
code: String, // warning code (see Warning Numbers)
reason: String, // message describing the error
evidence: RegExp, // with the offending text in match groups
line: Number, // line number of the error
character: Number, // column where evidence begins
scope: String // where the error was found
}
}
There is a grunt task which wraps i18n-lint's functionality, which
can be found at
- W001: hardcoded text node foundW002
- : hardcoded attribute value found
See CONTRIBUTING.md.
- npm testbin/
- lints JS files in , lib/ and test/mocha
- runs test suite
#### Generate Code Coverage Report
- npm run-script coverage./coverage
- coverage is output to a directory
i18n-lint` follows SemVer rules for version numbers.
- Support for more than one set of template delimiters add - thanks @alexmorvan
First published release.
Copyright (c) 2015 James Warwood. Licensed under the MIT license.
See AUTHORS.txt.
- CLI app scaffolding from