-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-English-variants.php
340 lines (305 loc) ยท 8.42 KB
/
class-English-variants.php
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
<?php // (C) Copyright Bobbing Wide 2017
/**
* Implement a class to automatically convert from US English to other variations of English.
*
* Note: oik plugins are written in UK English ( en_GB ) but the translatable strings are supposed to be US English
* so we need to perform a reverse mapping to US English if we want the UK English language version to be 'correct'.
* There may not be that many instances where translation / conversion is actually required.
* But we won't know until we've tried it.
*
*/
class English_variants {
/**
* File downloaded from https://docs.google.com/spreadsheets/d/1-Coz3zEHpPwsgcW0ZTe5SFeYqnMrPU6WxB2DyYD_HHg/edit#gid=280899954
* Linked from https://en-gb.wordpress.org/translations/
*/
private $file = "WordPress.org Shared English Variants Translation Glossary - Variants.csv";
private $locales = array( "en_US" => 0, "en_AU" => 1 , "en_CA" => 2 , "en_GB" => 3 , "en_NZ" => 4 , "en_ZA" => 5 );
private $variants = array();
private $source_locale = null;
private $target_locale = null;
private $map = array();
private $reverse_map = array();
private $context = null;
/**
* @var dependencies_cache the true instance
*/
private static $instance;
/**
* Return a single instance of this class
*
* @return object
*/
public static function instance() {
if ( !isset( self::$instance ) && !( self::$instance instanceof self ) ) {
self::$instance = new self;
}
return self::$instance;
}
/**
* Constructor
*
* Prepares for mapping from en_US to en_GB
*/
function __construct( $locale="en_GB" ) {
$this->source_locale( "en_US" );
$this->target_locale( $locale );
$this->load_variants();
$this->create_map();
$this->update_map_with_context();
}
/**
* Creates maps and reverse maps
*
* Each time you change the source and target then you need to create new maps.
* The reverse map is used to check that the original text is not already in the target locale's vocab.
*/
function create_map() {
$this->map = array();
$this->reverse_map = array();
foreach ( $this->variants as $variant ) {
$source = $variant[ $this->locales[ $this->source_locale ] ];
$target = $variant[ $this->locales[ $this->target_locale ] ];
$this->map[ $source ] = $target;
$this->reverse_map[ $target ] = $source;
}
}
/**
* Sets the source locale
*
* @param string $locale
*/
function source_locale( $locale="en_US" ) {
$this->source_locale = $locale;
}
/**
* Sets the target locale
*
* @param string $locale
*/
function target_locale( $locale ) {
$this->target_locale = $locale;
}
/**
*
* Note: You only need to load the variants once
*
*/
function load_variants() {
$file_path = __DIR__ . '/' . $this->file;
$file = file( $file_path, FILE_IGNORE_NEW_LINES );
$line = array_shift( $file );
$this->validate_locales( $line );
foreach ( $file as $line ) {
$variant = $this->get_variant( $line );
$this->add_variant( $variant );
}
}
/**
* Update map with context
*
*/
function update_map_with_context() {
$file_path = __DIR__ . '/' . $this->source_locale . '-' . $this->target_locale . '.csv';
if ( file_exists( $file_path ) ) {
$file = file( $file_path, FILE_IGNORE_NEW_LINES );
} else {
echo "Missing file: $file_path" . PHP_EOL;
//$file = array( "check,cheque,bank" );
$file = array();
}
foreach ( $file as $line ) {
$values = str_getcsv( $line );
$source = $values[0];
$target = $values[1];
$context = bw_array_get( $values, 2, null );
if ( $context ) {
unset( $this->map[ $source ] );
unset( $this->reverse_map[ $target ] );
$this->map[ $context . ':' . $source ] = $target;
$this->reverse_map[ $context . ':' . $target ] = $source;
} else {
$this->map[ $source ] = $target;
$this->reverse_map[ $target ] = $source;
}
}
}
/**
* Checks that the CSV file has the format we expect
*
* If not, we carry on but it may not work.
* PHPUnit tests should fail when they see the echo's.
*/
function validate_locales( $line ) {
if ( $line !== "en ๐บ๐ธ,en-au ๐ฆ๐บ,en-ca ๐จ๐ฆ,en-gb ๐ฌ๐ง,en-nz ๐ณ๐ฟ,en-za ๐ฟ๐ฆ,pos,description" ) {
echo "Unexpected first line" . PHP_EOL;
echo $line . PHP_EOL;
}
}
/**
* Loads a variant
*/
function get_variant( $line ) {
$variant = str_getcsv( $line );
return $variant;
}
/**
* Adds a variant
*/
function add_variant( $variant ) {
$this->variants[] = $variant;
}
/**
* Returns a mapped text
*
* We need to be able to parse words in English language
*
* @paran string $text
* @param string|null $context
* @return string mapped text
*/
function map( $text, $context=null ) {
$this->context = $context;
//$words = explode( " ", $text );
$words = $this->get_tokens( $text );
$new_words = array_map( [ $this, "map_word" ], $words);
$mapped = implode( "", $new_words );
return $mapped;
}
/**
* Map a single word
*
* @param string $word
* @return string the mapped result
*/
function map_word( $word ) {
$lcword = strtolower( $word );
$variant = null;
if ( $this->context ) {
$key = $this->context . ':' . $lcword;
$variant = bw_array_get( $this->map, $key, null );
}
if ( null === $variant ) {
$variant = bw_array_get( $this->map, $lcword, $lcword );
}
if ( $lcword <> $word ) {
$variant = $this->recapitalise( $variant, $word, $lcword );
}
return $variant;
}
/**
* Recapitalises the translated word
*
* @TODO Cater for more than just the first letter.
*
* @param string $variant
* @param string $word Original word with Capitals
* @param string $lcword lower case version of original word
*/
function recapitalise( $variant, $word, $lcword ) {
$chars = str_split( $word );
$lcs = str_split( $lcword );
$variants = str_split( $variant );
for ( $i = 0; $i < count( $chars ); $i++ ) {
if ( $chars[ $i] <> $lcs[ $i ] ) {
if ( $variants[ $i ] == $lcs[ $i ] ) {
$variants[ $i ] = $chars[ $i ];
}
}
}
$variant = implode( "", $variants );
return $variant;
}
/**
* Checks the original language
*
* @param string $text
* @param string|null $context
* @return bool true if OK
*/
function check_original_language( $text, $context=null ) {
$mapped = $this->reverse_map( $text, $context );
if ( $mapped != $text ) {
echo "Original text already in target locale." . PHP_EOL;
echo $this->source_locale . ": " . $text . PHP_EOL;
echo $this->target_locale . ": " . $mapped . PHP_EOL;
return false;
}
return true;
}
/**
* Returns a reverse mapped text
*
* @param string $text
* @param string|null $context
* @return string reverse mapped text
*/
function reverse_map( $text, $context=null ) {
$words = $this->get_tokens( $text );
$new_words = array_map( [ $this, "reverse_map_word" ], $words);
$mapped = implode( "", $new_words );
return $mapped;
}
/**
* Returns a reverse mapped word
*
* @param string $word
* @return string reverse mapped word
*/
function reverse_map_word( $word ) {
$lcword = strtolower( $word );
$variant = null;
if ( $this->context ) {
$key = $this->context . ':' . $lcword;
$variant = bw_array_get( $this->reverse_map, $key, null );
}
if ( null === $variant ) {
$variant = bw_array_get( $this->reverse_map, $lcword, $lcword );
}
if ( $lcword <> $word ) {
$variant = $this->recapitalise( $variant, $word, $lcword );
}
return $variant;
}
/**
* Splits into wordy tokens
*
* In order to perform a simple lookup we need words without any punctuation and white space
* Checking ctype_alpha() should be OK for US/UK English source but not for any other language.
*
* We don't expect there to be any digits in words.
*
* @param string $text text to tokenize into "words"
* @return array tokens - some of which are words
*/
public function get_tokens( $text ) {
$chars = str_split( $text );
$current = null;
$current_type = "n";
$tokens = array();
$count = count( $chars );
for ( $i = 0; $i < $count; $i++ ) {
$char = $chars[ $i ];
if ( ctype_alpha( $char ) ) {
$this_type = 'a';
} elseif ( ctype_space( $char ) ) {
$this_type = " ";
} else {
$this_type = "?";
}
if ( $this_type == $current_type ) {
$current .= $char;
} else {
if ( $current ) {
$tokens[] = $current;
}
$current = $char;
$current_type = $this_type;
}
}
if ( $current ) {
$tokens[] = $current;
}
return $tokens;
}
}