首页 > 解决方案 > 如何在单个 js 和 twig js 应用程序中制作 i18n

问题描述

如何在单个 js 和 twig js 应用程序中制作 i18n?找不到翻译树枝的解决方案..在角度使用 angular-gettext 但它不是 ng app ..需要类似的解决方案而不是 ng app ..也许有任何解决方案?

标签: javascripthtmlinternationalizationtwiggettext

解决方案


通过包含角度 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>

推荐阅读