/** * Dump PoeditFile translated keys into key/value .json file * * @param PoeditFile $file * @param string $filename * * @throws MissingFileLanguageException if no language is set and we have plural forms * * @return boolean */ public function dump(PoeditFile $file, $filename = null) { $content = array(); $pluralForms = null; $filename = null !== $filename ? $filename : $this->file; $strings = $file->getTranslated(); foreach ($strings as $string) { if ($string instanceof PoeditPluralString) { // if we found a plural form, we need to look for file language if (null === $pluralForms) { $pluralForms = $this->getLangPluralForm($file->getLang()); } if (null === $file->getLang()) { throw new MissingFileLanguageException('Your .po file must have a language defined in order to dump plural forms', 1); } $content[$string->getKey()] = array_combine($pluralForms, $string->getPlurals()); } else { $content[$string->getKey()] = $string->getValue(); } } $content = json_encode($content, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP); // fix some unwanted backslashes, auto-escaped by json_encode() $content = str_replace(array('\\\\n', '\\\\r', '\\\\t'), array('\\n', '\\r', '\\t'), $content); // ensure that path and file exists File::mkdirr(substr($filename, 0, strrpos($filename, '/'))); return false !== file_put_contents($filename, $content); }
public function testMkdirr() { $this->assertTrue(File::mkdirr(vfsStream::url('root/foo'))); $this->assertFalse(File::mkdirr(vfsStream::url('root/foo/bar.po'))); $this->assertTrue(File::mkdirr(vfsStream::url('root/foo/foo.po'))); $this->assertTrue($this->root->hasChild('foo/foo.po')); $this->assertTrue(File::mkdirr(vfsStream::url('root/bar/foo.po'))); $this->assertTrue($this->root->hasChild('bar/foo.po')); }
/** * Dump PoeditFile into .po file * * @param PoeditFile $file * @param string $filename * @param boolean $sort if enabled, sort strings and their comments. implemented to avoid too many git conflicts * @param string $charset * * @return boolean */ public function dump(PoeditFile $file, $filename = null, $sort = false, $charset = 'UTF-8') { $filename = null !== $filename ? $filename : $this->file; $content = $file->getHeaders() . PHP_EOL; $content .= "\"Content-Type: text/plain; charset=" . $charset . "\\n\"" . PHP_EOL . PHP_EOL; $strings = true === $sort ? $file->sortStrings()->getStrings() : $file->getStrings(); foreach ($strings as $string) { $content .= true === $sort ? $string->sortReferences()->sortComments()->sortExtracteds()->sortFlags() : $string; } // ensure that path and file exists File::mkdirr(substr($filename, 0, strrpos($filename, '/'))); return false !== file_put_contents($filename, $content); }