/** * @Route("/configs/{config}/domains/{domain}/locales/{locale}/messages", * name="jms_translation_update_message", * defaults = {"id" = null}, * options = {"i18n" = false}) * @Method("PUT") * @param Request $request * @param string $config * @param string $domain * @param string $locale * * @return Response */ public function updateMessageAction(Request $request, $config, $domain, $locale) { $id = $request->query->get('id'); $config = $this->configFactory->getConfig($config, $locale); $files = FileUtils::findTranslationFiles($config->getTranslationsDir()); if (!isset($files[$domain][$locale])) { throw new RuntimeException(sprintf('There is no translation file for domain "%s" and locale "%s".', $domain, $locale)); } // TODO: This needs more refactoring, the only sane way I see right now is to replace // the loaders of the translation component as these currently simply discard // the extra information that is contained in these files list($format, $file) = $files[$domain][$locale]; $this->updater->updateTranslation($file, $format, $domain, $locale, $id, $request->request->get('message')); return new Response('Translation was saved'); }
/** * @Route("/", name="jms_translation_index", options = {"i18n" = false}) * @Template * @param Request $request * @return array */ public function indexAction(Request $request) { $configs = $this->configFactory->getNames(); $config = $request->query->get('config') ?: reset($configs); if (!$config) { throw new RuntimeException('You need to configure at least one config under "jms_translation.configs".'); } $translationsDir = $this->configFactory->getConfig($config, 'en')->getTranslationsDir(); $files = FileUtils::findTranslationFiles($translationsDir); if (empty($files)) { throw new RuntimeException('There are no translation files for this config, please run the translation:extract command first.'); } $domains = array_keys($files); if (!($domain = $request->query->get('domain')) || !isset($files[$domain])) { $domain = reset($domains); } $locales = array_keys($files[$domain]); natsort($locales); if (!($locale = $request->query->get('locale')) || !isset($files[$domain][$locale])) { $locale = reset($locales); } $catalogue = $this->loader->loadFile($files[$domain][$locale][1]->getPathName(), $files[$domain][$locale][0], $locale, $domain); // create alternative messages // TODO: We should probably also add these to the XLIFF file for external translators, // and the specification already supports it $alternativeMessages = array(); foreach ($locales as $otherLocale) { if ($locale === $otherLocale) { continue; } $altCatalogue = $this->loader->loadFile($files[$domain][$otherLocale][1]->getPathName(), $files[$domain][$otherLocale][0], $otherLocale, $domain); foreach ($altCatalogue->getDomain($domain)->all() as $id => $message) { $alternativeMessages[$id][$otherLocale] = $message; } } $newMessages = $existingMessages = array(); foreach ($catalogue->getDomain($domain)->all() as $id => $message) { if ($message->isNew()) { $newMessages[$id] = $message; continue; } $existingMessages[$id] = $message; } return array('selectedConfig' => $config, 'configs' => $configs, 'selectedDomain' => $domain, 'domains' => $domains, 'selectedLocale' => $locale, 'locales' => $locales, 'format' => $files[$domain][$locale][0], 'newMessages' => $newMessages, 'existingMessages' => $existingMessages, 'alternativeMessages' => $alternativeMessages, 'isWriteable' => is_writeable($files[$domain][$locale][1]), 'file' => (string) $files[$domain][$locale][1], 'sourceLanguage' => $this->sourceLanguage); }