Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 46 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| tao_helpers_translation_POFileReader | |
0.00% |
0 / 46 |
|
0.00% |
0 / 1 |
182 | |
0.00% |
0 / 1 |
| read | |
0.00% |
0 / 46 |
|
0.00% |
0 / 1 |
182 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2008-2010 (original work) Deutsche Institut für Internationale Pädagogische Forschung |
| 19 | * (under the project TAO-TRANSFER); |
| 20 | * 2009-2012 (update and modification) Public Research Centre Henri Tudor |
| 21 | * (under the project TAO-SUSTAIN & TAO-DEV); |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | /** |
| 26 | * An implementation of TranslationFileReader aiming at reading PO files. |
| 27 | * |
| 28 | * @access public |
| 29 | * @author Jerome Bogaerts |
| 30 | * @package tao |
| 31 | * @since 2.2 |
| 32 | |
| 33 | * @version 1.0 |
| 34 | */ |
| 35 | class tao_helpers_translation_POFileReader extends tao_helpers_translation_TranslationFileReader |
| 36 | { |
| 37 | // --- ASSOCIATIONS --- |
| 38 | |
| 39 | |
| 40 | // --- ATTRIBUTES --- |
| 41 | |
| 42 | // --- OPERATIONS --- |
| 43 | |
| 44 | /** |
| 45 | * Short description of method read |
| 46 | * |
| 47 | * @access public |
| 48 | * @author firstname and lastname of author, <author@example.org> |
| 49 | * @throws tao_helpers_translation_TranslationException |
| 50 | * @return mixed |
| 51 | */ |
| 52 | public function read() |
| 53 | { |
| 54 | |
| 55 | $file = $this->getFilePath(); |
| 56 | if (!file_exists($file)) { |
| 57 | throw new tao_helpers_translation_TranslationException("The translation file '${file}' does not exist."); |
| 58 | } |
| 59 | |
| 60 | // Create the translation file. |
| 61 | $tf = new tao_helpers_translation_POFile(); |
| 62 | |
| 63 | $fc = implode('', file($file)); |
| 64 | |
| 65 | $matched = preg_match_all( |
| 66 | '/((?:#[\.:,\|]{0,1}\s+(?:.*?)\\n)*)' . |
| 67 | '(msgctxt\s+(?:"(?:[^"]|\\\\")*?"\s*)+)?' . |
| 68 | '(msgid\s+(?:"(?:[^"]|\\\\")*?"\s*)+)\s+' . |
| 69 | '(msgstr\s+(?:"(?:[^"]|\\\\")*?(?<!\\\)"\s*)+)/', |
| 70 | $fc, |
| 71 | $matches |
| 72 | ); |
| 73 | |
| 74 | preg_match('/sourceLanguage: (.*?)\\n/s', $fc, $sourceLanguage); |
| 75 | preg_match('/targetLanguage: (.*?)\\n/s', $fc, $targetLanguage); |
| 76 | |
| 77 | /** |
| 78 | * As a new process was introduced to translate languages on TAO (using Crowdin) |
| 79 | * we can not rely on the targetLanguage attribute anymore as they will be always en-US |
| 80 | * so the solution was to rely on the file's folder name |
| 81 | */ |
| 82 | |
| 83 | $matchedTargetLanguage = $targetLanguage[1] ?? ''; |
| 84 | |
| 85 | $targetLanguageFromFileName = basename(dirname($file)); |
| 86 | |
| 87 | if ( |
| 88 | strstr($matchedTargetLanguage, DEFAULT_LANG) |
| 89 | && $targetLanguageFromFileName !== "" |
| 90 | && preg_match("/^[a-z]{2}-[A-Z]{2}$/", $targetLanguageFromFileName) |
| 91 | ) { |
| 92 | $matchedTargetLanguage = $targetLanguageFromFileName; |
| 93 | } |
| 94 | |
| 95 | if (count($sourceLanguage)) { |
| 96 | $tf->setSourceLanguage(substr($sourceLanguage[1], 0, 5)); |
| 97 | } |
| 98 | if (count($targetLanguage)) { |
| 99 | $tf->setTargetLanguage(substr($matchedTargetLanguage, 0, 5)); |
| 100 | } |
| 101 | |
| 102 | if ($matched) { |
| 103 | for ($i = 0; $i < $matched; $i++) { |
| 104 | $annotations = $matches[1][$i]; |
| 105 | $msgctxt = preg_replace('/\s*msgctxt\s*"(.*)"\s*/s', '\\1', $matches[2][$i]); |
| 106 | $msgid = preg_replace('/\s*msgid\s*"(.*)"\s*/s', '\\1', $matches[3][$i]); |
| 107 | $msgstr = preg_replace('/\s*msgstr\s*"(.*)"\s*/s', '\\1', $matches[4][$i]); |
| 108 | |
| 109 | // Do not include meta data as a translation unit.. |
| 110 | if ($msgid !== '') { |
| 111 | // Sanitze the strings. |
| 112 | $msgid = tao_helpers_translation_POUtils::sanitize($msgid); |
| 113 | $msgstr = tao_helpers_translation_POUtils::sanitize($msgstr); |
| 114 | $msgctxt = tao_helpers_translation_POUtils::sanitize($msgctxt); |
| 115 | $tu = new tao_helpers_translation_POTranslationUnit(); |
| 116 | |
| 117 | // Set up source & target. |
| 118 | $tu->setSource($msgid); |
| 119 | if ($msgstr !== '') { |
| 120 | $tu->setTarget($msgstr); |
| 121 | } |
| 122 | if ($msgctxt) { |
| 123 | $tu->setContext($msgctxt); |
| 124 | } |
| 125 | |
| 126 | // Deal with annotations |
| 127 | $annotations = tao_helpers_translation_POUtils::unserializeAnnotations($annotations); |
| 128 | foreach ($annotations as $name => $value) { |
| 129 | $tu->addAnnotation($name, $value); |
| 130 | } |
| 131 | |
| 132 | $tf->addTranslationUnit($tu); |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | $this->setTranslationFile($tf); |
| 138 | } |
| 139 | } |