class-wpml-pb-reuse-translations.php
5.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
class WPML_PB_Reuse_Translations {
/** @var WPML_ST_String_Factory $string_factory */
private $string_factory;
/** @var array $original_strings */
private $original_strings;
/** @var array $current_strings */
private $current_strings;
public function __construct( WPML_ST_String_Factory $string_factory ) {
$this->string_factory = $string_factory;
}
/**
* We receive arrays of strings with this structure:
*
* array(
* 'gf4544ds454sds542122sd' => array(
* 'value' => 'The string value',
* 'context' => 'the-string-context',
* 'name' => 'the-string-name',
* 'id' => 123,
* 'package_id' => 123,
* 'location' => 123,
* ),
* )
*
* The key is the string hash.
*
* @param array[] $original_strings
* @param array[] $current_strings
* @param array[] $leftover_strings
*/
public function find_and_reuse_translations( array $original_strings, array $current_strings, array $leftover_strings ) {
$this->original_strings = $original_strings;
$this->current_strings = $current_strings;
$new_strings = $this->find_new_strings();
$new_strings_to_update = $this->find_existing_strings_for_new_strings( $new_strings, $leftover_strings );
$this->reuse_translations( $new_strings_to_update );
}
/** @return array */
private function find_new_strings() {
$new_strings = array();
foreach ( $this->current_strings as $current_string ) {
$found = false;
foreach ( $this->original_strings as $original_string ) {
if ( $current_string['id'] == $original_string['id'] ) {
$found = true;
break;
}
}
if ( ! $found ) {
$new_strings[ $current_string['id'] ] = 0;
}
}
return $new_strings;
}
/**
* @param int[] $new_strings
* @param array[] $leftover_strings
*
* @return int[]
*/
private function find_existing_strings_for_new_strings( array $new_strings, array $leftover_strings ) {
list( $new_strings, $leftover_strings ) = $this->find_by_location( $new_strings, $leftover_strings );
$new_strings = $this->find_by_similar_text( $new_strings, $leftover_strings );
return $new_strings;
}
/**
* @param int[] $new_strings
* @param array[] $leftover_strings
*
* @return array[]
*/
private function find_by_location( array $new_strings, array $leftover_strings ) {
if ( ! $leftover_strings ) {
return array( $new_strings, $leftover_strings );
}
if ( ( count( $this->current_strings ) - count( $leftover_strings ) ) !== count( $this->original_strings ) ) {
return array( $new_strings, $leftover_strings );
}
foreach ( $leftover_strings as $key => $leftover_string ) {
foreach ( $this->current_strings as $current_string ) {
if ( isset( $new_strings[ $current_string['id'] ] ) ) {
if ( $this->is_same_location_and_different_ids( $current_string, $leftover_string )
&& $this->is_similar_text( $leftover_string['value'], $current_string['value'] ) ) {
$new_strings[ $current_string['id'] ] = $leftover_string['id'];
unset( $leftover_strings[ $key ] );
}
}
}
}
return array( $new_strings, $leftover_strings );
}
/**
* @param int[] $new_strings
* @param array[] $leftover_strings
*
* @return int[]
*/
private function find_by_similar_text( array $new_strings, array $leftover_strings ) {
if ( $leftover_strings ) {
foreach ( $new_strings as $new_string_id => $old_string_id ) {
if ( ! $old_string_id ) {
$new_string = $this->string_factory->find_by_id( $new_string_id );
$new_string_value = $new_string->get_value();
foreach ( $leftover_strings as $key => $leftover_string ) {
$leftover_string_id = $leftover_string['id'];
$leftover_string = $this->string_factory->find_by_id( $leftover_string_id );
$leftover_string_value = $leftover_string->get_value();
if ( $this->is_similar_text( $leftover_string_value, $new_string_value ) ) {
$new_strings[ $new_string_id ] = $leftover_string_id;
unset( $leftover_strings[ $key ] );
}
}
}
}
}
return $new_strings;
}
/**
* @param array $current_string
* @param array $leftover_string
*
* @return bool
*/
private function is_same_location_and_different_ids( array $current_string, array $leftover_string ) {
return $current_string['location'] === $leftover_string['location']
&& $current_string['id'] !== $leftover_string['id'];
}
/**
* @param string $old_text
* @param string $new_text
*
* @return bool
*/
private function is_similar_text( $old_text, $new_text ) {
return WPML_ST_Diff::get_sameness_percent( $old_text, $new_text ) > 50;
}
/**
* @param int[] $strings
*/
private function reuse_translations( array $strings ) {
foreach ( $strings as $new_string_id => $old_string_id ) {
if ( $old_string_id ) {
$new_string = $this->string_factory->find_by_id( $new_string_id );
$old_string = $this->string_factory->find_by_id( $old_string_id );
$translations = $old_string->get_translations();
foreach ( $translations as $translation ) {
$status = $translation->status == ICL_TM_COMPLETE ? ICL_TM_NEEDS_UPDATE : $translation->status;
$new_string->set_translation(
$translation->language,
$translation->value,
$status,
$translation->translator_id,
$translation->translation_service,
$translation->batch_id
);
}
}
}
}
}