Retrieve translations from WordPress Translation API.
Parameters
$type
stringrequired- Type of translations. Accepts
'plugins'
,'themes'
,'core'
. $args
array|objectoptional- Translation API arguments. Optional.
Default:
null
Return
array|WP_Error On success an associative array of translations, WP_Error on failure.translations
arrayList of translations, each an array of data....$0
arraylanguage
stringLanguage code.version
stringWordPress version.updated
stringDate the translation was last updated, in MySQL datetime format.english_name
stringEnglish name of the language.native_name
stringNative name of the language.package
stringURL to download the translation package.iso
string[]Array of ISO language codes.strings
arrayArray of translated strings used in the installation process.
}
Source
function translations_api( $type, $args = null ) { if ( ! in_array( $type, array( 'plugins', 'themes', 'core' ), true ) ) { return new WP_Error( 'invalid_type', __( 'Invalid translation type.' ) ); } /** * Allows a plugin to override the WordPress.org Translation Installation API entirely. * * @since 4.0.0 * * @param false|array $result The result array. 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 = 'https://api.wordpress.org/translations/' . $type . '/1.0/'; $http_url = $url; $ssl = wp_http_supports( array( 'ssl' ) ); if ( $ssl ) { $url = set_url_scheme( $url, 'https' ); } $options = array( 'timeout' => 3, 'body' => array( 'wp_version' => wp_get_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 ) ) { wp_trigger_error( __FUNCTION__, 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/forums/' ) ) . ' ' . __( '(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/forums/' ) ), $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/forums/' ) ), wp_remote_retrieve_body( $request ) ); } } } /** * Filters the Translation Installation API response results. * * @since 4.0.0 * * @param array|WP_Error $res { * On success an associative array of translations, WP_Error on failure. * * @type array $translations { * List of translations, each an array of data. * * @type array ...$0 { * @type string $language Language code. * @type string $version WordPress version. * @type string $updated Date the translation was last updated, in MySQL datetime format. * @type string $english_name English name of the language. * @type string $native_name Native name of the language. * @type string $package URL to download the translation package. * @type string[] $iso Array of ISO language codes. * @type array $strings Array of translated strings used in the installation process. * } * } * } * @param string $type The type of translations being requested. * @param object $args Translation API arguments. */ return apply_filters( 'translations_api_result', $res, $type, $args ); }
Hooks
- apply_filters( ‘translations_api’,
false|array $result ,string $type ,object $args ) Allows a plugin to override the WordPress.org Translation Installation API entirely.
- apply_filters( ‘translations_api_result’,
array|WP_Error $res ,string $type ,object $args ) Filters the Translation Installation API response results.
Related
Show 4 moreShow lessUses Description wp_get_wp_version() wp-includes/functions.php
Returns the current WordPress version.
wp_trigger_error() wp-includes/functions.php
Generates a user-level error/warning/notice/deprecation message.
set_url_scheme() wp-includes/link-template.php
Sets the scheme for a URL.
wp_http_supports() wp-includes/http.php
Determines if there is an HTTP Transport that can process this request.
wp_remote_post() wp-includes/http.php
Performs an HTTP request using the POST method and returns its response.
wp_remote_retrieve_body() wp-includes/http.php
Retrieves only the body from the raw response.
apply_filters() wp-includes/plugin.php
Calls the callback functions that have been added to a filter hook.
is_wp_error() wp-includes/load.php
Checks whether the given variable is a WordPress Error.
WP_Error::__construct() wp-includes/class-wp-error.php
Initializes the error.
Used by Description wp_get_available_translations() wp-admin/includes/translation-install.php
Get available translations from the WordPress.org API.
Changelog
Version Description 4.0.0 Introduced. User Contributed Notes
You must log in before being able to contribute a note or feedback.
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,This will return an array of available translations for the plugin v4.4.1.