How to get translations in WordPress using translations_api

The translations_api function in WordPress is a core function that is designed to interface with the WordPress.org Translations API. The primary role of this function is to retrieve potential translations for a given project.

This function is typically used when there is a need to access translation sets for a specific project. It can retrieve translations for themes, plugins, and core projects. The data returned by the function includes the language, updated timestamp, package URL, English name, native name, iso codes, strings count and translation percentages.

By using the translations_api function, developers can obtain the necessary translation data directly from the WordPress.org Translations API, without having to manually extract the data from the API’s response.

It’s important to note that the translations_api function only retrieves potential translations. It does not install or activate these translations. The implementation of the retrieved translations is left to the developer.

Parameters of the WordPress translations_api Function

The translations_api function in WordPress accepts two parameters:

  • $type (string) – This is a mandatory parameter that specifies the type of translations. It can accept ‘plugins’, ‘themes’, or ‘core’.
  • $args (arrayobject) – This is an optional parameter that can be used to provide additional Translation API arguments. If not provided, its default value will be null.

Return Value of the WordPress translations_api Function

The translations_api function returns either an associative array of translations or a WP_Error, depending on the success or failure of the function. On successful execution, it provides an array of translations. However, in case of a failure, it returns a WP_Error.

If the function does not accept any parameters, it will be explicitly stated. In such cases, there is no need to pass any arguments while calling the function.

Examples

Example 1: How to retrieve translations for plugins

$args = array(
 'slug' => 'my-plugin-slug', // replace with your plugin slug
 'version' => '1.0.0' // replace with your plugin version
);

$translations = translations_api('plugins', $args);

if ( is_wp_error( $translations ) ) {
 echo '<p>Error: ' . $translations->get_error_message() . '</p>';
} else {
 echo '<p>Translations: ' . print_r($translations, true) . '</p>';
}

This code snippet retrieves the translations for a specific plugin. The $args array includes the slug and version of the plugin. The translations_api function is then called with ‘plugins’ as the type and $args as the arguments. If an error occurs, it is displayed. Otherwise, the translations are displayed.

Example 2: How to retrieve translations for themes

$args = array(
 'slug' => 'my-theme-slug', // replace with your theme slug
 'version' => '1.0.0' // replace with your theme version
);

$translations = translations_api('themes', $args);

if ( is_wp_error( $translations ) ) {
 echo '<p>Error: ' . $translations->get_error_message() . '</p>';
} else {
 echo '<p>Translations: ' . print_r($translations, true) . '</p>';
}

This code snippet retrieves the translations for a specific theme. The $args array includes the slug and version of the theme. The translations_api function is then called with ‘themes’ as the type and $args as the arguments. If an error occurs, it is displayed. Otherwise, the translations are displayed.

Example 3: How to retrieve core translations

$args = array(
 'version' => '5.7' // replace with your WordPress version
);

$translations = translations_api('core', $args);

if ( is_wp_error( $translations ) ) {
 echo '<p>Error: ' . $translations->get_error_message() . '</p>';
} else {
 echo '<p>Translations: ' . print_r($translations, true) . '</p>';
}

This code snippet retrieves the translations for the WordPress core. The $args array includes the version of WordPress. The translations_api function is then called with ‘core’ as the type and $args as the arguments. If an error occurs, it is displayed. Otherwise, the translations are displayed.

Conclusion

The translations_api function in WordPress is a utility that facilitates the retrieval of translations for installed themes, plugins, and core WordPress itself. This function is primarily used to fetch available translations from the WordPress API, which can be utilized to update or install new translations for various components of a WordPress site. By leveraging the translations_api function, developers can ensure that their WordPress site is accessible and comprehensible to a global audience by supporting multiple languages.