This library allows to verify the correctness of angular-translate elements.
Angular-translate is an Angular library to manage internationalization through JSon
files and keys in HTML views. Each language is managed in its own JSon file.
This library allows to check that...
{{ 'some text' }}
) are translated in HTML files.This library does not support...
./src/i18n/
../src/**/
.console.log
.false
.true
to search non-translated text in HTML mark-ups and attributes,
as well as in Angular texts ({{ 'some text' }}
). Default is true
. All the mark-ups are verified.
About attributes, only alt and title are verified.\t
.false
.false
.Add the dependency in your file...
npm install angular-translate-quality --save-dev
... and use it...
var qual = require('angular-translate-quality');
var valid = qual.validate();
... or with your own custom options...
var qual = require('angular-translate-quality');
function cb(msg) {
console.log(msg);
}
var valid = qual.validate({
loc_i18n: './i18n/**/',
loc_html: './html/**/',
cb: cb
});
Forbidden patterns is an option to ban words or verify typography in translated text (values in JSon files).
Each JSon file can have its own patterns.
In this example, we assume we have en.json, fr.json and it.json files.
We only define forbidden patterns in en.json and fr.json.
var options = {
forbidden_patterns: {}
};
options.forbidden_patterns.en = [
{regex: '\\s+[,.;:?!]', msg: '[EN] All the punctuation marks refuse any preceding white space character.'},
{regex: 'banned', sensitive: true, msg: '"banned" is a forbidden key word.'}
];
options.forbidden_patterns.fr = [
{regex: '\\s,', msg: 'Une virgule s\'écrit sans espace avant.'},
{regex: ',([^ ]| {2,})', msg: 'Une virgule s\'écrit avec un seul espace après.'},
{regex: '^[a-z]', sensitive: true, msg: 'A sentence starts with an upper-case letter.'}
];
The structure of this option is the following.
en.json
=> en
Each object has the following properties.
It is possible to intercept errors related to forbidden patterns.
As an example, considering the rules given above, a date pattern (e.g. MMM d, y @ h:mm:ss a
) would be rejected.
For such situations, it is possible to use a call back handler.
var options = {
forbidden_patterns: {}
};
// ...
options.forbidden_patterns_cb = function(errorCallback, key, value, errorMsg, lineNumber, filename) {
// Ignore the keys defined here, but only for English language
var keysToIgnoreForEnglish = ['KEY_2'];
var invalid = false;
if (filename !== 'en' || keysToIgnoreForEnglish.indexOf(key) === -1) {
// Display an error message
errorCallback(lineNumber + ': ' + errorMsg);
// Fail the validation
invalid = true;
}
return invalid;
}
When checking HTML files, mark-ups and some attributes (alt, title) are verified.
Angular-translate elements are removed from the text during the check. If the result is not empty
(or only made up of white spaces), an error is returned. Notice that HTML entities (e.g. »
) are
also skipped from the check. It is possible to add exclusions too. Thus...
<img src="https://github.com/linagora/angular-translate-quality/raw/master/" alt="..." />
... can be made valid by using the exclusions option.
var options = {
exclusions: ['...']
};
When options.check_html is true, this library searches for keys that re not used anywhere in HTML files. However, it is possible that keys are used in JS files instead of HTML ones. Let's consider the following example.
HTML view:
<p>{{ formatStatus( node ) | translate }}</p>
JS controller:
$scope.formatStatus = formatStatus;
function formatStatus( node ) {
// Find the right i18n key
return KEY;
}
Basically, the translation key is dynamically deduced by a controller function.
So, the key(s) will not be found in HTML files but in the JS file. Therefore, we do not want any warning here.
To deal with such situations, we can use the options.external_keys_cb option.
This option points to a callback function that takes the error callback (options.cb) and an array of the keys that
were not found. This callback must return false to consider the messages as warnings, true to mark the validation as invalid.
It is a way to filter warnings.
$options.external_keys_cb = function(errorCallback, notFoundKeys) {
notFoundKeys.forEach(function(key) {
// Manual check or read some file...
// If not found, add an error message.
errorCallback('Key ' + key + ' was not found anywhere.');
});
// Consider these errors as warnings => return false.
return false;
}
The warnings returned by this callback function will replace those that would have been emitted by default for not found keys.
var qual = require('angular-translate-quality');
var gutil = require('gulp-util');
gulp.task('check_i18n', function() {
var res = qual.validate();
if (! res) {
throw new gutil.PluginError({
plugin: 'check_i18n',
message: 'Errors were found about internationalization.'
});
}
});
To run checks then, just execute gulp check_i18n.
This package is licensed under the terms of the MIT license.
Simply update the version in the package.json file.
Then, commit and push your change to the Git repository, before running the release command.
npm install
gulp test
gulp coveralls
gulp lint
gulp complete-release
npm pack
For Linagora folks, releases to our NPM repository are managed through our Jenkins server.
Please, log into it and run the angular-translate-quality-release job.