Parse data-i18n attributes in html element
-
21-12-2019 - |
Question
I am trying to parse 'data-i18n' attributes from html files with custom regex (also with the default i18n.t function):
i18next modules/ -o lang/ -r '[^a-zA-Z0-9](?:(?:t)|(?:i18n\\.t)|(?:data-i18n))(?:\\(|\\s|=)\\s*(?:(?:\'((?:(?:\\\\\')?[^\']+)+[^\\\\])\')|(?:"((?:(?:\\\\")?[^"]+)+[^\\\\])"))'
The parser can be found here: https://github.com/karellm/i18next-parser
The command from the docs:
i18next /path/to/file/or/dir -r "(.*)"
The regex has been tested on regexpal and debuggex, it maches the proper tags:
[^a-zA-Z0-9](?:(?:t)|(?:i18n\.t)|(?:data-i18n))(?:\(|\s|=)\s*(?:(?:'((?:(?:\\')?[^']+)+[^\\])')|(?:"((?:(?:\\")?[^"]+)+[^\\])"))
I tried escaping the regex string in various forms, but it's not working. How to do it properly?
La solution
Consider using gulp as following:
var i18next = require("i18next-parser");
var gulp = require("gulp");
gulp.task("i18next-parse", function () {
gulp.src("js/modules/**")
.pipe(i18next({locales: ['en', 'hu'],
parser: '[^a-zA-Z0-9](?:(?:t)|(?:i18n\\.t)|(?:data-i18n))(?:\\(|\\s|=)\\s*(?:(?:\'((?:(?:\\\\\')?[^\']+)+[^\\\\])\')|(?:"((?:(?:\\\\")?[^"]+)+[^\\\\])"))',
output: "js/lang"})
.on('reading', function(path) {
console.log("Reading: " + path);
})
.on('writing', function(path) {
console.log("Writing: " + path);
}))
.pipe(gulp.dest('js/lang'));
});
You can pass the regex this way (which was the problem when you were trying to pass in the command line).
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow