comments.php
<?php
/**
* The template for displaying comments
*
* This is the template that displays the area of the page that contains both the current comments
* and the comment form.
*
* @link https://developer.wordpress.org/themes/basics/template-hierarchy/
*
* @package _s
*/
/*
* If the current post is protected by a password and
* the visitor has not yet entered the password we will
* return early without loading the comments.
*/
if ( post_password_required() ) {
return;
}
?>
<div id="comments" class="comments-area">
<?php
// You can start editing here -- including this comment!
if ( have_comments() ) :
?>
<h2 class="comments-title">
<?php
$_s_comment_count = get_comments_number();
if ( '1' === $_s_comment_count ) {
printf(
/* translators: 1: title. */
esc_html__( 'One thought on “%1$s”', '_s' ),
'<span>' . get_the_title() . '</span>'
);
} else {
printf( // WPCS: XSS OK.
/* translators: 1: comment count number, 2: title. */
esc_html( _nx( '%1$s thought on “%2$s”', '%1$s thoughts on “%2$s”', $_s_comment_count, 'comments title', '_s' ) ),
number_format_i18n( $_s_comment_count ),
'<span>' . get_the_title() . '</span>'
);
}
?>
</h2><!-- .comments-title -->
<?php the_comments_navigation(); ?>
<ol class="comment-list">
<?php
wp_list_comments( array(
'style' => 'ol',
'short_ping' => true,
) );
?>
</ol><!-- .comment-list -->
<?php
the_comments_navigation();
// If comments are closed and there are comments, let's leave a little note, shall we?
if ( ! comments_open() ) :
?>
<p class="no-comments"><?php esc_html_e( 'Comments are closed.', '_s' ); ?></p>
<?php
endif;
endif; // Check for have_comments().
comment_form();
?>
</div><!-- #comments -->
32行目から45行目あたりが意味が分からないです。
_nx ()
https://developer.wordpress.org/reference/functions/_nx/
_nx()は、_n()と_x()のハイブリッド関数です。_n()は英語の単数形と複数形の単語を数に合わせて返してくれる関数で、_x()はコンテクストに合わせて翻訳を返す関数です。
_n()
https://developer.wordpress.org/reference/functions/_n/
_n( '%s comment', '%s comments', $count)
例えば上だと、コメント一つであればcomment、二つ以上の時はcommentsという単語を返してくれるので、自然な英単語になります。日本語ではあまり使わなさそうです。
_x()
https://developer.wordpress.org/reference/functions/_x/
読んでも今一つうまく理解できないので、以下を参考にしました。
https://wpengineer.com/2237/whats-the-difference-between-__-_e-_x-and-_ex/
WordPressの翻訳は、gettexitという仕組みを利用しておこなれています。poファイルというファイルを作って、poファイルをコンパイルしたmoファイルを使って翻訳を行っているそうです。
poファイルでは、言葉をmsgidとmsgstrの一対一で通常は対応させるのですが、言語によって難しい場合、さらにコンテクストとしてmsgctxtを追加することで、同じmsgidに異なるmsgstrを対応させることができます。
number_format_i18n()
https://developer.wordpress.org/reference/functions/number_format_i18n/
数字をロケールに基づいた数字の文字列に変換して返してくれる関数。1000だったら、1,000にしてくれます。
このコードの_nxのコンテクスト ‘comments title’ は一体何?
このコードでは、コンテクストが’comments title’となっています。一体どういう状況を想定しているのか、よく意味が分かりません。githubでこの変更がマージされているところを読みましたが、複数形を色々と持つ言語に対応するために_n()ではなく_nx()を使うようになったそうです。例がうまく思いつかないので ‘comments title’ については考えるのをやめます。
なぜifでコメントが一つの場合を処理して、_nxでさらに単数形複数形の判定を行うか?
else部分で再度コメント一つの場合を処理しているように見える部分も、考えてもよくわらかないので、githubで変更がマージされている部分を読みました。単数形を他の数に対しても用いる言語のためにこの変更がなされたそうです。
‘1’ === $_s_comment_count
get_comments_number は、コメントがある場合は数字の文字列を返すそうです。