Esempio n. 1
0
 public function testFallbackLocale()
 {
     $app = $this->getApp();
     $tr1 = new TranslationFile($app, 'infos', 'en_GB');
     $tr2 = new TranslationFile($app, 'infos', 'en_CO');
     $content1 = $tr1->content();
     $content2 = $tr2->content();
     $this->assertSame($content1, $content2);
 }
Esempio n. 2
0
 /**
  * Get the translation data.
  *
  * @param array $tr
  *
  * @return string
  */
 private function getTranslationData(array &$tr)
 {
     $translation = new TranslationFile($this->app, $tr['domain'], $tr['locale']);
     list($tr['path'], $tr['shortPath']) = $translation->path();
     $this->app['logger.system']->info('Editing translation: ' . $tr['shortPath'], ['event' => 'translation']);
     $tr['writeallowed'] = $translation->isWriteAllowed();
     return ['contents' => $translation->content()];
 }
Esempio n. 3
0
 /**
  * Prepare/edit/save a translation.
  *
  * @param string      $domain    The domain
  * @param string      $tr_locale The translation locale
  * @param Application $app       The application/container
  * @param Request     $request   The Symfony Request
  *
  * @return \Twig_Markup|\Symfony\Component\HttpFoundation\RedirectResponse
  */
 public function translation($domain, $tr_locale, Application $app, Request $request)
 {
     $translation = new TranslationFile($app, $domain, $tr_locale);
     list($path, $shortPath) = $translation->path();
     $app['logger.system']->info('Editing translation: ' . $shortPath, array('event' => 'translation'));
     $data = array('contents' => $translation->content());
     $writeallowed = $translation->isWriteAllowed();
     $form = $app['form.factory']->createBuilder('form', $data)->add('contents', 'textarea', array('constraints' => array(new Assert\NotBlank(), new Assert\Length(array('min' => 10)))))->getForm();
     // Check if the form was POST-ed, and valid. If so, store the file.
     if ($request->isMethod('POST')) {
         $form->submit($app['request']->get($form->getName()));
         if ($form->isValid()) {
             $data = $form->getData();
             $contents = Input::cleanPostedData($data['contents']) . "\n";
             // Before trying to save a yaml file, check if it's valid.
             try {
                 $ok = Yaml\Yaml::parse($contents);
             } catch (ParseException $e) {
                 $ok = false;
                 $msg = Trans::__("File '%s' could not be saved:", array('%s' => $shortPath));
                 $app['session']->getFlashBag()->add('error', $msg . $e->getMessage());
             }
             // Before trying to save, check if it's writable.
             if ($ok) {
                 // clear any warning for file not found, we are creating it here
                 // we'll set an error if someone still submits the form and write is not allowed
                 $app['session']->getFlashBag()->clear('warning');
                 if (!$writeallowed) {
                     $msg = Trans::__("The file '%s' is not writable. You will have to use your own editor to make modifications to this file.", array('%s' => $shortPath));
                     $app['session']->getFlashBag()->add('error', $msg);
                 } else {
                     file_put_contents($path, $contents);
                     $msg = Trans::__("File '%s' has been saved.", array('%s' => $shortPath));
                     $app['session']->getFlashBag()->add('info', $msg);
                     return Lib::redirect('translation', array('domain' => $domain, 'tr_locale' => $tr_locale));
                 }
             }
         }
     }
     $context = array('form' => $form->createView(), 'basename' => basename($shortPath), 'filetype' => 'yml', 'write_allowed' => $writeallowed);
     return $app['render']->render('editlocale/editlocale.twig', array('context' => $context));
 }