first commit
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace WPML\ST\JED\Hooks;
|
||||
|
||||
use WPML\ST\TranslationFile\Sync\FileSync;
|
||||
use WPML_ST_JED_Domain;
|
||||
use WPML_ST_JED_File_Manager;
|
||||
use WPML_ST_Script_Translations_Hooks;
|
||||
use WPML_ST_Translations_File_Locale;
|
||||
|
||||
class Sync implements \IWPML_Frontend_Action, \IWPML_Backend_Action, \IWPML_DIC_Action {
|
||||
|
||||
/** @var FileSync */
|
||||
private $fileSync;
|
||||
|
||||
public function __construct( FileSync $fileSync ) {
|
||||
$this->fileSync = $fileSync;
|
||||
}
|
||||
|
||||
public function add_hooks() {
|
||||
add_filter(
|
||||
'load_script_translation_file',
|
||||
[ $this, 'syncCustomJedFile' ],
|
||||
WPML_ST_Script_Translations_Hooks::PRIORITY_OVERRIDE_JED_FILE - 1,
|
||||
3
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|false $jedFile Path to the translation file to load. False if there isn't one.
|
||||
* @param string $handler Name of the script to register a translation domain to.
|
||||
* @param string $domain The text domain.
|
||||
*/
|
||||
public function syncCustomJedFile( $jedFile, $handler, $domain ) {
|
||||
$this->fileSync->sync( $jedFile, WPML_ST_JED_Domain::get( $domain, $handler ) );
|
||||
|
||||
return $jedFile;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
class WPML_ST_JED_Domain {
|
||||
|
||||
public static function get( $domain, $handler ) {
|
||||
return $domain . '-' . $handler;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
use WPML\ST\TranslationFile\StringEntity;
|
||||
|
||||
class WPML_ST_JED_File_Builder extends WPML\ST\TranslationFile\Builder {
|
||||
|
||||
/** @var string $decoded_eot */
|
||||
private $decoded_eot;
|
||||
|
||||
public function __construct() {
|
||||
$this->decoded_eot = json_decode( WPML_ST_Translations_File_JED::DECODED_EOT_CHAR );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param StringEntity[] $strings
|
||||
* @return string
|
||||
*/
|
||||
public function get_content( array $strings ) {
|
||||
$data = new stdClass();
|
||||
|
||||
$data->{'translation-revision-date'} = date( 'Y-m-d H:i:sO' );
|
||||
$data->generator = 'WPML String Translation ' . WPML_ST_VERSION;
|
||||
$data->domain = 'messages';
|
||||
$data->locale_data = new stdClass();
|
||||
$data->locale_data->messages = new stdClass();
|
||||
|
||||
$data->locale_data->messages->{WPML_ST_Translations_File_JED::EMPTY_PROPERTY_NAME} = (object) array(
|
||||
'domain' => 'messages',
|
||||
'plural-forms' => $this->plural_form,
|
||||
'lang' => $this->language,
|
||||
);
|
||||
|
||||
foreach ( $strings as $string ) {
|
||||
$original = $this->get_original_with_context( $string );
|
||||
$data->locale_data->messages->{$original} = $string->get_translations();
|
||||
}
|
||||
|
||||
$jed_content = wp_json_encode( $data );
|
||||
|
||||
return preg_replace( '/"' . WPML_ST_Translations_File_JED::EMPTY_PROPERTY_NAME . '"/', '""', $jed_content, 1 );
|
||||
}
|
||||
|
||||
private function get_original_with_context( StringEntity $string ) {
|
||||
if ( $string->get_context() ) {
|
||||
return $string->get_context() . $this->decoded_eot . $string->get_original();
|
||||
}
|
||||
|
||||
return $string->get_original();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
use WPML\Collect\Support\Collection;
|
||||
use WPML\ST\TranslationFile\Manager;
|
||||
|
||||
class WPML_ST_JED_File_Manager extends Manager {
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
protected function getFileExtension() {
|
||||
return 'json';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isPartialFile() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collection
|
||||
*/
|
||||
protected function getDomains() {
|
||||
return $this->domains->getJEDDomains();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
use WPML\ST\JED\Hooks\Sync;
|
||||
use WPML\ST\TranslationFile\Sync\FileSync;
|
||||
use function WPML\Container\make;
|
||||
use WPML\ST\TranslationFile\UpdateHooks;
|
||||
|
||||
class WPML_ST_Script_Translations_Hooks_Factory implements IWPML_Backend_Action_Loader, IWPML_Frontend_Action_Loader {
|
||||
|
||||
/**
|
||||
* Create hooks.
|
||||
*
|
||||
* @return array|IWPML_Action
|
||||
* @throws \WPML\Auryn\InjectionException Auryn Exception.
|
||||
*/
|
||||
public function create() {
|
||||
$hooks = array();
|
||||
|
||||
$jed_file_manager = make(
|
||||
WPML_ST_JED_File_Manager::class,
|
||||
[ ':builder' => make( WPML_ST_JED_File_Builder::class ) ]
|
||||
);
|
||||
|
||||
$hooks['update'] = $this->get_update_hooks( $jed_file_manager );
|
||||
|
||||
if ( ! wpml_is_ajax() && ! wpml_is_rest_request() ) {
|
||||
$hooks['filtering'] = $this->get_filtering_hooks( $jed_file_manager );
|
||||
}
|
||||
|
||||
if ( WPML\ST\TranslationFile\Hooks::useFileSynchronization() ) {
|
||||
$hooks['sync'] = make(
|
||||
Sync::class,
|
||||
[
|
||||
':fileSync' => make( FileSync::class, [ ':manager' => $jed_file_manager ] ),
|
||||
':manager' => $jed_file_manager,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
return $hooks;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WPML_ST_JED_File_Manager $jed_file_manager
|
||||
*
|
||||
* @return UpdateHooks
|
||||
*/
|
||||
private function get_update_hooks( $jed_file_manager ) {
|
||||
return make(
|
||||
UpdateHooks::class,
|
||||
[ ':file_manager' => $jed_file_manager ]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WPML_ST_JED_File_Manager $jed_file_manager
|
||||
*
|
||||
* @return WPML_ST_Script_Translations_Hooks
|
||||
*/
|
||||
private function get_filtering_hooks( $jed_file_manager ) {
|
||||
return make(
|
||||
WPML_ST_Script_Translations_Hooks::class,
|
||||
[ ':jed_file_manager' => $jed_file_manager ]
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
class WPML_ST_Script_Translations_Hooks implements IWPML_Action {
|
||||
|
||||
const PRIORITY_OVERRIDE_JED_FILE = 10;
|
||||
|
||||
/** @var WPML_ST_Translations_File_Dictionary $dictionary */
|
||||
private $dictionary;
|
||||
|
||||
/** @var WPML_ST_JED_File_Manager $jed_file_manager */
|
||||
private $jed_file_manager;
|
||||
|
||||
/** @var WPML_File $wpml_file */
|
||||
private $wpml_file;
|
||||
|
||||
public function __construct(
|
||||
WPML_ST_Translations_File_Dictionary $dictionary,
|
||||
WPML_ST_JED_File_Manager $jed_file_manager,
|
||||
WPML_File $wpml_file
|
||||
) {
|
||||
$this->dictionary = $dictionary;
|
||||
$this->jed_file_manager = $jed_file_manager;
|
||||
$this->wpml_file = $wpml_file;
|
||||
}
|
||||
|
||||
public function add_hooks() {
|
||||
add_filter( 'load_script_translation_file', array( $this, 'override_jed_file' ), self::PRIORITY_OVERRIDE_JED_FILE, 3 );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $filepath
|
||||
* @param string $handler
|
||||
* @param string $domain
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function override_jed_file( $filepath, $handler, $domain ) {
|
||||
if ( ! $filepath ) {
|
||||
return $filepath;
|
||||
}
|
||||
|
||||
$locale = $this->get_file_locale( $filepath, $domain );
|
||||
$domain = WPML_ST_JED_Domain::get( $domain, $handler );
|
||||
$wpml_filepath = $this->jed_file_manager->get( $domain, $locale );
|
||||
|
||||
if ( $wpml_filepath ) {
|
||||
return $wpml_filepath;
|
||||
}
|
||||
|
||||
return $filepath;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $filepath
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function is_file_imported( $filepath ) {
|
||||
$relative_path = $this->wpml_file->get_relative_path( $filepath );
|
||||
$file = $this->dictionary->find_file_info_by_path( $relative_path );
|
||||
$statuses = array( WPML_ST_Translations_File_Entry::IMPORTED, WPML_ST_Translations_File_Entry::FINISHED );
|
||||
|
||||
return $file && in_array( $file->get_status(), $statuses, true );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $filepath
|
||||
* @param string $domain
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function get_file_locale( $filepath, $domain ) {
|
||||
return \WPML\Container\make( \WPML_ST_Translations_File_Locale::class )->get( $filepath, $domain );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user