get_translations_for_domain()


WordPressのget_translations_for_domain()関数は、指定したテキストドメインに関連付けられたすべての翻訳データを取得するために使用されます。この関数は、特定のドメインに登録されている翻訳テーブルを返します。

シンタックス

get_translations_for_domain( string $domain );
  • $domain (string) — 翻訳データを取得する対象のテキストドメインを指定します。

例1: 指定したドメインの翻訳データを取得する

以下のコードは、my-text-domainの翻訳データを取得して表示します。

<?php 
$translations = get_translations_for_domain( 'my-text-domain' ); 
var_dump( $translations );
?>

例2: 翻訳キーの存在を確認する

指定したキーが翻訳ドメインに登録されているかを確認します。

<?php 
$translations = get_translations_for_domain( 'my-text-domain' ); 
if ( isset( $translations->entries['hello'] ) ) { 
    echo '翻訳キーが存在します。'; 
} 
?>

例3: 翻訳テキストを取得する

特定のキーに対応する翻訳テキストを取得します。

<?php 
$translations = get_translations_for_domain( 'my-text-domain' ); 
if ( isset( $translations->entries['welcome'] ) ) { 
    echo $translations->entries['welcome']->translations[0]; 
} 
?>

例4: すべての翻訳キーをループ処理する

指定したドメインのすべての翻訳キーを表示します。

<?php 
$translations = get_translations_for_domain( 'my-text-domain' ); 
foreach ( $translations->entries as $key => $entry ) { 
    echo $key . ': ' . $entry->translations[0] . '<br>'; 
} 
?>

例5: 翻訳データが空の場合の確認

ドメインに登録されている翻訳が空であるかを確認します。

<?php 
$translations = get_translations_for_domain( 'empty-domain' ); 
if ( empty( $translations->entries ) ) { 
    echo '翻訳データが存在しません。'; 
} 
?>

注意事項

  • テキストドメインが正確でない場合、関数は空の結果を返す可能性があります。
  • 翻訳データを変更する必要がある場合は、適切なフックを使用してください。

関連機能: