sourceFile. * @var Language_File */ private $targetFile; public function __construct(Language_File $sourceFile, Language_File $targetFile) { $this->sourceFile = $sourceFile; $this->targetFile = $targetFile; } public function merge() { $sourceStrings = $this->sourceFile->parse(); $targetStrings = $this->targetFile->parse(); $toUpdate = []; foreach ($targetStrings as $key => $string) { if ( isset($sourceStrings[$key]) && $sourceStrings[$key]['translated'] === true && (! isset($string['translation']) || $sourceStrings[$key]['translation'] != $string['translation']) ) { $toUpdate[$key] = $sourceStrings[$key]['translation']; } } // move the code that actually writes to the language.php to another class // that would handle every ways to write to a language.php file. $tmpFilePath = $this->targetFile->filePath . '.tmp'; $handle = fopen($tmpFilePath, 'w'); $lines = file($this->targetFile->filePath); if ($handle) { // foreach each line in the target file check decide whether to keep the // current translation or use the translation from the source file if one exists foreach ($lines as $line) { $matches = []; if ( preg_match('|^/?/?\s*\"(.*)\"\s*\=\>\s*\"(.*)\"\s*\,\s*$|', $line, $matches) && isset($toUpdate[$matches[1]]) ) { fwrite($handle, "\"{$matches[1]}\" => \"{$toUpdate[$matches[1]]}\",\n"); } else { fwrite($handle, $line); } } rename($tmpFilePath, $this->targetFile->filePath); } } }