javascript - 如何在单个 js 和 twig js 应用程序中制作 i18n
问题描述
如何在单个 js 和 twig js 应用程序中制作 i18n?找不到翻译树枝的解决方案..在角度使用 angular-gettext 但它不是 ng app ..需要类似的解决方案而不是 ng app ..也许有任何解决方案?
解决方案
通过包含角度 gettext 解决
吞咽文件:
const gulp = require('gulp');
const gettext = require('gulp-angular-gettext');
gulp.task('extract', function () {
return gulp.src(['src/**/*.html', 'src/**/*.js'])
.pipe(gettext.extract('template.pot'))
.pipe(gulp.dest('translations/'));
});
gulp.task('translations', function () {
return gulp.src(['translations/*.po'])
.pipe(gettext.compile({
format: 'json'
}))
.pipe(gulp.dest('src/i18n'));
});
js
const fs = require('fs');
const phrases = JSON.parse(fs.readFileSync('src/i18n/zh.json', 'utf8'));
const gettext = (text) => phrases.zh[text] || text;
Twig.extendFilter('translate', gettext);
装饰
js -
page().find('.dashboard-mentions').html(gettext("You haven't been mentioned yet."));
html -
<h4 class="text-label">{{ 'Title *' | translate }}</h4>
推荐阅读
- javascript - 如何使用 JavaScript 创建具有多个可选参数的搜索?
- ios - 使用 Pulley 库的嵌入式视图控制器的 Nil IBOutlet
- javascript - 实现 Redux 到 React 以从提供的端点返回 JSON 数据
- angular - 动态生成标题标签
- xamarin - 使用 PCL 存储实际在哪里创建文件夹和文件?
- python - 我的 Python 没有正确读取 XML
- reactjs - 在 server.js 中找不到模块
- javascript - 警告:无法对未安装的组件(多个组件)执行 React 状态更新
- c++ - 如何删除列表中动态分配的类对象
? - python - 如何在列中重新组织输出结果