Avancé
AvancéTraduire via du code PHP

Traduire via du code PHP

Vous pouvez déclencher des traductions via du code PHP au sein de votre application, thème ou plugin.

La classe GatoStandalone\GatoAITranslationsForPolylang\Gato fournit des méthodes statiques pour déclencher des traductions :

MéthodeDescription
translateAlias de translateCustomPosts
translateCustomPostsTraduire les custom posts (pages, articles, types d'articles personnalisés)
translateTaxonomyTermsTraduire les termes de taxonomie (catégories, étiquettes, taxonomies personnalisées)
translateMediaTraduire les éléments média (images, documents, etc.)

Signature de la méthode

Voici la signature de la méthode pour toutes les méthodes de traduction :

Seul le paramètre ids est obligatoire. Tous les autres paramètres, s'ils ne sont pas fournis, seront définis en utilisant la valeur des Réglages du plugin.

Vous pouvez trouver cette classe dans le fichier src/Gato.php à l'intérieur du plugin.

namespace GatoStandalone\GatoAITranslationsForPolylang;
 
class Gato
{
  /**
   * Alias of `translateCustomPosts`
   *
   * @param int|int[] $ids Array of custom post IDs to translate
   * @param string|null $statusToUpdate The status the custom posts must have to be updated
   * @param string|null $statusWhenTranslated The status the custom posts will have after translation. Possible values: "draft", "pending", "publish", "private", "current" (i.e. don't modify the status), "same-as-origin" (i.e. copy the status from the origin post)
   * @param array<string,string>|null $languageProviders Array of: Key => language code, Value: Provider name, "none" to disable for that language, or "default" to use the default provider
   * @return array<string|int>|false Array of custom post IDs that were processed for translation, or `false` if none of the provided IDs was valid
   * @throws PolylangNotActiveException
   * @throws LicenseNotActiveException
   * @throws PluginNotInitializedException
   */
  public static function translate(
    int|array $ids,
    ?string $statusToUpdate = null,
    ?string $statusWhenTranslated = null,
    ?bool $copyDate = null,
    ?bool $translateSlugs = null,
    ?array $languageProviders = null,
    ?string $defaultTranslationProvider = null,
  ): array|false;
 
  /**
   * Translate custom posts
   *
   * @param int|int[] $ids Array of custom post IDs to translate
   * @param string|null $statusToUpdate The status the custom posts must have to be updated
   * @param string|null $statusWhenTranslated The status the custom posts will have after translation. Possible values: "draft", "pending", "publish", "private", "current" (i.e. don't modify the status), "same-as-origin" (i.e. copy the status from the origin post)
   * @param array<string,string>|null $languageProviders Array of: Key => language code, Value: Provider name, "none" to disable for that language, or "default" to use the default provider
   * @return array<string|int>|false Array of custom post IDs that were processed for translation, or `false` if none of the provided IDs was valid
   * @throws PolylangNotActiveException
   * @throws LicenseNotActiveException
   * @throws PluginNotInitializedException
   */
  public static function translateCustomPosts(
    int|array $ids,
    ?string $statusToUpdate = null,
    ?string $statusWhenTranslated = null,
    ?bool $copyDate = null,
    ?bool $translateSlugs = null,
    ?array $languageProviders = null,
    ?string $defaultTranslationProvider = null,
  ): array|false;
 
  /**
   * Translate taxonomy terms (categories and tags)
   *
   * @param int|int[] $ids Array of taxonomy term IDs to translate
   * @param array<string,string>|null $languageProviders Array of: Key => language code, Value: Provider name, "none" to disable for that language, or "default" to use the default provider
   * @return array<string|int>|false Array of taxonomy term IDs that were processed for translation, or `false` if none of the provided IDs was valid
   * @throws PolylangNotActiveException
   * @throws LicenseNotActiveException
   * @throws PluginNotInitializedException
   */
  public static function translateTaxonomyTerms(
    int|array $ids,
    ?bool $translateSlugs = null,
    ?array $languageProviders = null,
    ?string $defaultTranslationProvider = null,
  ): array|false;
 
  /**
   * Translate media items
   *
   * @param int|int[] $ids Array of media item IDs to translate
   * @param array<string,string>|null $languageProviders Array of: Key => language code, Value: Provider name, "none" to disable for that language, or "default" to use the default provider
   * @return array<string|int>|false Array of media item IDs that were processed for translation, or `false` if none of the provided IDs was valid
   * @throws PolylangNotActiveException
   * @throws LicenseNotActiveException
   * @throws PluginNotInitializedException
   */
  public static function translateMedia(
    int|array $ids,
    ?bool $translateSlugs = null,
    ?array $languageProviders = null,
    ?string $defaultTranslationProvider = null,
  ): array|false;
}

Contexte d'exécution

Exécutez l'une des méthodes uniquement après que le plugin ait été initialisé. Cela se produit sur différents hooks, selon le contexte :

ContexteHook
FrontendHook d'action 'wp'
AdminHook d'action 'wp_loaded'
REST APIHook de filtre 'rest_jsonp_enabled'

Exemples d'exécution

Exécuter des traductions depuis chacun des hooks WordPress :

use GatoStandalone\GatoAITranslationsForPolylang\Exception\AbstractGatoAITranslationsForPolylangException;
 
// Frontend
add_action('wp', function() {
  try {
    Gato::translateCustomPosts(123);
  } catch (AbstractGatoAITranslationsForPolylangException $e) {
    error_log($e->getMessage());
  }
});
 
// Admin
add_action('wp_loaded', function() {
  try {
    Gato::translateTaxonomyTerms([456, 789]);
  } catch (AbstractGatoAITranslationsForPolylangException $e) {
    error_log($e->getMessage());
  }
});
 
// REST API
add_filter('rest_jsonp_enabled', function(mixed $value): mixed {
  try {
    Gato::translateMedia([101, 102]);
  } catch (AbstractGatoAITranslationsForPolylangException $e) {
    error_log($e->getMessage());
  }
  return $value;
});

Si vous êtes certain que :

  • Polylang est actif
  • Vous disposez d'une licence valide pour le plugin
  • Le plugin a été initialisé (c'est-à-dire que les hooks ci-dessus ont été exécutés)

…alors vous pouvez exécuter des traductions sans vérifier les exceptions :

Gato::translate(123); // Same as `translateCustomPosts`
Gato::translateCustomPosts(123);
Gato::translateTaxonomyTerms([456, 789]);
Gato::translateMedia([101, 102]);