/** * 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 testGetters() { $file = new PoeditFile(); $file->addString(new String('foo', 'bar', array('comment1'), array(), array(), array(), true)); $file->addString(new String('bar', 'baz', array('comment1'))); $file->getString('bar')->setFuzzy(true); $file->addString(new String('qux')); $untranslated = $file->getUntranslated(); $fuzzy = $file->getFuzzy(); $translated = $file->getTranslated(); $deprecated = $file->getDeprecated(); $this->assertCount(1, $translated); $this->assertCount(1, $fuzzy); $this->assertCount(1, $untranslated); $this->assertCount(1, $deprecated); }