0

レンダリングエンジンとしてejsを使用して、email-templatesモジュールで動作するようにnodemailerをセットアップしました。電子メールテンプレートを使用したi18nモジュール

これまでのところ、私のアプリケーションの他のセクションと同様に、i18nモジュールを使用してメールテキストを自分のアプリケーションのレンダリングビューと同じように翻訳したいと思っています。

残念ながら、動作しません。

例html.ejs:ルートで

<h1><%= __('Hi') %>, <%= user.name %>!</h1> 

ノードコード:物事の

// requires at the top 
    var i18n = require('i18n'); 

    // (.....) 

    // use template based sender to send a message 
    sendMailTemplate({ 
     to: user.email, 
     // EmailTemplate renders html and text but no subject so we need to 
     // set it manually either here or in the defaults section of templateSender() 
     subject: i18n.__('translatable subject') 
    }, { 
     user: user, 
     __: i18n.__ 
    }, function(err, info){ 
     if(err){ 
      console.log('Error'); 
      console.log(err); 
     }else{ 
      console.log('email sent'); 
      req.flash('info', i18n.__('mail sent to %s', user.email)); 
      done(err, 'done');     
     } 
    }); 
    // other stuff... 

だけのカップル:

    これは私がやってみましたものです
  • 私のメールの出力では、何もborks - strin gはちょうど翻訳されていません。
  • 私は、レンダリングエンジンに行くオブジェクトにi18n__関数を渡すだけで、ejsに利用できるようになり、意図したとおりに実行できるはずですが、わかりません。これについての考えは?
  • i18n.setLocaleはありませんので、req.(これは私の受信したメールに表示されている言語です)と仮定すると、おそらく英語に設定されているはずです。これが意図したとおりに翻訳されていないのはなぜですか?

ご迷惑をおかけしておりません。

答えて

0

まあ、私が正しいと分かりました。 i18nが実際に英語にデフォルトしていました。私はテンプレートエンジンに渡す前にロケールを設定しなければならないと思っていました。

express.jsのreq.accceptsLanguages()メソッドを使用して、受け入れられている言語が優先されました。これは実際にはユーザーの母国語であることを保証する方法がないため(ほとんどの場合はそうであるはずですが)、防御的ではありません。

また、グローバルな範囲でi18nを使用していたので、それが英語にデフォルト設定されていた理由です。そのように、私はhereを示すように、次にそれを使用して、テンプレートエンジンのaswellにそれを渡して:

__({phrase: 'Hello', locale: 'fr'}); // Salut 

ので、最終ビットは、この線に沿って何かである:なぜわからない

// requires at the top 
var i18n = require('i18n'); 

// (.....) 

// THIS IS NEW - it's from express.js 
// returns a shorthand for the user's preferred locale: en, es, de, fr, pt, ... 
var userLocale = req.acceptsLanguages()[1]; 

// use template based sender to send a message 
sendMailTemplate({ 
    to: user.email, 
    // EmailTemplate renders html and text but no subject so we need to 
    // set it manually either here or in the defaults section of templateSender() 
    subject: i18n.__({phrase: 'translatable subject', locale: userLocale}) // note the syntax here 
}, { 
    user: user, 
    __: i18n.__, 
    locale: userLocale  // THIS IS ALSO NEW 
}, function(err, info){ 
    if(err){ 
     console.log('Error'); 
     console.log(err); 
    }else{ 
     console.log('email sent'); 
     // also note the syntax here: 
     req.flash('info', i18n.__({phrase: 'mail sent to %s', locale: userLocale}, user.email)); 
     done(err, 'done');     
    } 
}); 
// other stuff... 
+0

Funilly、 ejs電子メールテンプレートでこの構文を使用する前であっても、正しいロケールですでにレンダリングされていました。私はそれがテンプレートに渡される前に、 'subject'文字列でそれを使用したことと何か関係があると思います。 – Joum

関連する問題