返回介绍

wp_dropdown_languages()

发布于 2017-09-11 11:54:24 字数 6060 浏览 1031 评论 0 收藏 0

wp_dropdown_languages( string|array $args = array() )

Language selector.


description


参数

$args

(string|array) (Optional) Array or string of arguments for outputting the language selector.

  • 'id'
    (string) ID attribute of the select element.
  • 'name'
    (string) Name attribute of the select element.
  • 'languages'
    (array) List of installed languages, contain only the locales.
  • 'translations'
    (array) List of available translations. Default result of wp_get_available_translations().
  • 'selected'
    (string) Language which should be selected.
  • 'echo'
    (bool|int) Whether to echo the generated markup. Accepts 0, 1, or their boolean equivalents. Default 1.
  • 'show_available_translations'
    (bool) Whether to show available translations. Default true.
  • 'show_option_site_default'
    (bool) Whether to show an option to fall back to the site's locale. Default false.

Default value: array()


返回值

(string) HTML content


源代码

File: wp-includes/l10n.php

function wp_dropdown_languages( $args = array() ) {

	$args = wp_parse_args( $args, array(
		'id'           => '',
		'name'         => '',
		'languages'    => array(),
		'translations' => array(),
		'selected'     => '',
		'echo'         => 1,
		'show_available_translations' => true,
		'show_option_site_default'    => false,
	) );

	// English (United States) uses an empty string for the value attribute.
	if ( 'en_US' === $args['selected'] ) {
		$args['selected'] = '';
	}

	$translations = $args['translations'];
	if ( empty( $translations ) ) {
		require_once( ABSPATH . 'wp-admin/includes/translation-install.php' );
		$translations = wp_get_available_translations();
	}

	/*
	 * $args['languages'] should only contain the locales. Find the locale in
	 * $translations to get the native name. Fall back to locale.
	 */
	$languages = array();
	foreach ( $args['languages'] as $locale ) {
		if ( isset( $translations[ $locale ] ) ) {
			$translation = $translations[ $locale ];
			$languages[] = array(
				'language'    => $translation['language'],
				'native_name' => $translation['native_name'],
				'lang'        => current( $translation['iso'] ),
			);

			// Remove installed language from available translations.
			unset( $translations[ $locale ] );
		} else {
			$languages[] = array(
				'language'    => $locale,
				'native_name' => $locale,
				'lang'        => '',
			);
		}
	}

	$translations_available = ( ! empty( $translations ) && $args['show_available_translations'] );

	$output = sprintf( '<select name="%s" id="%s">', esc_attr( $args['name'] ), esc_attr( $args['id'] ) );

	// Holds the HTML markup.
	$structure = array();

	// List installed languages.
	if ( $translations_available ) {
		$structure[] = '<optgroup label="' . esc_attr_x( 'Installed', 'translations' ) . '">';
	}

	if ( $args['show_option_site_default'] ) {
		$structure[] = sprintf(
			'<option value="site-default" data-installed="1"%s>%s</option>',
			selected( 'site-default', $args['selected'], false ),
			_x( 'Site Default', 'default site language' )
		);
	}

	$structure[] = sprintf(
		'<option value="" lang="en" data-installed="1"%s>English (United States)</option>',
		selected( '', $args['selected'], false )
	);

	foreach ( $languages as $language ) {
		$structure[] = sprintf(
			'<option value="%s" lang="%s"%s data-installed="1">%s</option>',
			esc_attr( $language['language'] ),
			esc_attr( $language['lang'] ),
			selected( $language['language'], $args['selected'], false ),
			esc_html( $language['native_name'] )
		);
	}
	if ( $translations_available ) {
		$structure[] = '</optgroup>';
	}

	// List available translations.
	if ( $translations_available ) {
		$structure[] = '<optgroup label="' . esc_attr_x( 'Available', 'translations' ) . '">';
		foreach ( $translations as $translation ) {
			$structure[] = sprintf(
				'<option value="%s" lang="%s"%s>%s</option>',
				esc_attr( $translation['language'] ),
				esc_attr( current( $translation['iso'] ) ),
				selected( $translation['language'], $args['selected'], false ),
				esc_html( $translation['native_name'] )
			);
		}
		$structure[] = '</optgroup>';
	}

	$output .= join( "\n", $structure );

	$output .= '</select>';

	if ( $args['echo'] ) {
		echo $output;
	}

	return $output;
}

更新日志

Versiondescription
4.7.0Introduced the show_option_site_default argument.
4.3.0Introduced the echo argument.
4.0.0Introduced.

相关函数

Uses

  • wp-admin/includes/translation-install.php: wp_get_available_translations()
  • wp-includes/l10n.php: esc_attr_x()
  • wp-includes/l10n.php: _x()
  • wp-includes/formatting.php: esc_attr()
  • wp-includes/formatting.php: esc_html()
  • wp-includes/general-template.php: selected()
  • wp-includes/functions.php: wp_parse_args()
  • Show 2 more uses Hide more uses

Used By

  • wp-signup.php: show_blog_form()

User Contributed Notes

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

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

发布评论

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