protected function setUp() { parent::setUp(); $group = MessageGroupBase::factory($this->groupConfiguration); /** @var YamlFFS $ffs */ $this->ffs = $group->getFFS(); }
public function testParsing() { $file = <<<PROPERTIES \t\t\t# You are reading the ".properties" entry. ! The exclamation mark can also mark text as comments. website = <nowiki>http://en.wikipedia.org/</nowiki> language = English # The backslash below tells the application to continue reading # the value onto the next line. message = Welcome to \\ Wikipedia! # Add spaces to the key key\\ with\\ spaces = Value that can be looked up with "key with spaces". key-with-{curlies} = Value that can be looked up with "key-with-{curlies}". PROPERTIES; /** * @var FileBasedMessageGroup $group */ $group = MessageGroupBase::factory($this->groupConfiguration); $ffs = new JavaFFS($group); $parsed = $ffs->readFromVariable($file); $expected = array('website' => '<nowiki>http://en.wikipedia.org/</nowiki>', 'language' => 'English', 'message' => 'Welcome to Wikipedia!', 'key with spaces' => 'Value that can be looked up with "key with spaces".', 'key-with-=7Bcurlies=7D' => 'Value that can be looked up with "key-with-{curlies}".'); $expected = array('MESSAGES' => $expected, 'AUTHORS' => array()); $this->assertEquals($expected, $parsed); }
public function testSomeBlackList() { $conf = $this->groupConfiguration; $conf['LANGUAGES'] = array('blacklist' => array('or', 'hi')); $group = MessageGroupBase::factory($conf); $translatableLanguages = $group->getTranslatableLanguages(); $this->assertTrue(!isset($translatableLanguages['hi'])); $this->assertTrue(isset($translatableLanguages['he'])); }
public function testGenerateMessageBlock() { $ffs = MessageGroupBase::factory($this->conf)->getFFS(); $obj = new ReflectionObject($ffs); $method = $obj->getMethod('generateMessageBlock'); $method->setAccessible(true); $collection = new MockMessageCollectionForExport(); $mangler = StringMatcher::emptyMatcher(); $result = $method->invoke($ffs, $collection, $mangler); $expected = "\n\t'translatedmsg' => 'translation',\n\t'fuzzymsg' => 'translation', # Fuzzy\n"; $this->assertEquals($expected, $result); }
public static function isValid($data) { $conf = array('BASIC' => array('class' => 'FileBasedMessageGroup', 'namespace' => 8)); /** * @var FileBasedMessageGroup $group */ $group = MessageGroupBase::factory($conf); wfSuppressWarnings(); $ffs = new IniFFS($group); $parsed = $ffs->readFromVariable($data); wfRestoreWarnings(); return !!count($parsed['MESSAGES']); }
public function testExport() { global $wgSitename; $file = file_get_contents(__DIR__ . '/../data/IniFFSTest2.ini'); $file = str_replace('$wgSitename', $wgSitename, $file); $collection = new MockMessageCollectionForExport(); /** * @var FileBasedMessageGroup $group */ $group = MessageGroupBase::factory($this->groupConfiguration); $ffs = new IniFFS($group); $this->assertEquals($file, $ffs->writeIntoVariable($collection)); }
public function testFileRoundtrip() { $infile = file_get_contents(__DIR__ . '/../data/AppleFFSTest1.strings'); $group = MessageGroupBase::factory($this->groupConfiguration); $ffs = new AppleFFS($group); $parsed = $ffs->readFromVariable($infile); $outfile = ''; foreach ($parsed['MESSAGES'] as $key => $value) { $outfile .= AppleFFS::writeRow($key, $value); } $reparsed = $ffs->readFromVariable($outfile); $this->assertSame($parsed['MESSAGES'], $reparsed['MESSAGES'], "Messages survive roundtrip through write and read"); }
public function testParsing() { $group = MessageGroupBase::factory($this->groupConfiguration); $ffs = new XliffFFS($group); $file = file_get_contents(__DIR__ . '/../data/minimal.xlf'); $parsed = $ffs->readFromVariable($file, 'target'); $expected = array('1' => 'Hei maailma', '2' => TRANSLATE_FUZZY . 'Fuzzy translation', '3' => 'Tämä on <g id="1" ctype="bold">paksu</g>.'); $expected = array('MESSAGES' => $expected); $this->assertEquals($expected, $parsed); $parsed = $ffs->readFromVariable($file, 'source'); $expected = array('1' => 'Hello world', '2' => 'Fuzzy message', '3' => 'This is <g id="1" ctype="bold">bold</g>.'); $expected = array('MESSAGES' => $expected); $this->assertEquals($expected, $parsed); }
public function testWrite() { /** * @var FileBasedMessageGroup $group */ $group = MessageGroupBase::factory($this->groupConfiguration); $ffs = new AndroidXmlFFS($group); $messages = array('ko=26ra' => 'wawe', 'foobar' => '!!FUZZY!!Kissa kala <koira> "a\'b', 'amuch' => '{{PLURAL|one=bunny|other=bunnies}}'); $collection = new MockMessageCollection($messages); $xml = $ffs->writeIntoVariable($collection); $parsed = $ffs->readFromVariable($xml); $expected = array('MESSAGES' => $messages, 'AUTHORS' => array()); $this->assertEquals($expected, $parsed); }
public function testExport() { $collection = new MockMessageCollectionForExport(); /** * @var FileBasedMessageGroup $group */ $group = MessageGroupBase::factory($this->groupConfiguration); $ffs = new AmdFFS($group); $data = $ffs->writeIntoVariable($collection); $parsed = $ffs->readFromVariable($data); $this->assertEquals(array('Nike the bunny'), $parsed['AUTHORS'], 'Authors are exported'); $this->assertArrayHasKey('fuzzymsg', $parsed['MESSAGES'], 'fuzzy message is exported'); $this->assertArrayHasKey('translatedmsg', $parsed['MESSAGES'], 'translated message is exported'); if (array_key_exists('untranslatedmsg', $parsed['MESSAGES'])) { $this->fail('Untranslated messages should not be exported'); } }
public function testMsgctxtExport() { /** @var FileBasedMessageGroup $group */ $group = MessageGroupBase::factory($this->groupConfiguration); $ffs = new GettextFFS($group); $object = new ReflectionObject($ffs); $method = $object->getMethod('formatMessageBlock'); $method->setAccessible(true); $key = 'key'; $m = new FatMessage('key', 'definition'); $m->setTranslation('translation'); $trans = array(); $pot = array(); $pluralCount = 0; $results = <<<GETTEXT # msgid "definition" msgstr "translation" # msgctxt "" msgid "definition" msgstr "translation" # msgctxt "context" msgid "definition" msgstr "translation" GETTEXT; $results = preg_split('/\\n\\n/', $results); // Case 1: no context $this->assertEquals($results[0], trim($method->invoke($ffs, $key, $m, $trans, $pot, $pluralCount))); // Case 2: empty context $pot['ctxt'] = ''; $this->assertEquals($results[1], trim($method->invoke($ffs, $key, $m, $trans, $pot, $pluralCount))); // Case 3: context $pot['ctxt'] = 'context'; $this->assertEquals($results[2], trim($method->invoke($ffs, $key, $m, $trans, $pot, $pluralCount))); }
public function testParsing() { $file = <<<DTD \t\t\t<!-- # Messages for Interlingua (interlingua) # Exported from translatewiki.net # Author: McDutchie --> <!ENTITY okawix.title "Okawix &okawix.vernum; - Navigator de Wikipedia"> <!ENTITY okawix.back "Retro"> DTD; /** * @var FileBasedMessageGroup $group */ $group = MessageGroupBase::factory($this->groupConfiguration); $ffs = new DtdFFS($group); $parsed = $ffs->readFromVariable($file); $expected = array('okawix.title' => 'Okawix &okawix.vernum; - Navigator de Wikipedia', 'okawix.back' => 'Retro'); $expected = array('MESSAGES' => $expected, 'AUTHORS' => array('McDutchie')); $this->assertEquals($expected, $parsed); }
/** * Try parsing file. * @param $data * @return array */ protected function parseFile($data) { /** Construct a dummy group for us... * @todo Time to rethink the interface again? */ $group = MessageGroupBase::factory(array('FILES' => array('class' => 'GettextFFS', 'CtxtAsKey' => true), 'BASIC' => array('class' => 'FileBasedMessageGroup', 'namespace' => -1))); $ffs = new GettextFFS($group); $data = $ffs->readFromVariable($data); /** * Special data added by GettextFFS */ $metadata = $data['METADATA']; /** * This should catch everything that is not a gettext file exported from us */ if (!isset($metadata['code']) || !isset($metadata['group'])) { return array('no-headers'); } /** * And check for stupid editors that drop msgctxt which * unfortunately breaks submission. */ if (isset($metadata['warnings'])) { global $wgLang; return array('warnings', $wgLang->commaList($metadata['warnings'])); } return array('ok', $data); }
/** * Creates MediaWikiExtensionMessageGroup objects from parsed data. * @param string $id unique group id already prefixed * @param array $info array of group info * @return MediaWikiExtensionMessageGroup */ protected function createMessageGroup($id, $info) { $conf = array(); $conf['BASIC']['class'] = 'MediaWikiExtensionMessageGroup'; $conf['BASIC']['id'] = $id; $conf['BASIC']['namespace'] = $this->namespace; $conf['BASIC']['label'] = $info['name']; if (isset($info['desc'])) { $conf['BASIC']['description'] = $info['desc']; } else { $conf['BASIC']['descriptionmsg'] = $info['descmsg']; $conf['BASIC']['extensionurl'] = $info['url']; } if ($info['format'] === 'json') { $conf['FILES']['class'] = 'JsonFFS'; } else { $conf['FILES']['class'] = 'MediaWikiExtensionFFS'; } $conf['FILES']['sourcePattern'] = $this->path . '/' . $info['file']; // @todo Find a better way if (isset($info['aliasfile'])) { $conf['FILES']['aliasFileSource'] = $this->path . '/' . $info['aliasfile']; $conf['FILES']['aliasFile'] = $info['aliasfile']; } if (isset($info['magicfile'])) { $conf['FILES']['magicFileSource'] = $this->path . '/' . $info['magicfile']; $conf['FILES']['magicFile'] = $info['magicfile']; } if (isset($info['prefix'])) { $conf['MANGLER']['class'] = 'StringMatcher'; $conf['MANGLER']['prefix'] = $info['prefix']; $conf['MANGLER']['patterns'] = $info['mangle']; $mangler = new StringMatcher($info['prefix'], $info['mangle']); if (isset($info['ignored'])) { $info['ignored'] = $mangler->mangle($info['ignored']); } if (isset($info['optional'])) { $info['optional'] = $mangler->mangle($info['optional']); } } $conf['CHECKER']['class'] = 'MediaWikiMessageChecker'; $conf['CHECKER']['checks'] = array('pluralCheck', 'pluralFormsCheck', 'wikiParameterCheck', 'wikiLinksCheck', 'XhtmlCheck', 'braceBalanceCheck', 'pagenameMessagesCheck', 'miscMWChecks'); $conf['INSERTABLES']['class'] = 'MediaWikiInsertablesSuggester'; if (isset($info['optional'])) { $conf['TAGS']['optional'] = $info['optional']; } if (isset($info['ignored'])) { $conf['TAGS']['ignored'] = $info['ignored']; } return MessageGroupBase::factory($conf); }
/** * @expectedException MWException * @expectedExceptionMessage No valid namespace defined */ public function testGetNamespaceInvalid() { $conf = $this->groupConfiguration; $conf['BASIC']['namespace'] = 'ergweofijwef'; MessageGroupBase::factory( $conf ); }
/** * Constructs a FileBasedMessageGroup from any normal message group. * Useful for doing special Gettext exports from any group. * @param $group MessageGroup * @return FileBasedMessageGroup */ public static function newFromMessageGroup($group) { $conf = array('BASIC' => array('class' => 'FileBasedMessageGroup', 'id' => $group->getId(), 'label' => $group->getLabel(), 'namespace' => $group->getNamespace()), 'FILES' => array('sourcePattern' => '', 'targetPattern' => '')); return MessageGroupBase::factory($conf); }
/** * This constructs the list of all groups from multiple different * sources. When possible, a cache dependency is created to automatically * recreate the cache when configuration changes. * @todo Reduce the ways of which messages can be added. Target is just * to have three ways: Yaml files, translatable pages and with the hook. * @todo In conjuction with the above, reduce the number of global * variables like wgTranslate#C and have the message groups specify * their own cache dependencies. */ protected static function loadGroupDefinitions() { global $wgTranslateAddMWExtensionGroups; global $wgEnablePageTranslation, $wgTranslateGroupFiles; global $wgTranslateAC, $wgTranslateEC, $wgTranslateCC; global $wgAutoloadClasses; global $wgTranslateWorkflowStates; $deps = array(); $deps[] = new GlobalDependency('wgTranslateAddMWExtensionGroups'); $deps[] = new GlobalDependency('wgEnablePageTranslation'); $deps[] = new GlobalDependency('wgTranslateGroupFiles'); $deps[] = new GlobalDependency('wgTranslateAC'); $deps[] = new GlobalDependency('wgTranslateEC'); $deps[] = new GlobalDependency('wgTranslateCC'); $deps[] = new GlobalDependency('wgTranslateExtensionDirectory'); $deps[] = new GlobalDependency('wgTranslateWorkflowStates'); $deps[] = new FileDependency(dirname(__FILE__) . '/groups/mediawiki-defines.txt'); $deps[] = new FileDependency(dirname(__FILE__) . '/groups/Wikia/extensions.txt'); $deps[] = new FileDependency(dirname(__FILE__) . '/groups/Toolserver/toolserver-textdomains.txt'); if ($wgTranslateAddMWExtensionGroups) { $a = new PremadeMediawikiExtensionGroups(); $a->addAll(); } if ($wgEnablePageTranslation) { $dbr = wfGetDB(DB_MASTER); $tables = array('page', 'revtag'); $vars = array('page_id', 'page_namespace', 'page_title'); $conds = array('page_id=rt_page', 'rt_type' => RevTag::getType('tp:mark')); $options = array('GROUP BY' => 'rt_page'); $res = $dbr->select($tables, $vars, $conds, __METHOD__, $options); foreach ($res as $r) { $title = Title::makeTitle($r->page_namespace, $r->page_title); $id = TranslatablePage::getMessageGroupIdFromTitle($title); $wgTranslateCC[$id] = new WikiPageMessageGroup($id, $title); $wgTranslateCC[$id]->setLabel($title->getPrefixedText()); } } if ($wgTranslateWorkflowStates) { $wgTranslateCC['translate-workflow-states'] = new WorkflowStatesMessageGroup(); } $autoload = array(); wfRunHooks('TranslatePostInitGroups', array(&$wgTranslateCC, &$deps, &$autoload)); foreach ($wgTranslateGroupFiles as $configFile) { wfDebug($configFile . "\n"); $deps[] = new FileDependency(realpath($configFile)); $fgroups = TranslateYaml::parseGroupFile($configFile); foreach ($fgroups as $id => $conf) { if (!empty($conf['AUTOLOAD']) && is_array($conf['AUTOLOAD'])) { $dir = dirname($configFile); foreach ($conf['AUTOLOAD'] as $class => $file) { // For this request and for caching. $wgAutoloadClasses[$class] = "{$dir}/{$file}"; $autoload[$class] = "{$dir}/{$file}"; } } $group = MessageGroupBase::factory($conf); $wgTranslateCC[$id] = $group; } } $key = wfMemckey('translate-groups'); $value = array('ac' => $wgTranslateAC, 'ec' => $wgTranslateEC, 'cc' => $wgTranslateCC, 'autoload' => $autoload); $wrapper = new DependencyWrapper($value, $deps); $wrapper->storeToCache(self::getCache(), $key, 60 * 60 * 2); wfDebug(__METHOD__ . "-end\n"); }
protected function setUp() { parent::setUp(); $this->group = MessageGroupBase::factory( $this->groupConfiguration ); }
/** * Get all the aggregate messages groups defined in translate_metadata table. * * @return array */ protected static function loadAggregateGroups() { $dbw = wfGetDB(DB_MASTER); $tables = array('translate_metadata'); $fields = array('tmd_group', 'tmd_value'); $conds = array('tmd_key' => 'subgroups'); $res = $dbw->select($tables, $fields, $conds, __METHOD__); $groups = array(); foreach ($res as $row) { $id = $row->tmd_group; $conf = array(); $conf['BASIC'] = array('id' => $id, 'label' => TranslateMetadata::get($id, 'name'), 'description' => TranslateMetadata::get($id, 'description'), 'meta' => 1, 'class' => 'AggregateMessageGroup', 'namespace' => NS_TRANSLATIONS); $conf['GROUPS'] = TranslateMetadata::getSubgroups($id); $group = MessageGroupBase::factory($conf); $groups[$id] = $group; } return $groups; }