返回介绍

_n()

发布于 2017-09-11 13:27:50 字数 6363 浏览 1261 评论 0 收藏 0

_n( string $single,  string $plural,  int $number,  string $domain = 'default' )

Translates and retrieves the singular or plural form based on the supplied number.


description

Used when you want to use the appropriate form of a string based on whether a number is singular or plural.

Example:

printf( _n( '%s person', '%s people', $count, 'text-domain' ), number_format_i18n( $count ) );

参数

$single

(string) (Required) The text to be used if the number is singular.

$plural

(string) (Required) The text to be used if the number is plural.

$number

(int) (Required) The number to compare against to use either the singular or plural form.

$domain

(string) (Optional) Text domain. Unique identifier for retrieving translated strings.

Default value: 'default'


返回值

(string) The translated singular or plural form.


源代码

File: wp-includes/l10n.php

function _n( $single, $plural, $number, $domain = 'default' ) {
	$translations = get_translations_for_domain( $domain );
	$translation  = $translations->translate_plural( $single, $plural, $number );

	/**
	 * Filters the singular or plural form of a string.
	 *
	 * @since 2.2.0
	 *
	 * @param string $translation Translated text.
	 * @param string $single      The text to be used if the number is singular.
	 * @param string $plural      The text to be used if the number is plural.
	 * @param string $number      The number to compare against to use either the singular or plural form.
	 * @param string $domain      Text domain. Unique identifier for retrieving translated strings.
	 */
	return apply_filters( 'ngettext', $translation, $single, $plural, $number, $domain );
}

更新日志

Versiondescription
2.8.0Introduced.

相关函数

Uses

  • wp-includes/l10n.php: get_translations_for_domain()
  • wp-includes/l10n.php: ngettext
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/pomo/translations.php: Translations::translate_plural()

Used By

  • wp-includes/class-wp-customize-manager.php: WP_Customize_Manager::save_changeset_post()
  • wp-includes/customize/class-wp-customize-custom-css-setting.php: WP_Customize_Custom_CSS_Setting::validate()
  • wp-includes/embed.php: print_embed_comments_button()
  • wp-includes/class-wp-customize-nav-menus.php: WP_Customize_Nav_Menus::customize_register()
  • wp-includes/comment-template.php: get_comments_number_text()
  • wp-admin/includes/class-wp-ms-users-list-table.php: WP_MS_Users_List_Table::get_views()
  • wp-admin/includes/class-wp-screen.php: WP_Screen::render_screen_layout()
  • wp-admin/includes/class-wp-plugins-list-table.php: WP_Plugins_List_Table::get_views()
  • wp-admin/includes/class-wp-list-table.php: WP_List_Table::ajax_response()
  • wp-admin/includes/class-wp-list-table.php: WP_List_Table::comments_bubble()
  • wp-admin/includes/class-wp-list-table.php: WP_List_Table::pagination()
  • wp-admin/includes/class-wp-ms-themes-list-table.php: WP_MS_Themes_List_Table::get_views()
  • wp-admin/includes/plugin-install.php: install_plugin_information()
  • wp-admin/includes/dashboard.php: wp_network_dashboard_right_now()
  • wp-admin/includes/dashboard.php: wp_dashboard_right_now()
  • wp-admin/includes/template.php: wp_star_rating()
  • wp-admin/includes/class-wp-users-list-table.php: WP_Users_List_Table::single_row()
  • wp-admin/includes/ajax-actions.php: _wp_ajax_delete_comment_response()
  • wp-admin/includes/ajax-actions.php: wp_ajax_replyto_comment()
  • wp-includes/l10n.php: translate_nooped_plural()
  • wp-includes/formatting.php: human_time_diff()
  • wp-includes/pluggable.php: wp_notify_moderator()
  • wp-includes/deprecated.php: _nc()
  • wp-includes/update.php: wp_get_update_data()
  • wp-includes/admin-bar.php: wp_admin_bar_comments_menu()
  • wp-includes/ms-functions.php: wpmu_validate_blog_signup()
  • wp-includes/comment-template.php: comments_popup_link()
  • wp-includes/class-wp-customize-widgets.php: WP_Customize_Widgets::enqueue_scripts()
  • Show 23 more used by Hide more used by

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 1You must log in to vote on the helpfulness of this note Contributed by Codex

    Display either “1 star” or “x stars” for a star rating plugin.

    
    $rating = '3';
    
    $text = sprintf( _n( '%s star', '%s stars', $rating, 'wpdocs_textdomain' ), $rating );
    
    // "3 stars"
    echo $text;
    

    Important: Never do a calculation inside the sprintf() function! The following won’t work:

    
    $text = sprintf( 
    	_n( '%s star', '%s stars', $rating, 'wpdocs_textdomain' ), 
    	2 <= $rating ? $rating -1 : $rating
    );
    
    
  2. Example from /wp-admin/edit-comments.php without the use of a text domain for translation.

    
    if ( $approved > 0 ) {
    	$messages[] = sprintf( _n( '%s comment approved', '%s comments approved', $approved ), $approved );
    }
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文