LoginSignup
0
1

More than 5 years have passed since last update.

i18next tipsその1

Posted at

PersoniumのOSS開発で下記の問題を解決したので、みなさんに共有します。

Original記事

問題

メッセージ/ラベルを英語に翻訳してHTMLタグに書き込みましたら、言語(例:日本語)を変換したでも該当メッセージ/ラベルは英語のままです。

悪い使い方

  1. 下記のコマンドを実行する。

    $('dispMsg').html(i18next.t("msg.error.fileNotFound");
    
  2. HTMLの結果。

    <div id="dispMsg">File not found.</div>
    
  3. 日本語に変換

    i18next.changeLanguage("ja");
    $('[data-i18n]').localize(); // need jQuery-i18next
    
  4. HTMLの結果(英語のまま)

    <div id="dispMsg">File not found.</div>
    

正しい使い方

  1. 下記のコマンドを実行する。

    $('#dispMsg').attr("data-i18n", "msg.error.fileNotFound")
        .localize();
    
  2. HTMLの結果。

    <div id="dispMsg" data-i18n="msg.error.fileNotFound">File not found.</div>
    
  3. 日本語に変換

    i18next.changeLanguage("ja");
    $('[data-i18n]').localize(); // need jQuery-i18next
    
  4. HTMLの結果(日本語になった!!!)

    <div id="dispMsg" data-i18n="msg.error.fileNotFound">ファイルがないです。</div>
    
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1