返回介绍

translations_api()

发布于 2017-09-11 10:42:47 字数 6077 浏览 1085 评论 0 收藏 0

translations_api( string $type,  array|object $args = null )

Retrieve translations from WordPress Translation API.


description


参数

$type

(string) (Required) Type of translations. Accepts 'plugins', 'themes', 'core'.

$args

(array|object) (Optional) Translation API arguments. Optional.

Default value: null


返回值

(object|WP_Error) On success an object of translations, WP_Error on failure.


源代码

File: wp-admin/includes/translation-install.php

function translations_api( $type, $args = null ) {
	include( ABSPATH . WPINC . '/version.php' ); // include an unmodified $wp_version

	if ( ! in_array( $type, array( 'plugins', 'themes', 'core' ) ) ) {
		return	new WP_Error( 'invalid_type', __( 'Invalid translation type.' ) );
	}

	/**
	 * Allows a plugin to override the WordPress.org Translation Install API entirely.
	 *
	 * @since 4.0.0
	 *
	 * @param bool|array  $result The result object. Default false.
	 * @param string      $type   The type of translations being requested.
	 * @param object      $args   Translation API arguments.
	 */
	$res = apply_filters( 'translations_api', false, $type, $args );

	if ( false === $res ) {
		$url = $http_url = 'http://api.wordpress.org/translations/' . $type . '/1.0/';
		if ( $ssl = wp_http_supports( array( 'ssl' ) ) ) {
			$url = set_url_scheme( $url, 'https' );
		}

		$options = array(
			'timeout' => 3,
			'body' => array(
				'wp_version' => $wp_version,
				'locale'     => get_locale(),
				'version'    => $args['version'], // Version of plugin, theme or core
			),
		);

		if ( 'core' !== $type ) {
			$options['body']['slug'] = $args['slug']; // Plugin or theme slug
		}

		$request = wp_remote_post( $url, $options );

		if ( $ssl && is_wp_error( $request ) ) {
			trigger_error(
				sprintf(
					/* translators: %s: support forums URL */
					__( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server’s configuration. If you continue to have problems, please try the <a href="%s">support forums</a>.' ),
					__( 'https://wordpress.org/support/' )
				) . ' ' . __( '(WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.)' ),
				headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE
			);

			$request = wp_remote_post( $http_url, $options );
		}

		if ( is_wp_error( $request ) ) {
			$res = new WP_Error( 'translations_api_failed',
				sprintf(
					/* translators: %s: support forums URL */
					__( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server’s configuration. If you continue to have problems, please try the <a href="%s">support forums</a>.' ),
					__( 'https://wordpress.org/support/' )
				),
				$request->get_error_message()
			);
		} else {
			$res = json_decode( wp_remote_retrieve_body( $request ), true );
			if ( ! is_object( $res ) && ! is_array( $res ) ) {
				$res = new WP_Error( 'translations_api_failed',
					sprintf(
/* translators: %s: support forums URL */
__( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server’s configuration. If you continue to have problems, please try the <a href="%s">support forums</a>.' ),
__( 'https://wordpress.org/support/' )
					),
					wp_remote_retrieve_body( $request )
				);
			}
		}
	}

	/**
	 * Filters the Translation Install API response results.
	 *
	 * @since 4.0.0
	 *
	 * @param object|WP_Error $res  Response object or WP_Error.
	 * @param string          $type The type of translations being requested.
	 * @param object          $args Translation API arguments.
	 */
	return apply_filters( 'translations_api_result', $res, $type, $args );
}

更新日志

Versiondescription
4.0.0Introduced.

相关函数

Uses

  • wp-admin/includes/translation-install.php: translations_api
  • wp-admin/includes/translation-install.php: translations_api_result
  • wp-includes/compat.php: json_decode()
  • wp-includes/l10n.php: __()
  • wp-includes/l10n.php: get_locale()
  • wp-includes/link-template.php: set_url_scheme()
  • wp-includes/http.php: wp_http_supports()
  • wp-includes/http.php: wp_remote_post()
  • wp-includes/http.php: wp_remote_retrieve_body()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/load.php: is_wp_error()
  • wp-includes/class-wp-error.php: WP_Error::__construct()
  • Show 7 more uses Hide more uses

Used By

  • wp-admin/includes/translation-install.php: wp_get_available_translations()

User Contributed Notes

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

    This function queries the WP translation database for a given project. For example, the Contact Form 7 plugin maintains its translations here. To find out which translation locales are available, you can use the translations_api function,

    
    $api = translations_api( 'plugins', array(
    	'slug'    => 'contact-form-7',
    	'version' => '4.4.1'
    ) );
    

    This will return an array of available translations for the plugin v4.4.1.

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

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

发布评论

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