【问题标题】:How to make i18n in single js and twig js app如何在单个 js 和 twig js 应用程序中制作 i18n
【发布时间】:2018-06-29 15:08:44
【问题描述】:

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

【问题讨论】:

    标签: javascript html internationalization twig gettext


    【解决方案1】:

    通过包含 angular gettext 解决

    gulpfile:

    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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-06
      • 1970-01-01
      • 2019-11-04
      • 1970-01-01
      • 1970-01-01
      • 2020-05-13
      • 2020-04-22
      相关资源
      最近更新 更多