wp/wp-includes/l10n.php
changeset 19 3d72ae0968f4
parent 18 be944660c56a
child 21 48c4eec2b7e6
equal deleted inserted replaced
18:be944660c56a 19:3d72ae0968f4
   112 /**
   112 /**
   113  * Determine the current locale desired for the request.
   113  * Determine the current locale desired for the request.
   114  *
   114  *
   115  * @since 5.0.0
   115  * @since 5.0.0
   116  *
   116  *
   117  * @global string $pagenow
   117  * @global string $pagenow The filename of the current screen.
   118  *
   118  *
   119  * @return string The determined locale.
   119  * @return string The determined locale.
   120  */
   120  */
   121 function determine_locale() {
   121 function determine_locale() {
   122 	/**
   122 	/**
   142 
   142 
   143 	if ( isset( $_GET['_locale'] ) && 'user' === $_GET['_locale'] && wp_is_json_request() ) {
   143 	if ( isset( $_GET['_locale'] ) && 'user' === $_GET['_locale'] && wp_is_json_request() ) {
   144 		$determined_locale = get_user_locale();
   144 		$determined_locale = get_user_locale();
   145 	}
   145 	}
   146 
   146 
   147 	if ( ! empty( $_GET['wp_lang'] ) && ! empty( $GLOBALS['pagenow'] ) && 'wp-login.php' === $GLOBALS['pagenow'] ) {
   147 	$wp_lang = '';
   148 		$determined_locale = sanitize_text_field( $_GET['wp_lang'] );
   148 
       
   149 	if ( ! empty( $_GET['wp_lang'] ) ) {
       
   150 		$wp_lang = sanitize_text_field( $_GET['wp_lang'] );
       
   151 	} elseif ( ! empty( $_COOKIE['wp_lang'] ) ) {
       
   152 		$wp_lang = sanitize_text_field( $_COOKIE['wp_lang'] );
       
   153 	}
       
   154 
       
   155 	if ( ! empty( $wp_lang ) && ! empty( $GLOBALS['pagenow'] ) && 'wp-login.php' === $GLOBALS['pagenow'] ) {
       
   156 		$determined_locale = $wp_lang;
   149 	}
   157 	}
   150 
   158 
   151 	/**
   159 	/**
   152 	 * Filters the locale for the current request.
   160 	 * Filters the locale for the current request.
   153 	 *
   161 	 *
   189 	$translation = apply_filters( 'gettext', $translation, $text, $domain );
   197 	$translation = apply_filters( 'gettext', $translation, $text, $domain );
   190 
   198 
   191 	/**
   199 	/**
   192 	 * Filters text with its translation for a domain.
   200 	 * Filters text with its translation for a domain.
   193 	 *
   201 	 *
   194 	 * The dynamic portion of the hook, `$domain`, refers to the text domain.
   202 	 * The dynamic portion of the hook name, `$domain`, refers to the text domain.
   195 	 *
   203 	 *
   196 	 * @since 5.5.0
   204 	 * @since 5.5.0
   197 	 *
   205 	 *
   198 	 * @param string $translation Translated text.
   206 	 * @param string $translation Translated text.
   199 	 * @param string $text        Text to translate.
   207 	 * @param string $text        Text to translate.
   257 	$translation = apply_filters( 'gettext_with_context', $translation, $text, $context, $domain );
   265 	$translation = apply_filters( 'gettext_with_context', $translation, $text, $context, $domain );
   258 
   266 
   259 	/**
   267 	/**
   260 	 * Filters text with its translation based on context information for a domain.
   268 	 * Filters text with its translation based on context information for a domain.
   261 	 *
   269 	 *
   262 	 * The dynamic portion of the hook, `$domain`, refers to the text domain.
   270 	 * The dynamic portion of the hook name, `$domain`, refers to the text domain.
   263 	 *
   271 	 *
   264 	 * @since 5.5.0
   272 	 * @since 5.5.0
   265 	 *
   273 	 *
   266 	 * @param string $translation Translated text.
   274 	 * @param string $translation Translated text.
   267 	 * @param string $text        Text to translate.
   275 	 * @param string $text        Text to translate.
   399  *
   407  *
   400  * @param string $text    Text to translate.
   408  * @param string $text    Text to translate.
   401  * @param string $context Context information for the translators.
   409  * @param string $context Context information for the translators.
   402  * @param string $domain  Optional. Text domain. Unique identifier for retrieving translated strings.
   410  * @param string $domain  Optional. Text domain. Unique identifier for retrieving translated strings.
   403  *                        Default 'default'.
   411  *                        Default 'default'.
   404  * @return string Translated context string without pipe.
       
   405  */
   412  */
   406 function _ex( $text, $context, $domain = 'default' ) {
   413 function _ex( $text, $context, $domain = 'default' ) {
   407 	echo _x( $text, $context, $domain );
   414 	echo _x( $text, $context, $domain );
   408 }
   415 }
   409 
   416 
   481 	$translation = apply_filters( 'ngettext', $translation, $single, $plural, $number, $domain );
   488 	$translation = apply_filters( 'ngettext', $translation, $single, $plural, $number, $domain );
   482 
   489 
   483 	/**
   490 	/**
   484 	 * Filters the singular or plural form of a string for a domain.
   491 	 * Filters the singular or plural form of a string for a domain.
   485 	 *
   492 	 *
   486 	 * The dynamic portion of the hook, `$domain`, refers to the text domain.
   493 	 * The dynamic portion of the hook name, `$domain`, refers to the text domain.
   487 	 *
   494 	 *
   488 	 * @since 5.5.0
   495 	 * @since 5.5.0
   489 	 *
   496 	 *
   490 	 * @param string $translation Translated text.
   497 	 * @param string $translation Translated text.
   491 	 * @param string $single      The text to be used if the number is singular.
   498 	 * @param string $single      The text to be used if the number is singular.
   541 	$translation = apply_filters( 'ngettext_with_context', $translation, $single, $plural, $number, $context, $domain );
   548 	$translation = apply_filters( 'ngettext_with_context', $translation, $single, $plural, $number, $context, $domain );
   542 
   549 
   543 	/**
   550 	/**
   544 	 * Filters the singular or plural form of a string with gettext context for a domain.
   551 	 * Filters the singular or plural form of a string with gettext context for a domain.
   545 	 *
   552 	 *
   546 	 * The dynamic portion of the hook, `$domain`, refers to the text domain.
   553 	 * The dynamic portion of the hook name, `$domain`, refers to the text domain.
   547 	 *
   554 	 *
   548 	 * @since 5.5.0
   555 	 * @since 5.5.0
   549 	 *
   556 	 *
   550 	 * @param string $translation Translated text.
   557 	 * @param string $translation Translated text.
   551 	 * @param string $single      The text to be used if the number is singular.
   558 	 * @param string $single      The text to be used if the number is singular.
   623  * @param string $domain   Optional. Text domain. Unique identifier for retrieving translated strings.
   630  * @param string $domain   Optional. Text domain. Unique identifier for retrieving translated strings.
   624  *                         Default null.
   631  *                         Default null.
   625  * @return array {
   632  * @return array {
   626  *     Array of translation information for the strings.
   633  *     Array of translation information for the strings.
   627  *
   634  *
   628  *     @type string $0        Singular form to be localized. No longer used.
   635  *     @type string      $0        Singular form to be localized. No longer used.
   629  *     @type string $1        Plural form to be localized. No longer used.
   636  *     @type string      $1        Plural form to be localized. No longer used.
   630  *     @type string $2        Context information for the translators. No longer used.
   637  *     @type string      $2        Context information for the translators. No longer used.
   631  *     @type string $singular Singular form to be localized.
   638  *     @type string      $singular Singular form to be localized.
   632  *     @type string $plural   Plural form to be localized.
   639  *     @type string      $plural   Plural form to be localized.
   633  *     @type string $context  Context information for the translators.
   640  *     @type string      $context  Context information for the translators.
   634  *     @type string $domain   Text domain.
   641  *     @type string|null $domain   Text domain.
   635  * }
   642  * }
   636  */
   643  */
   637 function _nx_noop( $singular, $plural, $context, $domain = null ) {
   644 function _nx_noop( $singular, $plural, $context, $domain = null ) {
   638 	return array(
   645 	return array(
   639 		0          => $singular,
   646 		0          => $singular,
  1479  *
  1486  *
  1480  * @since 4.0.0
  1487  * @since 4.0.0
  1481  * @since 4.3.0 Introduced the `echo` argument.
  1488  * @since 4.3.0 Introduced the `echo` argument.
  1482  * @since 4.7.0 Introduced the `show_option_site_default` argument.
  1489  * @since 4.7.0 Introduced the `show_option_site_default` argument.
  1483  * @since 5.1.0 Introduced the `show_option_en_us` argument.
  1490  * @since 5.1.0 Introduced the `show_option_en_us` argument.
       
  1491  * @since 5.9.0 Introduced the `explicit_option_en_us` argument.
  1484  *
  1492  *
  1485  * @see get_available_languages()
  1493  * @see get_available_languages()
  1486  * @see wp_get_available_translations()
  1494  * @see wp_get_available_translations()
  1487  *
  1495  *
  1488  * @param string|array $args {
  1496  * @param string|array $args {
  1498  *     @type bool|int $echo                         Whether to echo the generated markup. Accepts 0, 1, or their
  1506  *     @type bool|int $echo                         Whether to echo the generated markup. Accepts 0, 1, or their
  1499  *                                                  boolean equivalents. Default 1.
  1507  *                                                  boolean equivalents. Default 1.
  1500  *     @type bool     $show_available_translations  Whether to show available translations. Default true.
  1508  *     @type bool     $show_available_translations  Whether to show available translations. Default true.
  1501  *     @type bool     $show_option_site_default     Whether to show an option to fall back to the site's locale. Default false.
  1509  *     @type bool     $show_option_site_default     Whether to show an option to fall back to the site's locale. Default false.
  1502  *     @type bool     $show_option_en_us            Whether to show an option for English (United States). Default true.
  1510  *     @type bool     $show_option_en_us            Whether to show an option for English (United States). Default true.
       
  1511  *     @type bool     $explicit_option_en_us        Whether the English (United States) option uses an explicit value of en_US
       
  1512  *                                                  instead of an empty value. Default false.
  1503  * }
  1513  * }
  1504  * @return string HTML dropdown list of languages.
  1514  * @return string HTML dropdown list of languages.
  1505  */
  1515  */
  1506 function wp_dropdown_languages( $args = array() ) {
  1516 function wp_dropdown_languages( $args = array() ) {
  1507 
  1517 
  1515 			'selected'                    => '',
  1525 			'selected'                    => '',
  1516 			'echo'                        => 1,
  1526 			'echo'                        => 1,
  1517 			'show_available_translations' => true,
  1527 			'show_available_translations' => true,
  1518 			'show_option_site_default'    => false,
  1528 			'show_option_site_default'    => false,
  1519 			'show_option_en_us'           => true,
  1529 			'show_option_en_us'           => true,
       
  1530 			'explicit_option_en_us'       => false,
  1520 		)
  1531 		)
  1521 	);
  1532 	);
  1522 
  1533 
  1523 	// Bail if no ID or no name.
  1534 	// Bail if no ID or no name.
  1524 	if ( ! $parsed_args['id'] || ! $parsed_args['name'] ) {
  1535 	if ( ! $parsed_args['id'] || ! $parsed_args['name'] ) {
  1525 		return;
  1536 		return;
  1526 	}
  1537 	}
  1527 
  1538 
  1528 	// English (United States) uses an empty string for the value attribute.
  1539 	// English (United States) uses an empty string for the value attribute.
  1529 	if ( 'en_US' === $parsed_args['selected'] ) {
  1540 	if ( 'en_US' === $parsed_args['selected'] && ! $parsed_args['explicit_option_en_us'] ) {
  1530 		$parsed_args['selected'] = '';
  1541 		$parsed_args['selected'] = '';
  1531 	}
  1542 	}
  1532 
  1543 
  1533 	$translations = $parsed_args['translations'];
  1544 	$translations = $parsed_args['translations'];
  1534 	if ( empty( $translations ) ) {
  1545 	if ( empty( $translations ) ) {
  1579 			_x( 'Site Default', 'default site language' )
  1590 			_x( 'Site Default', 'default site language' )
  1580 		);
  1591 		);
  1581 	}
  1592 	}
  1582 
  1593 
  1583 	if ( $parsed_args['show_option_en_us'] ) {
  1594 	if ( $parsed_args['show_option_en_us'] ) {
       
  1595 		$value       = ( $parsed_args['explicit_option_en_us'] ) ? 'en_US' : '';
  1584 		$structure[] = sprintf(
  1596 		$structure[] = sprintf(
  1585 			'<option value="" lang="en" data-installed="1"%s>English (United States)</option>',
  1597 			'<option value="%s" lang="en" data-installed="1"%s>English (United States)</option>',
       
  1598 			esc_attr( $value ),
  1586 			selected( '', $parsed_args['selected'], false )
  1599 			selected( '', $parsed_args['selected'], false )
  1587 		);
  1600 		);
  1588 	}
  1601 	}
  1589 
  1602 
  1590 	// List installed languages.
  1603 	// List installed languages.
  1711 	/* @var WP_Locale_Switcher $wp_locale_switcher */
  1724 	/* @var WP_Locale_Switcher $wp_locale_switcher */
  1712 	global $wp_locale_switcher;
  1725 	global $wp_locale_switcher;
  1713 
  1726 
  1714 	return $wp_locale_switcher->is_switched();
  1727 	return $wp_locale_switcher->is_switched();
  1715 }
  1728 }
       
  1729 
       
  1730 /**
       
  1731  * Translates the provided settings value using its i18n schema.
       
  1732  *
       
  1733  * @since 5.9.0
       
  1734  * @access private
       
  1735  *
       
  1736  * @param string|string[]|array[]|object $i18n_schema I18n schema for the setting.
       
  1737  * @param string|string[]|array[]        $settings    Value for the settings.
       
  1738  * @param string                         $textdomain  Textdomain to use with translations.
       
  1739  *
       
  1740  * @return string|string[]|array[] Translated settings.
       
  1741  */
       
  1742 function translate_settings_using_i18n_schema( $i18n_schema, $settings, $textdomain ) {
       
  1743 	if ( empty( $i18n_schema ) || empty( $settings ) || empty( $textdomain ) ) {
       
  1744 		return $settings;
       
  1745 	}
       
  1746 
       
  1747 	if ( is_string( $i18n_schema ) && is_string( $settings ) ) {
       
  1748 		return translate_with_gettext_context( $settings, $i18n_schema, $textdomain );
       
  1749 	}
       
  1750 	if ( is_array( $i18n_schema ) && is_array( $settings ) ) {
       
  1751 		$translated_settings = array();
       
  1752 		foreach ( $settings as $value ) {
       
  1753 			$translated_settings[] = translate_settings_using_i18n_schema( $i18n_schema[0], $value, $textdomain );
       
  1754 		}
       
  1755 		return $translated_settings;
       
  1756 	}
       
  1757 	if ( is_object( $i18n_schema ) && is_array( $settings ) ) {
       
  1758 		$group_key           = '*';
       
  1759 		$translated_settings = array();
       
  1760 		foreach ( $settings as $key => $value ) {
       
  1761 			if ( isset( $i18n_schema->$key ) ) {
       
  1762 				$translated_settings[ $key ] = translate_settings_using_i18n_schema( $i18n_schema->$key, $value, $textdomain );
       
  1763 			} elseif ( isset( $i18n_schema->$group_key ) ) {
       
  1764 				$translated_settings[ $key ] = translate_settings_using_i18n_schema( $i18n_schema->$group_key, $value, $textdomain );
       
  1765 			} else {
       
  1766 				$translated_settings[ $key ] = $value;
       
  1767 			}
       
  1768 		}
       
  1769 		return $translated_settings;
       
  1770 	}
       
  1771 	return $settings;
       
  1772 }
       
  1773 
       
  1774 /**
       
  1775  * Retrieves the list item separator based on the locale.
       
  1776  *
       
  1777  * @since 6.0.0
       
  1778  *
       
  1779  * @global WP_Locale $wp_locale WordPress date and time locale object.
       
  1780  *
       
  1781  * @return string Locale-specific list item separator.
       
  1782  */
       
  1783 function wp_get_list_item_separator() {
       
  1784 	global $wp_locale;
       
  1785 
       
  1786 	return $wp_locale->get_list_item_separator();
       
  1787 }