PluginProbe
Linguise – AI Automatic Multilingual Translation / trunk
Linguise – AI Automatic Multilingual Translation vtrunk
2.2.64 2.2.63 2.2.62 2.2.61 2.2.60 2.2.59 2.2.58 2.2.57 2.2.56 2.2.55 2.2.54 2.2.53 2.2.52 2.2.51 2.2.50 2.2.49 2.2.47 2.2.48 2.2.46 2.2.45 2.2.44 2.2.43 2.2.42 1.9.7 1.9.8 All 255 releases
linguise / src / FragmentAutoHandler.php

FragmentAutoHandler.php in Linguise – AI Automatic Multilingual Translation trunk, at src/FragmentAutoHandler.php

50 lines 1.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Linguise\WordPress;
4
5 use Linguise\WordPress\FragmentBase;
6 use Linguise\Vendor\JsonPath\JsonObject;
7 use Linguise\Vendor\Linguise\Script\Core\Debug;
8
9 defined('ABSPATH') || die('');
10
11 /**
12 * Fragment auto mode handler.
13 */
14 class FragmentAutoHandler extends FragmentBase
15 {
16 /**
17 * Apply the translated fragments for the auto mode.
18 *
19 * @param array $json_data The JSON data to be injected
20 * @param array $fragments The array of fragments to be injected, from intoJSONFragments
21 *
22 * @return array
23 */
24 public static function applyTranslatedFragmentsForAuto($json_data, $fragments)
25 {
26 $json_path = new JsonObject($json_data);
27 // Warn if $json_data is empty
28 if (empty($json_path->getValue())) {
29 return false;
30 }
31
32 foreach ($fragments as $fragment) {
33 if (isset($fragment['skip']) && $fragment['skip']) {
34 // If skip is true, then don't replace this fragment (but remove it)
35 continue;
36 }
37 // remove the html fragment from the translated page
38 $decoded_key = self::unwrapKey($fragment['key']);
39 try {
40 $json_path->set('$.' . $decoded_key, $fragment['value']);
41 } catch (\Linguise\Vendor\JsonPath\InvalidJsonPathException $e) {
42 Debug::log('Failed to set key in auto: ' . $decoded_key . ' -> ' . $e->getMessage());
43 }
44 }
45
46 $replaced_json = $json_path->getValue();
47 return $replaced_json;
48 }
49 }
50