tl; dr:私のプラグインはコアまたは_n_noop()を使用する他のプラグインから文字列を翻訳したいです。どうやって?
この質問は このプラグイン に関連しており、 この質問へのフォローアップのようなものです .
私のプラグインは "{status}(n)"(例えば "Drafts(10)")をどの投稿タイプの管理メニューにも追加します。 register_post_status()
関数を使ってすべてのステータスを取得するので、get_post_stati()
で追加された投稿ステータスをサポートします(イライラするほど似たget_post_statuses()
ではなく)。
誰かがregister_post_status()
に投稿ステータスを登録するとき、label_count
に$args
値を含めます。これは私が必要としているものに最適です。私はいつもメニューに複数形のラベル数を表示し、カウント値をドロップするためにsprintf()
を使いたいです。
これは現在のプラグインコードの抜粋です(最初のsprintf()
は私が変更しようとしているものです)。
<?php
// an array of all the statuses
$ps_statuses = get_post_stati( array( 'show_in_admin_status_list' => true ), 'objects' );
// a filter for the list of statuses for anything people want to do that's plugin specific
$ps_statuses = apply_filters( 'psmi_statuses', $ps_statuses );
// Get status counts of all post types
$ps_status_counts = wp_count_posts( $ps_type_id );
foreach( $ps_statuses as $status ) {
$ps_status_id = $status->name;
$ps_status_count = $ps_status_counts -> $ps_status_id;
// If a status has any posts, show it
if( $ps_status_count > 0 ) {
// Get the plural post status label
$ps_status_label = $status->label_count['plural'];
$submenu[$menu][] = array(
sprintf(
$ps_status_label,
$ps_status_count
),
'read',
sprintf(
'%1$sedit.php?post_status=%2$s&post_type=%3$s',
get_admin_url(),
$ps_status_id,
$ps_type_id
)
);
}
}
これはうまくいきますが、翻訳できません。
WordPressがpost statiをcore(/wp-includes/post.php
)に登録すると、_n_noop()
関数を使用して複数の翻訳が可能になります。
register_post_status( 'draft', array(
'label' => _x( 'Draft', 'post' ),
'protected' => true,
'_builtin' => true, /* internal use only. */
'label_count' => _n_noop( 'Draft <span class="count">(%s)</span>', 'Drafts <span class="count">(%s)</span>' ),
) );
しかし、翻訳された文字列を機能させる方法を私は理解することができませんでした。私はtranslate_nooped_plural()
を使う必要があると思いますが、それをうまく動かすことができませんでした(そして、実際には、1つのステータスに投稿が1つしかない場合でも複数の文字列を使いたいだけです)。第二に、ステータスはコア、カスタムコール、register_post_status()
、または編集フローのようなプラグインから来ている可能性があるので、私はどのテキストドメインを使用すべきか本当に迷っています。
// Get the plural post status label
$ps_status_label = $status->label_count;
$submenu[$menu][] = array(
sprintf(
translate_nooped_plural(
$ps_status_label,
$ps_status_count
),
$ps_status_count
),
これはコアの翻訳では期待通りに動作するはずです。これでテキストドメインが必要になりました。翻訳したい文字列を検索しましょう。
foreach ( $GLOBALS['l10n'] as $domain => $data ) {
if ( in_array( $ps_status_label[0], array_keys( $data->entries ) ) )
break;
$domain = '';
}
$domain = ( ! empty( $domain ) ) ? $domain : 'default';
そして今、あなたは最初のコードスニペットをに変更することができます
// Get the plural post status label
$ps_status_label = $status->label_count;
$submenu[$menu][] = array(
sprintf(
translate_nooped_plural(
$ps_status_label,
$ps_status_count,
$domain
),
$ps_status_count
),
複数形の投稿ステータスのみが必要です。translate_nooped_plural()
のcount値を増やすだけです。
sprintf(
translate_nooped_plural(
$ps_status_label,
10,
$domain
),
$ps_status_count
),
カウント値が1より大きいため、これは常に複数形を返します。3より大きい値を使用するようにしてください。言語によっては、1、2、またはそれ以上の場合で違いが生じることがあります。
明確にするために:
translate_nooped_plural(
[array(
[string for singular]
[string for plural]
),
[count] (1 = select singular string, greater than 1 select the plural string)
[domain]
)