load_child_theme_textdomain()


WordPress関数load_child_theme_textdomain()は、子テーマの翻訳ファイルを読み込むために使用されます。

シンタックス

load_child_theme_textdomain( string $domain, string $path = '' );

引数の説明:

  • $domain (string) — 翻訳ドメイン名。
  • $path (string) — 翻訳ファイルが格納されているディレクトリのパス。省略可能で、デフォルトはget_stylesheet_directory() . '/languages'

例 1: 子テーマのデフォルト翻訳ファイルを読み込む

子テーマのlanguagesフォルダに翻訳ファイルがある場合の基本的な使用例です。

<?php
function my_child_theme_setup() {
    load_child_theme_textdomain( 'my-child-theme' );
}
add_action( 'after_setup_theme', 'my_child_theme_setup' );
?>

例 2: カスタムパスを指定して翻訳ファイルを読み込む

翻訳ファイルが別のディレクトリにある場合。

<?php
function my_custom_language_path() {
    load_child_theme_textdomain( 'my-child-theme', get_stylesheet_directory() . '/custom-languages' );
}
add_action( 'after_setup_theme', 'my_custom_language_path' );
?>

例 3: 親テーマと子テーマの翻訳ファイルを両方サポート

親テーマと子テーマの両方から翻訳を使用する場合。

<?php
function load_parent_and_child_textdomains() {
    load_theme_textdomain( 'my-parent-theme', get_template_directory() . '/languages' );
    load_child_theme_textdomain( 'my-child-theme' );
}
add_action( 'after_setup_theme', 'load_parent_and_child_textdomains' );
?>

例 4: 管理画面での翻訳をサポート

管理画面専用の翻訳をサポートする例です。

<?php
function admin_textdomain_setup() {
    load_child_theme_textdomain( 'my-child-theme-admin', get_stylesheet_directory() . '/admin-languages' );
}
add_action( 'admin_init', 'admin_textdomain_setup' );
?>

例 5: 動的に翻訳ドメインを切り替える

条件に応じて翻訳ドメインを動的に切り替える場合。

<?php
function conditional_textdomain_load() {
    if ( is_admin() ) {
        load_child_theme_textdomain( 'admin-text', get_stylesheet_directory() . '/admin-languages' );
    } else {
        load_child_theme_textdomain( 'front-text', get_stylesheet_directory() . '/front-languages' );
    }
}
add_action( 'after_setup_theme', 'conditional_textdomain_load' );
?>

注意事項

  • 翻訳ファイルの命名規則は[domain]-[locale].moである必要があります。
  • 翻訳ドメインが一意であることを確認してください。他のテーマやプラグインと競合する可能性があります。
  • after_setup_themeフックを使用することで、テーマの初期化中に翻訳を読み込むことができます。

関連機能: