1 | <?php |
||
38 | class Po |
||
|
|||
39 | { |
||
40 | /** |
||
41 | * Reads a Gettext .po file. |
||
42 | * |
||
43 | * @link http://www.gnu.org/software/gettext/manual/gettext.html#PO-Files |
||
44 | * |
||
45 | * @param string $file Path to PO file. |
||
46 | * |
||
47 | * @return mixed|bool|array |
||
48 | */ |
||
49 | public static function read($file) |
||
50 | { |
||
51 | // read .po file |
||
52 | $fh = fopen($file, 'r'); |
||
53 | |||
54 | if ($fh === false) { |
||
55 | // could not open file resource |
||
56 | return false; |
||
57 | } |
||
58 | |||
59 | $result = []; |
||
60 | $temp = []; |
||
61 | $state = null; |
||
62 | $fuzzy = false; |
||
63 | |||
64 | // iterate over lines |
||
65 | while (($line = fgets($fh, 65536)) !== false) { |
||
66 | $line = trim($line); |
||
67 | |||
68 | if ($line === '') { |
||
69 | continue; |
||
70 | } |
||
71 | |||
72 | list($key, $data) = preg_split('/\s/', $line, 2); |
||
73 | |||
74 | switch ($key) { |
||
75 | case '#,': // flag... |
||
76 | $fuzzy = in_array('fuzzy', preg_split('/,\s*/', $data), true); |
||
77 | // fall-through |
||
78 | case '#': // translator-comments |
||
79 | case '#.': // extracted-comments |
||
80 | case '#:': // reference... |
||
81 | case '#|': // msgid previous-untranslated-string |
||
82 | // start a new entry |
||
83 | if (count($temp) && array_key_exists('msgid', $temp) && array_key_exists('msgstr', $temp)) { |
||
84 | if (false === $fuzzy) { |
||
85 | $result[] = $temp; |
||
86 | } |
||
87 | |||
88 | $temp = []; |
||
89 | $state = null; |
||
90 | $fuzzy = false; |
||
91 | } |
||
92 | break; |
||
93 | case 'msgctxt': // context |
||
94 | case 'msgid': // untranslated-string |
||
95 | case 'msgid_plural': // untranslated-string-plural |
||
96 | $state = $key; |
||
97 | $temp[$state] = $data; |
||
98 | break; |
||
99 | case 'msgstr': // translated-string |
||
100 | $state = 'msgstr'; |
||
101 | $temp[$state][] = $data; |
||
102 | break; |
||
103 | default: |
||
104 | if (strpos($key, 'msgstr[') !== false) { |
||
105 | // translated-string-case-n |
||
106 | $state = 'msgstr'; |
||
107 | $temp[$state][] = $data; |
||
108 | } else { |
||
109 | // continued lines |
||
110 | switch ($state) { |
||
111 | case 'msgctxt': |
||
112 | case 'msgid': |
||
113 | case 'msgid_plural': |
||
114 | $temp[$state] .= "\n" . $line; |
||
115 | break; |
||
116 | case 'msgstr': |
||
117 | $temp[$state][count($temp[$state]) - 1] .= "\n" . $line; |
||
118 | break; |
||
119 | default: |
||
120 | // parse error |
||
121 | fclose($fh); |
||
122 | |||
123 | return false; |
||
124 | } |
||
125 | } |
||
126 | break; |
||
127 | } |
||
128 | } |
||
129 | |||
130 | fclose($fh); |
||
131 | |||
132 | // add final entry |
||
133 | if ($state === 'msgstr') { |
||
134 | $result[] = $temp; |
||
135 | } |
||
136 | |||
137 | // Cleanup data, merge multiline entries, reindex result for ksort |
||
138 | $temp = $result; |
||
139 | $result = []; |
||
140 | |||
141 | foreach ($temp as $entry) { |
||
142 | foreach ($entry as &$v) { |
||
143 | $v = self::poCleaner($v); |
||
144 | |||
145 | if ($v === false) { |
||
146 | // parse error |
||
147 | return false; |
||
148 | } |
||
149 | } |
||
150 | $result[$entry['msgid']] = $entry; |
||
151 | } |
||
152 | |||
153 | return $result; |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * Cleans the po. |
||
158 | * |
||
159 | * @param string|array $x |
||
160 | * |
||
161 | * @return string Cleaned PO element. |
||
162 | */ |
||
163 | private static function poCleaner($x) |
||
186 | } |
||
187 |
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.