private function createLanguageTranslationToken($locale, $translation, TranslationToken $token) { $language = new Language(); $language->setLocale($locale); $languageTranslationToken = new LanguageTranslationToken(); $languageTranslationToken->setLanguage($language); $languageTranslationToken->setTranslation($translation); $token->addLanguageTranslationToken($languageTranslationToken); self::$em->persist($language); self::$em->persist($languageTranslationToken); self::$em->flush(); return $languageTranslationToken; }
/** * @param array $currentConfig * * @return array */ public function merge(array $currentConfig) { $languages = array(); $dbLanguages = $this->em->getRepository(Language::clazz())->findBy(array('isEnabled' => true)); foreach ($dbLanguages as $dbLanguage) { $languages[] = array('id' => $dbLanguage->getId(), 'name' => $dbLanguage->getName()); } return array_merge($currentConfig, array('modera_backend_languages' => array('languages' => $languages))); }
private function checkEnabled($expected) { $actual = 0; $dbLanguages = self::$em->getRepository(Language::clazz())->findAll(); foreach ($dbLanguages as $dbLanguage) { if ($dbLanguage->getEnabled()) { ++$actual; } } $this->assertEquals($expected, $actual); }
public function testName() { $language = new Language(); $language->setLocale('en'); $this->assertEquals('English', $language->getName()); $this->assertEquals('Английский', $language->getName('ru')); $this->assertEquals('Inglise', $language->getName('et')); $language->setLocale('ru'); $this->assertEquals('Русский', $language->getName()); $this->assertEquals('Russian', $language->getName('en')); $this->assertEquals('Vene', $language->getName('et')); $language->setLocale('et'); $this->assertEquals('Eesti', $language->getName()); $this->assertEquals('Estonian', $language->getName('en')); $this->assertEquals('Эстонский', $language->getName('ru')); $language->setLocale('undefined'); $this->assertEquals('Undefined', $language->getName()); }
/** * @param Language $dbLanguage * * @return array */ private function tableRow(Language $dbLanguage) { return array($dbLanguage->getLocale(), $dbLanguage->getName(), $dbLanguage->getEnabled()); }
public static function dropDatabase() { self::$st->dropSchema([self::$em->getClassMetadata(Language::clazz())]); self::$st->dropSchema([self::$em->getClassMetadata(TranslationToken::clazz())]); self::$st->dropSchema([self::$em->getClassMetadata(LanguageTranslationToken::clazz())]); }
/** * @return array */ public function getConfig() { return array('entity' => Language::clazz(), 'hydration' => array('groups' => array('list' => ['id', 'name', 'locale', 'isEnabled']), 'profiles' => array('list'))); }
protected function execute(InputInterface $input, OutputInterface $output) { /* @var EntityManager $em */ $em = $this->getContainer()->get('doctrine.orm.entity_manager'); /* @var TranslationHandlersChain $thc */ $thc = $this->getContainer()->get('modera_translations.service.translation_handlers_chain'); $languages = $em->getRepository(Language::clazz())->findBy(array('isEnabled' => true)); if (!count($languages)) { $defaultLocale = $this->getContainer()->getParameter('locale'); $language = new Language(); $language->setLocale($defaultLocale); $language->setEnabled(true); $em->persist($language); $em->flush(); $languages = array($language); } $handlers = $thc->getHandlers(); if (count($handlers) > 0) { $imported = false; /* @var TranslationHandlerInterface $handler */ foreach ($handlers as $handler) { $bundleName = $handler->getBundleName(); foreach ($handler->getSources() as $source) { $tokens = $em->getRepository(TranslationToken::clazz())->findBy(array('source' => $source, 'bundleName' => $bundleName)); /* @var Language $language */ foreach ($languages as $language) { $locale = $language->getLocale(); $extractedCatalogue = $handler->extract($source, $locale); if (null === $extractedCatalogue) { continue; } $currentCatalogue = new MessageCatalogue($locale); /* @var TranslationToken $token */ foreach ($tokens as $token) { if ($token->isObsolete()) { continue; } /* @var LanguageTranslationToken $ltt */ foreach ($token->getLanguageTranslationTokens() as $ltt) { $lang = $ltt->getLanguage(); if ($lang && $lang->getLocale() == $locale) { $currentCatalogue->set($token->getTokenName(), $ltt->getTranslation(), $token->getDomain()); break; } } } // process catalogues $operation = new DiffOperation($currentCatalogue, $extractedCatalogue); foreach ($operation->getDomains() as $domain) { $newMessages = $operation->getNewMessages($domain); $obsoleteMessages = $operation->getObsoleteMessages($domain); if (count($newMessages) || count($obsoleteMessages)) { $imported = true; $output->writeln('>>> ' . $bundleName . ' : ' . $source . ' : ' . $locale . ' : ' . $domain); } if (count($newMessages)) { $output->writeln(sprintf(' <fg=green>New messages: %s</>', count($newMessages))); foreach ($newMessages as $tokenName => $translation) { $token = $this->findOrCreateTranslationToken($source, $bundleName, $domain, $tokenName); $token->setObsolete(false); $ltt = $em->getRepository(LanguageTranslationToken::clazz())->findOneBy(array('language' => $language, 'translationToken' => $token, 'translation' => $translation)); if (!$ltt) { $ltt = new LanguageTranslationToken(); $ltt->setLanguage($language); $token->addLanguageTranslationToken($ltt); } if ($ltt->isNew()) { $ltt->setTranslation($translation); } $em->persist($token); } $em->flush(); } if (count($obsoleteMessages)) { $output->writeln(sprintf(' <fg=red>Obsolete messages: %s</>', count($obsoleteMessages))); foreach ($obsoleteMessages as $tokenName => $translation) { $token = $this->findOrCreateTranslationToken($source, $bundleName, $domain, $tokenName); $token->setObsolete(true); $em->persist($token); } $em->flush(); } } } } } } if ($imported) { $output->writeln('>>> Translations have been successfully imported'); } else { $output->writeln('>>> Nothing to import'); } }