如何解决字符串应该在Wordpress中具有可翻译的内容?



我正在尝试解决这里的 linting 问题是代码。

function get_the_breadcrumb() {
if ( ! is_front_page() ) {
// Start the breadcrumb with a link to your homepage.
echo '<div class="o__breadcrumb">';
echo '<a href="';
echo esc_html( get_option( 'home' ) );
echo '"> Home';
echo '</a> <span> ';
echo esc_html( Load::atom( 'icons/breadcrumb_arrow' ) );
echo '</span>';
// Check if the current page is a category, an archive or a single page. If so show the category or archive name.
if ( is_category() || is_single() ) {
the_category( 'title_li=' );
} elseif ( is_archive() || is_single() ) {
if ( is_day() ) {
/* translators: %s: text term */
printf( esc_html( __( '%s', 'text_domain' ) ), esc_html( get_the_date() ) );
} elseif ( is_month() ) {
/* translators: %s: text term */
printf( esc_html( __( '%s', 'text_domain' ) ), get_the_date( _x( 'F Y', 'monthly archives date format', 'text_domain' ) ) );
} elseif ( is_year() ) {
/* translators: %s: text term */
printf( esc_html( __( '%s', 'text_domain' ) ), get_the_date( _x( 'Y', 'yearly archives date format', 'text_domain' ) ) );
} else {
esc_attr_e( 'Blog Archives', 'text_domain' );
}
}
// If the current page is a single post, show its title with the separator.
if ( is_single() ) {
echo '<span>';
echo esc_html( Load::atom( 'icons/breadcrumb_arrow' ) );
echo '</span>';
the_title();
}
// If the current page is a static page, show its title.
if ( is_page() ) {
echo the_title();
}
// if you have a static page assigned to be you posts list page. It will find the title of the static page and display it. i.e Home >> Blog.
if ( is_home() ) {
global $post;
$page_for_posts_id = get_option( 'page_for_posts' );
if ( $page_for_posts_id ) {
$post = get_page( $page_for_posts_id );
setup_postdata( $post );
the_title();
rewind_posts();
}
}
echo '</div>';
}
}

林廷响应

FOUND 3 ERRORS AFFECTING 3 LINES
----------------------------------------------------------------------
193 | ERROR | Strings should have translatable content
196 | ERROR | Strings should have translatable content
199 | ERROR | Strings should have translatable content

行号 193

printf( esc_html( __( '%s', 'text_domain' ) ), esc_html( get_the_date() ) );

行号 196

printf( esc_html( __( '%s', 'text_domain' ) ), get_the_date( _x( 'F Y', 'monthly archives date format', 'text_domain' ) ) );

行号 199

printf( esc_html( __( '%s', 'text_domain' ) ), get_the_date( _x( 'Y', 'yearly archives date format', 'text_domain' ) ) );

这是因为您%s作为翻译函数中的文本调用__(...),这是不可翻译的。

相反,你应该在翻译调用中调用printf(),这样翻译人员就可以真正看到你到底想翻译什么。

但是,您不应该尝试以这种方式翻译日期。它不起作用,因为日期总是在变化,__()翻译的工作方式是匹配传入翻译的确切字符串。根据这个答案,你应该使用date_i18n

以及为什么要尝试翻译要传递给get_the_date的日期格式字符串,这些是 php 使用的代码值,它们不会根据您所在的位置而更改。翻译这些只会给你带来问题。

您还在第 193 行拨打esc_html两次电话。

因此,您应该像这样编写代码行:

行号 193

esc_html( date_i18n( get_the_date() ) );

行号 196

esc_html( date_i18n( get_the_date('F Y') ) );

行号 199

esc_html( date_i18n( get_the_date( 'Y' ) ) );

请注意,我认为这里实际上不需要esc_html调用,因为内部只是只返回日期的 WordPress 函数......那里不应该有 HTML

最新更新