简体   繁体   中英

Gulp task to translate JS

I'm working on a JavaScript app and have so far entered all my strings as plain text.

This is starting to feel really hacky (I'm used to gettext) so I'd prefer to wrap them all in something like {{translatable_string}} and have a gulp task just search/replace them all during the build step.

So, my question is; is there a generic (no framework-specific like angular-gettext or something like that) gettext replacer out there?

Obviously it doesn't even have to be connected to JavaScript in any way, you should be able to run it on any file type and have {{translatable_string}}:s be translated.

You may want to look into using gulp-replace . As they explained in this answer , you should be able to use it to find and replace any string that you want in the stream.

I suggest a database of strings for your translations if dynamic generation of page content is possible for your app. Starting with English or whichever is normal but the need to localize content is a tough issue without a robust system. A simple MongoDB table can be used to store the content, and when the app needs an interface it can be loaded with the right localized strings. As a for instance:

if(err) alert("Please turn off caps lock");

could become:

if(err) alert(Please_turn_off_caps_lock.English);

If you are needing to build static pages with gulp, a database in conjunction with gulp-replace sounds interesting. Using gulp-data to call up and package the strings, you can then feed it to gulp-replace and alter the files. The extensible nature of databases or document stores enable you to expand your localization without hacking on individual files or trees all the time.

Try gulp-gettext-parser .

var gettext = require("gulp-gettext-parser");
var rename = require("gulp-rename");

gulp.task("gettext", function() {
   return gulp.src("src/**/*.js")
      .pipe(gettext())
      .pipe(rename("bundle.po"))
      .pipe(gulp.dest("dist/"));
});

Perhaps what you need is mustache.js , take a look: https://github.com/janl/mustache.js/

I'm not used to work with mustache, but I had to do some updates in a project done with it, and I was surprised the capabilities it have.

If you're familiar with jade (now renamed to pug), you'll find is something similar but at the end, you're not forced to generate only html files, you cand generate any kind of text file.

This blog could be helpful to understand the differences between some other templating languages over Nodejs: https://strongloop.com/strongblog/compare-javascript-templates-jade-mustache-dust/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM