定制 sitesoft-be/wp-api-translator 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

sitesoft-be/wp-api-translator

Composer 安装命令:

composer require sitesoft-be/wp-api-translator

包简介

Translate WordPress posts using Deepl or Google Translate API

README 文档

README

This package provides an easy way to automatically translate WordPress posts and ACF fields into multiple languages using translation APIs such as Deepl and Google Translate. It integrates with WPML for managing translations.

Table of Contents

Installation

You can install this package via Composer by running the following command in the root of your WordPress project:

composer require sitesoft-be/wp-api-translator

Example Usage

1. Translate a Post on Save

use Sitesoft\WpApiTranslator\DeeplTranslator;
use Sitesoft\WpApiTranslator\WPMLPostTranslator;

require_once 'vendor/autoload.php';

function translatePost( $post_id ) {
	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
		return;
	}

	if ( wp_is_post_revision( $post_id ) ) {
		return;
	}

	if ( get_post_type( $post_id ) !== "your_custom_post_type" ) {
		return;
	}

	remove_action( "save_post", "translatePost" );

	$deepTranslator = new DeeplTranslator( 'YOUR_DEEPL_API_KEY' );

	$wpmlTranslator = new WPMLPostTranslator( $deepTranslator );

	$wpmlTranslator->translatePost( $post_id, [ "fr", "en" ] );

	add_action( "save_post", "translatePost" );
}

add_action( 'save_post', 'translatePost' );

2. Translate ACF Fields

use Sitesoft\WpApiTranslator\DeeplTranslator;
use Sitesoft\WpApiTranslator\WPMLPostTranslator;

require_once 'vendor/autoload.php';

function translatePost( $post_id ) {
	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
		return;
	}

	if ( wp_is_post_revision( $post_id ) ) {
		return;
	}

	if ( get_post_type( $post_id ) !== "your_custom_post_type" ) {
		return;
	}

	remove_action( "save_post", "translatePost" );

	$deepTranslator = new DeeplTranslator( 'YOUR_DEEPL_API_KEY' );

	$wpmlTranslator = new WPMLPostTranslator( $deepTranslator );

	$wpmlTranslator->translatePostACF( $post_id, [ "fr", "en" ] );

	add_action( "save_post", "translatePost" );
}

add_action( 'save_post', 'translatePost' );

Translator Configuration

1. Deepl Translator

You can use the Deepl API for high-quality translations. To configure it, replace the FakeTranslator with DeeplTranslator:

use Sitesoft\WpApiTranslator\DeeplTranslator;

$deeplTranslator = new DeeplTranslator( 'your-api-key-here' );
$wpmlTranslator = new WPMLPostTranslator( $deeplTranslator );

2. Google Translate Translator

use Sitesoft\WpApiTranslator\GoogleTranslateTranslator;

$googleTranslator = new GoogleTranslateTranslator( 'your-api-key-here' );
$wpmlTranslator = new WPMLPostTranslator( $googleTranslator );

WPML Integration

This package integrates with WPML to manage the multilingual content. It uses the wpml_set_element_language_details hook to set the language details for newly translated posts.

Supported Languages

This package supports any language that is available in the translation API you choose to use. By default, you can use any language code supported by Deepl or Google Translate.

Notes

The package relies on WPML for managing translations. Ensure WPML is installed and configured on your WordPress site. Make sure to replace API keys with your actual API credentials for Deepl or Google Translate. You may need to configure your ACF fields to ensure they are correctly translated.

License

This package is licensed under the MIT License. See the LICENSE file for more details.

统计信息

  • 总下载量: 28
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-12-22