wpml-st-translations-file-jed.php
2.15 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
<?php
class WPML_ST_Translations_File_JED implements IWPML_ST_Translations_File {
const EMPTY_PROPERTY_NAME = '_empty_';
const DECODED_EOT_CHAR = '"\u0004"';
const PLURAL_SUFFIX_PATTERN = ' [plural %d]';
/** @var string $filepath */
private $filepath;
/** @var string $decoded_eot_char */
private $decoded_eot_char;
public function __construct( $filepath ) {
$this->filepath = $filepath;
$this->decoded_eot_char = json_decode( self::DECODED_EOT_CHAR );
}
/**
* @return WPML_ST_Translations_File_Translation[]
*/
public function get_translations() {
$translations = array();
$data = json_decode( (string) file_get_contents( $this->filepath ) );
if ( isset( $data->locale_data->messages ) ) {
$entries = (array) $data->locale_data->messages;
unset( $entries[ self::EMPTY_PROPERTY_NAME ] );
foreach ( $entries as $str => $str_data ) {
$str_data = (array) $str_data;
if ( ! isset( $str_data[0] ) ) {
continue;
}
list( $str, $context ) = $this->get_string_and_context( $str );
$count_translations = count( $str_data );
$translations[] = new WPML_ST_Translations_File_Translation( $str, $str_data[0], $context );
if ( $count_translations > 1 ) {
/**
* The strings coming after the first element are the plural translations.
* As we don't have the information about the original plural in the JED file,
* we will add a suffix to the original singular string.
*/
for ( $i = 1; $i < $count_translations; $i++ ) {
$plural_str = $str . sprintf( self::PLURAL_SUFFIX_PATTERN, $i );
$translations[] = new WPML_ST_Translations_File_Translation( $plural_str, $str_data[ $i ], $context );
}
}
}
}
return $translations;
}
/**
* The context is the first part of the string separated with the EOT char (\u0004)
*
* @param string $string
*
* @return array
*/
private function get_string_and_context( $string ) {
$context = '';
$parts = explode( $this->decoded_eot_char, $string );
if ( $parts && count( $parts ) > 1 ) {
$context = $parts[0];
$string = $parts[1];
}
return array( $string, $context );
}
}