Skip to content
Snippets Groups Projects

Wpct i18n

Adapter for multilingual wp plugins.

The plugin offers a coherent and consistent filters API to interact with multiple translation plugins. Integrates with WPML and Polylang.

Installation

Download the latest release as a zipfile. Once downloaded, decompress and place its content on your WP instance. wp-content/plugins's directory.

Go to the releases page to find previous versions.

You can install it with wp-cli with the next command:

wp plugin install https://git.coopdevs.org/codeccoop/wp/plugins/wpct-i18n/-/releases/permalink/latest/downloads/plugins/wpct-i18n.zip

API

Getters

wpct_i18n_default_language

Get site default language.

Arguments:

  1. any $default: Default value.
  2. string $format: Locale or slug.

Returns:

  1. string $language: Site current language.

Example:

$language = apply_filter('wpct_i18n_default_language', null, 'slug');
echo $language;

wpct_i18n_current_language

Get site current language.

Arguments:

  1. any $default: Default value.
  2. string $format: Locale or slug.

Returns:

  1. string $language: Site current language.

Example:

$language = apply_filter('wpct_i18n_current_language', null, 'slug');
echo $language;

wpct_i18n_active_languages

Returns the site active languages.

Arguments:

  1. any $default: Default value.
  2. string $format: Locale or slug.

Returns:

  1. array $lang: Active languages

Example:

$languages = apply_filters('wpct_i18n_active_languages', [], 'slug');
foreach ($languages as $language) {
	// do something
}

wpct_i18n_post_language

Get WP_Post language.

Arguments:

  1. any $default: Default value.
  2. integer $post_id: Post ID.
  3. string $format: Locale or slug.

Returns:

  1. string $language: Post's language.

Example:

$lang = apply_filters('wpct_i18n_post_language', null, 10, 'locale');
if ($lang !== 'en_US') {
	// do something
}

wpct_i18n_post_translations

Get WP_Post current translations.

Arguments:

  1. any $default: Default value.
  2. integer $post_id: Post ID.

Returns:

  1. array<string, integer>: Array with languages and post IDs.

Example:

$translations = apply_filters('wpct_i18n_post_translations', [], 10);
foreach ($translations as $lang => $trans_id) {
	// do something
}

wpct_i18n_term_translations

Gets WP_Term current translations.

Arguments:

  1. any $default: Default value.
  2. integer $term_id: Term ID.

Returns:

  1. array<string, integer>: Array with languages and term IDs.

Example:

$translations = apply_filters('wpct_i18n_term_translations', [], 10);
foreach ($translations as $lang => $trans_id) {
	// do something
}

wpct_i18n_is_translation

Get site default language.

Arguments:

  1. any $default: Default value.
  2. integer $post_id: Post ID.

Returns:

  1. boolean $is_translation: True if post is a translation.

Example:

$is_translation = apply_filters('wpct_i18n_is_translation', false, 274);
if ($is_translation) {
	// do something
}

wpct_i18n_translate_post

Add a post translation to the given post.

Arguments:

  1. any $default: Default value.
  2. WP_Post $post: Post object.
  3. string $lang: Language code.

Returns:

  1. integer $trans_id: ID of the new translation.

Example:

$trans_id = apply_filters('wpct_i18n_translate_post', null, 1, 'en');
echo $trans_id;

Actions

wpct_i18n_language_switcher

Renders the integration language switcher.

Arguments:

  1. any $default: Default value.
  2. boolean $echo: Should return string or echo it.

Returns:

  1. string $html: Language switcher rendered HTML.

Example:

do_action('wpct_i18n_language_switcher', '', true);