メインコンテンツに移動

メインナビゲーション

  • ホーム
  • サイトマップ
  • ビデオ
  • ご連絡

パンくず

  • ホーム
  • WordPressの関数:__()、_e()、_x()、および_ex()の意味および違い

WordPressの関数:__()、_e()、_x()、および_ex()の意味および違い

wordpress
hook

WordPressの関数:__()、_e()、_x()、および_ex()の意味

  • WordPressには多言語サイト(i18n)で、複数の言語翻訳インターフェースがあります
  • 言語翻訳などに関する関数がいくつかあります
    • __()
    • _e()
    • _x()
    • _ex()
    • _n()
  • 古いバージョン(バージョン2.9)までに:_c() がありますが、その後廃止され、_x()に置き換えられました。
  • __()と_e()は最も単純な関数です。
    • 変換された文字列を返す
    • 1つの文字列、1つの翻訳
  • _x()は2つ以上の言語翻訳時に使用されます
    string _x (string $text, string $context, [string $domain = 'default']) 
    • パラメータ$context: 異なるコンテキストから文字列の区別ができます
    • poファイルに違う文字列を定義します
      // .potファイルで文字列の定義
      msgctxt "test1"
      msgid "testing"
      msgstr "context1"
      
      msgctxt "test2"
      msgid "testing"
      msgstr "context2"
    • ここで、 "msgctxt" がマジックのような存在で、コンテキストへの変換ができます
      // _x()の使用
      echo 'Context: test1 -> ' . _x('testing', 'test1', 'test');
      echo '<br>Context: test2 -> ' . _x('testing', 'test2', 'test');
      
      // 上記の実行結果
      Context: test1 -> context1
      Context: test2 -> context2
      
  • 関数:_exは、_eと_xの組み合わせです。コンテキストを使用して翻訳された文字列を出力します
  • 関数:_n() は"単数"、"複数"の抽出ができます
    _n( $single, $plural, $number, $domain = 'default' )
    • ドメインが$l10nリストに設定されていない場合は、比較が行われ、いずれか$pluralまたは$singleパラメータが返されます
    • この関数はフィルタngettextを介して返され、これについては返された文字列をフィルタリングできます。
  • _n()の簡単な例は以下のようで
    $domain = 'test';
    $comment_count = 1;
    echo _n('comment', 'comments', $comment_count, $domain) . '<br/>';
    $comment_count = 2;
    echo _n('comment', 'comments', $comment_count, $domain);
    
    // 日本語環境(ja)の定義
    msgid "comment"
    msgid_plural "comments"
    msgstr[0] "コメント"
    msgstr[1] "コメント一覧"
    
    // 実行結果
    コメント
    コメント一覧
  • コメントに関する"単数形"、"複数形"の出力
ホーム

古松

検索

Article Category

  • apache(7)
  • css(19)
  • drupal(295)
  • Electron(4)
  • html(34)
  • javascript(27)
  • laravel(4)
  • linux(5)
  • macOS(2)
  • mysql(13)
  • php(19)
  • python(4)
  • SEO(12)
  • video(72)
  • Visual Studio Code(4)
  • windows(13)
  • wordpress(32)