2016-11-20 5 views
1

i18nextをgrunt-pug-i18nタスクにバインドするのに問題があります。i18nextをgrunt-contrib-pugコンパイラにバインドする方法

私は国際化のためにi18nextとi18next-express-middlewareのウェブサイトでNode.js、Express.js、Pugを使用しています。

そこで私は翻訳を見つけるために、パグテンプレートでこの機能を使用します。

=t('key') // or #{t('key')} 

その後、私は、静的バージョンのニーズに合わせてウェブサイトをコンパイルするために面倒な作業で作男-PUG-I18Nを使用してhtmlに変換します。しかし、基本的な使用は、このような名前空間で、テンプレート内のtメソッドを置き換えるために私を必要になります。

#{$i18n.key} 
細かい動作しますが、それは動的なバージョンを壊し

動的な世界と静的な世界の両方を同じように動作させるための解決策が必要です。

私はgrunt-pug-i18nをt( 'key')メソッドで動作させようとしました。

タスクは、テンプレート内のT法を理解することができますので、私はoptions.dataにi18n.t方法をバインドすることができますようにこの問題https://github.com/AdesisNetlife/grunt-pug-i18n/issues/21を使用して、それが見えます:

// instantiate i18next 
var i18n = require('i18next'); 
i18n.init({ 
    lng: 'en', 
    resources: { 
    en: { 
     translation: grunt.file.readJSON(webDir + 'locales/en/translation.json') 
    } 
    } 
}); 
console.log(i18n.t('key'));//works fine 

... 

// grunt task 
pug: { 
    compile: { 
    options: { 
     namespace : 'JST', 
     separator : '\n\n', 
     amd   : false, 
     client  : false, 
     pretty  : true, 
     self  : false, 
     debug  : false, 
     compileDebug: true, 
     //i18n specific options 
     i18n: { 
     locales: webDir + 'locales/en/translation.json' 
     }, 
     //supposedly bind the i18n.t method to the task 
     data: function() { 
     return { 
      t: i18n.t 
     }; 
     } 
    }, 
    files: [ { 
     cwd: webDir + 'views', 
     src: ['**/*.pug', '!**/_*.pug'], 
     dest: webDir + 'static', 
     expand: true, 
     ext: '.htm' 
    } ] 
    } 
} 

結合があるように見えます行われるが、私はこのエラーで終わる:

>> TypeError: website/views/_layout.pug:9 
>>  7|  meta(name='description' content='') 
>>  8|  meta(name='author' content='') 
>> > 9|  title=t('title') 
>>  10| 
>> Cannot read property 'translator' of undefined 

作男-のcontrib-パグタスクにi18next変換方法をバインドする方法は?代わりに

//supposedly bind the i18n.t method to the task 
data: function() { 
    return { 
    t: i18n.t 
    }; 
} 

答えて

2

//supposedly bind the i18n.t method to the task 
data: function() { 
    return { 
    t: i18n.t.bind(i18n) 
    }; 
} 

を試すには、ここで詳細を参照してください。 https://github.com/i18next/i18next-express-middleware/blob/master/src/index.js#L33

+0

それはもう、エラーメッセージをスローしませんようそれが良いでしょう。それは翻訳の代わりに鍵で翻訳されます。 ** t( 'section.hout.h2')**は翻訳の代わりにsection.about2を与えます –

+0

Mh、thats strange。 指定されたキーの翻訳が見つからなかった場合のデフォルト動作です。 jadeテンプレートを使ってアプリでこのようにしています。 devツールコンソールでは動作しますか? 最初に似たような問題がありましたが、翻訳リソースが完全にロードされる前に翻訳にアクセスしていたのは私のせいでした。 – iamrickyspanish

+0

あなたは私が正しいです** grunt.file.read()** ** grunt.file.readJSON() ** i18nextインスタンシエーションで翻訳ファイルを取得すると完全に動作します。 –

関連する問題