parseFile = $parseFile; $this->filePath = $parseFile->filePath; $this->tmpFilePath = $this->filePath . '.tmp'; if (! is_writable($this->filePath)) { throw new Language_Exception("Can't write to file $this->filePath."); } } /** * Update language.php file with new strings. * * @param array $strings English strings collected from source files * @param bool $outputFiles whether file paths were string was found should be included or not in the output * @param string $language current language being processed * @return null */ public function writeStringsToFile(array $strings, $outputFiles = false, string $language = "") { if (empty($strings)) { return false; } // backup original language file copy($this->filePath, $this->filePath . '.old'); $this->translations = $this->parseFile->getTranslations(); $entries = []; foreach ($strings as $string) { if (isset($this->translations[$string['name']])) { $string['translation'] = $this->translations[$string['name']]; } else { // Handle punctuations at the end of the string (cf. comments in lib/init/tra.php) // For example, if the string is 'Login:', we put 'Login' for translation instead // (except if we already have an explicit translation for 'Login:', in which case we don't reach this else) $stringLength = strlen($string['name']); $stringLastChar = $string['name'][$stringLength - 1]; if (in_array($stringLastChar, Language::punctuations)) { $trimmedString = substr($string['name'], 0, $stringLength - 1); $string['name'] = $trimmedString; if (isset($this->translations[$trimmedString])) { $string['translation'] = $this->translations[$trimmedString]; } } } $entries[$string['name']] = $string; } $handle = fopen($this->tmpFilePath, 'w'); if ($handle) { fwrite($handle, "fileHeader()); if ($language != "en") { fwrite($handle, "include('lang/en/language.php'); // Needed for providing a sensible default text for untranslated strings with context like : \"edit_C(verb)\"\n"); fwrite($handle, "\$lang_current = array(\n"); // do not use short array syntax here yet for Transifex.com translation resource import (till they add support for the PHP short array syntax) } else { fwrite($handle, "\$lang = array(\n"); // do not use short array syntax here yet for Transifex.com translation resource import (till they add support for the PHP short array syntax) } foreach ($entries as $entry) { fwrite($handle, $this->formatString($entry, $outputFiles, $language)); } fwrite($handle, ");\n"); if ($language != "en") { fwrite($handle, "\$lang = array_replace(\$lang, \$lang_current);\n"); } fclose($handle); } rename($this->tmpFilePath, $this->filePath); } /** * Return the text used for language.php header * @return string */ protected function fileHeader() { $header = << \"$trans\",\n"; } else { $string .= "// \"$source\" => \"$source\",\n"; } return $string; } }