forked from qtranslate/qtranslate-xt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qtranslate_language_blocks.php
440 lines (391 loc) · 13.3 KB
/
qtranslate_language_blocks.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
<?php
/**
* Break a multilingual string into string blocks as tokens.
* Not related to WordPress block editor (gutenberg).
*
* @param string $text multilingual string e.g. "[:en]English text[:fr]Texte francais[:]".
*
* @return string[] array of string tokens, including the ML tags.
* @since 3.3.6 swirly bracket encoding added
*/
function qtranxf_get_language_blocks( $text ) {
$lang_code = QTX_LANG_CODE_FORMAT;
$split_regex = "#(<!--:$lang_code-->|<!--:-->|\[:$lang_code\]|\[:\]|\{:$lang_code\}|\{:\})#ism";
return preg_split( $split_regex, $text, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE );
}
/**
* Split a multilingual string into array of tokens by language.
*
* @param string $text multilingual string.
*
* @return string[] array of string items indexed by language.
*/
function qtranxf_split( $text ) {
$blocks = qtranxf_get_language_blocks( $text );
return qtranxf_split_blocks( $blocks );
}
/**
* Split blocks into string items indexed by language.
*
* @param string[] $blocks array of string tokens, including tags.
* @param bool[] $found array of booleans indicating which languages are found.
*
* @return string[] array of string items indexed by language.
* @since 3.4.5.2 $found added
*/
function qtranxf_split_blocks( $blocks, &$found = array() ) {
global $q_config;
$result = array();
foreach ( $q_config['enabled_languages'] as $language ) {
$result[ $language ] = '';
}
$current_language = false;
$lang_code = QTX_LANG_CODE_FORMAT;
foreach ( $blocks as $block ) {
// detect c-tags
if ( preg_match( "#^<!--:($lang_code)-->$#ism", $block, $matches ) ) {
$current_language = $matches[1];
continue;
// detect b-tags
} elseif ( preg_match( "#^\[:($lang_code)]$#ism", $block, $matches ) ) {
$current_language = $matches[1];
continue;
// detect s-tags @since 3.3.6 swirly bracket encoding added
} elseif ( preg_match( "#^{:($lang_code)}$#ism", $block, $matches ) ) {
$current_language = $matches[1];
continue;
}
switch ( $block ) {
case '[:]':
case '{:}':
case '<!--:-->':
$current_language = false;
break;
default:
// correctly categorize text block
if ( $current_language ) {
if ( ! isset( $result[ $current_language ] ) ) {
$result[ $current_language ] = '';
}
$result[ $current_language ] .= $block;
$found[ $current_language ] = true;
$current_language = false;
} else {
foreach ( $q_config['enabled_languages'] as $language ) {
$result[ $language ] .= $block;
}
}
break;
}
}
// it gets trimmed later in qtranxf_use() anyway, better to do it here
foreach ( $result as $lang => $text ) {
$result[ $lang ] = trim( $text );
}
return $result;
}
/**
* gets only part with encoded languages
*/
function qtranxf_split_languages( $blocks ) {
$result = array();
$current_language = false;
$lang_code = QTX_LANG_CODE_FORMAT;
foreach ( $blocks as $block ) {
// detect c-tags
if ( preg_match( "#^<!--:($lang_code)-->$#ism", $block, $matches ) ) {
$current_language = $matches[1];
continue;
// detect b-tags
} elseif ( preg_match( "#^\[:($lang_code)]$#ism", $block, $matches ) ) {
$current_language = $matches[1];
continue;
// detect s-tags @since 3.3.6 swirly bracket encoding added
} elseif ( preg_match( "#^{:($lang_code)}$#ism", $block, $matches ) ) {
$current_language = $matches[1];
continue;
}
switch ( $block ) {
case '[:]':
case '{:}':
case '<!--:-->':
$current_language = false;
break;
default:
// correctly categorize text block
if ( $current_language ) {
if ( ! isset( $result[ $current_language ] ) ) {
$result[ $current_language ] = '';
}
$result[ $current_language ] .= $block;
$current_language = false;
}
break;
}
}
// it gets trimmed later in qtranxf_getAvailableLanguages() anyway, better to do it here
foreach ( $result as $lang => $text ) {
$result[ $lang ] = trim( $text );
}
return $result;
}
function qtranxf_allthesame( $texts ) {
$text = null;
// take first not empty
foreach ( $texts as $lang => $lang_text ) {
if ( ! $lang_text || $lang_text == '' ) {
continue;
}
$text = $lang_text;
break;
}
if ( empty( $text ) ) {
return '';
}
foreach ( $texts as $lang => $lang_text ) {
if ( $lang_text != $text ) {
return null;
}
}
return $text;
}
function qtranxf_join_c( $texts ) {
$text = qtranxf_allthesame( $texts );
if ( ! is_null( $text ) ) {
return $text;
}
$text = '';
foreach ( $texts as $lang => $lang_text ) {
if ( empty( $lang_text ) ) {
continue;
}
$text .= '<!--:' . $lang . '-->' . $lang_text . '<!--:-->';
}
return $text;
}
function qtranxf_join_b_no_closing( $texts ) {
$text = qtranxf_allthesame( $texts );
if ( ! is_null( $text ) ) {
return $text;
}
$text = '';
foreach ( $texts as $lang => $lang_text ) {
if ( empty( $lang_text ) ) {
continue;
}
$text .= '[:' . $lang . ']' . $lang_text;
}
return $text;
}
function qtranxf_join_b( $texts ) {
$text = qtranxf_allthesame( $texts );
if ( ! is_null( $text ) ) {
return $text;
}
$text = '';
foreach ( $texts as $lang => $lang_text ) {
if ( empty( $lang_text ) ) {
continue;
}
$text .= '[:' . $lang . ']' . $lang_text;
}
if ( ! empty( $text ) ) {
$text .= '[:]';
}
return $text;
}
/**
* @since 3.3.6 swirly bracket encoding
*/
function qtranxf_join_s( $texts ) {
$text = qtranxf_allthesame( $texts );
if ( ! is_null( $text ) ) {
return $text;
}
$text = '';
foreach ( $texts as $lang => $lang_text ) {
if ( empty( $lang_text ) ) {
continue;
}
$text .= '{:' . $lang . '}' . $lang_text;
}
if ( ! empty( $text ) ) {
$text .= '{:}';
}
return $text;
}
/**
* Prepares multilingual text leaving text that matches $regex_sep outside of language tags.
* @since 3.4.6.2
*/
function qtranxf_join_byseparator( $texts, $regex_sep ) {
$text = qtranxf_allthesame( $texts );
if ( ! is_null( $text ) ) {
return $text;
}
$lines = array();
foreach ( $texts as $lang => $lang_text ) {
$lines[ $lang ] = preg_split( $regex_sep, $lang_text, -1, PREG_SPLIT_DELIM_CAPTURE );
}
$text = '';
while ( true ) {
$done = true;
$to_join = array();
$sep = '';
foreach ( $lines as $lang => $lang_lines ) {
$t = next( $lang_lines );
if ( $t === false ) {
continue;
}
if ( preg_match( $regex_sep, $t ) ) {
$sep = $t;
$t = next( $lang_lines );
}
$done = false;
$to_join[ $lang ] = $t;
}
if ( $done ) {
break;
}
$text .= qtranxf_join_b( $to_join ) . $sep;
}
return $text;
}
/**
* Prepare multilingal text leaving new line outside of language tags '[:]'.
*/
function qtranxf_join_byline( $texts ) {
$text = qtranxf_allthesame( $texts );
if ( ! is_null( $text ) ) {
return $text;
}
$lines = array();
foreach ( $texts as $lang => $text ) {
$lines[ $lang ] = preg_split( '/\r?\n\r?/', $text );
}
$text = '';
for ( $i = 0; true; ++$i ) {
$done = true;
$to_join = array();
foreach ( $lines as $lang => $lang_lines ) {
if ( sizeof( $lang_lines ) <= $i ) {
continue;
}
$done = false;
$line = $lang_lines[ $i ];
if ( ! $line || $line == '' ) {
continue;
}
$to_join[ $lang ] = $line;
}
if ( $done ) {
break;
}
$text .= qtranxf_join_b( $to_join ) . PHP_EOL;
}
return $text;
}
function qtranxf_use( $lang, $text, $show_available = false, $show_empty = false ) {
// return full string if language is not enabled
if ( is_array( $text ) ) {
// handle arrays recursively
foreach ( $text as $key => $t ) {
$text[ $key ] = qtranxf_use( $lang, $t, $show_available, $show_empty );
}
return $text;
}
if ( is_object( $text ) || $text instanceof __PHP_Incomplete_Class ) {
foreach ( get_object_vars( $text ) as $key => $t ) {
if ( ! isset( $text->$key ) ) {
continue;
}
$text->$key = qtranxf_use( $lang, $t, $show_available, $show_empty );
}
return $text;
}
// prevent filtering weird data types and save some resources
if ( ! is_string( $text ) || empty( $text ) ) {
return $text;
}
return qtranxf_use_language( $lang, $text, $show_available, $show_empty );
}
/** when $text is already known to be string */
function qtranxf_use_language( $lang, $text, $show_available = false, $show_empty = false ) {
$blocks = qtranxf_get_language_blocks( $text );
if ( count( $blocks ) <= 1 )//no language is encoded in the $text, the most frequent case
{
return $text;
}
return qtranxf_use_block( $lang, $blocks, $show_available, $show_empty );
}
function qtranxf_use_block( $lang, $blocks, $show_available = false, $show_empty = false ) {
$available_langs = array();
$content = qtranxf_split_blocks( $blocks, $available_langs );
return qtranxf_use_content( $lang, $content, $available_langs, $show_available, $show_empty );
}
function qtranxf_use_content( $lang, $content, $available_langs, $show_available = false, $show_empty = false ) {
global $q_config;
// show the content in the requested language, if available
if ( ! empty( $available_langs[ $lang ] ) ) {
return $content[ $lang ];
} elseif ( $show_empty ) {
return '';
}
// content is not available in requested language (bad!!) what now?
$alangs = array();
foreach ( $q_config['enabled_languages'] as $language ) {
if ( empty( $available_langs[ $language ] ) ) {
continue;
}
$alangs[] = $language;
}
if ( empty( $alangs ) ) {
return '';
}
$available_langs = $alangs;
// set alternative language to the first available in the order of enabled languages
$alt_lang = current( $available_langs );
$alt_content = $content[ $alt_lang ];
if ( ! $show_available ) {
if ( $q_config['show_displayed_language_prefix'] ) {
return '(' . $q_config['language_name'][ $alt_lang ] . ') ' . $alt_content;
} else {
return $alt_content;
}
}
// display selection for available languages
$language_list = '';
if ( preg_match( '/%LANG:([^:]*):([^%]*)%/', $q_config['not_available'][ $lang ], $match ) ) {
$normal_separator = $match[1];
$end_separator = $match[2];
// build available languages string backward
$i = 0;
foreach ( array_reverse( $available_langs ) as $language ) {
if ( $i == 1 ) {
$language_list = $end_separator . $language_list;
} elseif ( $i > 1 ) {
$language_list = $normal_separator . $language_list;
}
$language_name = qtranxf_getLanguageName( $language );
$language_list = '<a href="' . qtranxf_convertURL( '', $language, false, true ) . '" class="qtranxs-available-language-link qtranxs-available-language-link-' . $language . '" title="' . $q_config['language_name'][ $language ] . '">' . $language_name . '</a>' . $language_list;
++$i;
}
}
$msg = preg_replace( '/%LANG:([^:]*):([^%]*)%/', $language_list, $q_config['not_available'][ $lang ] );
$output = '<p class="qtranxs-available-languages-message qtranxs-available-languages-message-' . $lang . '">' . $msg . '</p>';
if ( ! empty( $q_config['show_alternative_content'] ) && $q_config['show_alternative_content'] ) {
$output .= $alt_content;
}
return apply_filters( 'i18n_content_translation_not_available', $output, $lang, $language_list, $alt_lang, $alt_content, $msg, $q_config );
}
function qtranxf_showAllSeparated( $text ) {
if ( empty( $text ) ) {
return $text;
}
global $q_config;
$result = '';
foreach ( qtranxf_getSortedLanguages() as $language ) {
$result .= $q_config['language_name'][ $language ] . ':' . PHP_EOL . qtranxf_use( $language, $text ) . PHP_EOL . PHP_EOL;
}
return $result;
}