简体   繁体   English

Gulp任务翻译JS

[英]Gulp task to translate JS

I'm working on a JavaScript app and have so far entered all my strings as plain text. 我正在使用JavaScript应用程序,到目前为止,我所有的字符串都输入为纯文本。

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. 这开始感觉真的很hacky(我习惯于获取文本),所以我更喜欢将它们全部包裹在{{translatable_string}}之类的东西中,并且吞噬所有任务,只在构建步骤中搜索/替换它们。

So, my question is; 所以,我的问题是; is there a generic (no framework-specific like angular-gettext or something like that) gettext replacer out there? 是否有通用的(没有特定于框架的类似angular-gettext或类似的东西)gettext替换器?

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. 显然,它甚至不必以任何方式连接到JavaScript,您应该能够在任何文件类型上运行它并进行{{translatable_string}}:s的翻译。

You may want to look into using gulp-replace . 您可能需要研究使用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. 一个简单的MongoDB表可用于存储内容,并且当应用程序需要界面时,可以使用正确的本地化字符串加载该表。 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. 如果您需要使用gulp构建静态页面,那么结合gulp-replace的数据库听起来很有趣。 Using gulp-data to call up and package the strings, you can then feed it to gulp-replace and alter the files. 使用gulp-data调用和打包字符串,然后可以将其提供给gulp-replace和更改文件。 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 . 尝试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/ 也许您需要的是mustache.js ,看看: 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. 如果您熟悉jade (现在已重命名为pug),则会发现类似的东西,但是最后,您不必强迫仅生成html文件,可以生成任何类型的文本文件。

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/ 该博客有助于理解Nodejs上其他模板语言之间的差异: https ://strongloop.com/strongblog/compare-javascript-templates-jade-mustache-dust/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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