Last active
August 24, 2022 16:58
-
-
Save pimpreneil/3f210f92b9d7b80fc1b44673fbcd5989 to your computer and use it in GitHub Desktop.
Small script to extract/import XLIFF from WPML with code for Wordpress translation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once('wp-load.php'); | |
require_once('wp-content/plugins/sitepress-multilingual-cms/inc/wpml-private-actions-tm.php'); | |
use WPML\TM\AutomaticTranslation\Actions\Actions; | |
define( 'WP_DEBUG', true ); | |
error_reporting(E_ALL); | |
function get_post_xliff($source_language, $post_id, $target_language) { | |
// We first create a translation job | |
global $sitepress; | |
$factory = new WPML_Translation_Element_Factory($sitepress); | |
$helper = new Actions($factory); | |
$res = $helper->createNewTranslationJobs($source_language, [[$post_id, $target_language]]); | |
// And then get the job's XLIFF | |
return wpml_tm_get_job_xliff($res[0]['jobId']); | |
} | |
function import_xliff_translation($xliff_content) { | |
$factory = wpml_tm_load_job_factory(); | |
$reader = new WPML_TM_General_Xliff_Reader($factory); | |
$data = $reader->get_data($xliff_content); | |
wpml_tm_save_data($data, false); | |
} | |
// Export the XLIFF for a job | |
$post_xliff_content = get_post_xliff('en', 1, 'fr'); | |
// Import the translated XLIFF | |
import_xliff_translation(file_get_contents('translation.xliff')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment