public function testAdd()
 {
     $catalogue = new MessageCatalogue('en', array('domain1' => array('foo' => 'foo'), 'domain2' => array('bar' => 'bar')));
     $catalogue->add(array('foo1' => 'foo1'), 'domain1');
     $this->assertEquals('foo', $catalogue->get('foo', 'domain1'));
     $this->assertEquals('foo1', $catalogue->get('foo1', 'domain1'));
     $catalogue->add(array('foo' => 'bar'), 'domain1');
     $this->assertEquals('bar', $catalogue->get('foo', 'domain1'));
     $this->assertEquals('foo1', $catalogue->get('foo1', 'domain1'));
     $catalogue->add(array('foo' => 'bar'), 'domain88');
     $this->assertEquals('bar', $catalogue->get('foo', 'domain88'));
 }
Esempio n. 2
0
 public function writeTranslationsByModule($module, $messages)
 {
     $currentMessages = $this->getMessagesByModule($module);
     $language = 'en';
     $resource = DRUPAL_ROOT . '/' . drupal_get_path('module', $module) . '/config/translations/';
     $messageCatalogue = new MessageCatalogue($language);
     if ($currentMessages && $currentMessages['messages']) {
         $messageCatalogue->add($currentMessages['messages'], 'console');
     }
     $messageCatalogue->add($messages, 'console');
     $translatorWriter = new TranslationWriter();
     $translatorWriter->addDumper('yaml', new YamlFileDumper());
     $translatorWriter->writeTranslations($messageCatalogue, 'yaml', ['path' => $resource, 'nest-level' => 10, 'indent' => 2]);
 }
Esempio n. 3
0
 public function testDumpWithCustomEncoding()
 {
     $catalogue = new MessageCatalogue('en');
     $catalogue->add(array('foo' => '"bar"'));
     $dumper = new JsonFileDumper();
     $this->assertStringEqualsFile(__DIR__ . '/../fixtures/resources.dump.json', $dumper->formatCatalogue($catalogue, 'messages', array('json_encoding' => JSON_HEX_QUOT)));
 }
 /**
  * Loads a locale.
  *
  * @param mixed $resource A resource
  * @param string $locale   A locale
  * @param string $domain   The domain
  *
  * @return MessageCatalogue A MessageCatalogue instance
  *
  * @throws NotFoundResourceException when the resource cannot be found
  * @throws InvalidResourceException  when the resource cannot be loaded
  */
 public function load($resource, $locale, $domain = 'messages')
 {
     $translations = $this->translationRepo->kvByLocaleAndDomain($locale, $domain);
     $catalogue = new MessageCatalogue($locale);
     $catalogue->add($translations, $domain);
     return $catalogue;
 }
Esempio n. 5
0
 public function testLinearFormatCatalogue()
 {
     $catalogue = new MessageCatalogue('en');
     $catalogue->add(array('foo.bar1' => 'value1', 'foo.bar2' => 'value2'));
     $dumper = new YamlFileDumper();
     $this->assertStringEqualsFile(__DIR__ . '/../fixtures/messages_linear.yml', $dumper->formatCatalogue($catalogue, 'messages'));
 }
 /**
  * {@inheritdoc}
  *
  * @api
  */
 public function load($resource, $locale, $domain = 'messages')
 {
     $this->flatten($resource);
     $catalogue = new MessageCatalogue($locale);
     $catalogue->add($resource, $domain);
     return $catalogue;
 }
Esempio n. 7
0
 public function testFormatCatalogue()
 {
     $catalogue = new MessageCatalogue('en');
     $catalogue->add(array('foo' => 'bar'), 'resources');
     $dumper = new QtFileDumper();
     $this->assertStringEqualsFile(__DIR__ . '/../fixtures/resources.ts', $dumper->formatCatalogue($catalogue, 'resources'));
 }
 /**
  *
  * {@inheritdoc}
  *
  */
 public function load($resource, $locale, $domain = 'messages')
 {
     if (!stream_is_local($resource)) {
         throw new InvalidResourceException(sprintf('This is not a local file "%s".', $resource));
     }
     if (!is_dir($resource)) {
         throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
     }
     try {
         $rb = new \ResourceBundle($locale, $resource);
     } catch (\Exception $e) {
         // HHVM compatibility: constructor throws on invalid resource
         $rb = null;
     }
     if (!$rb) {
         throw new InvalidResourceException(sprintf('Cannot load resource "%s"', $resource));
     } elseif (intl_is_failure($rb->getErrorCode())) {
         throw new InvalidResourceException($rb->getErrorMessage(), $rb->getErrorCode());
     }
     $messages = $this->flatten($rb);
     $catalogue = new MessageCatalogue($locale);
     $catalogue->add($messages, $domain);
     if (class_exists('Symfony\\Component\\Config\\Resource\\DirectoryResource')) {
         $catalogue->addResource(new DirectoryResource($resource));
     }
     return $catalogue;
 }
 private function getDatabaseCatalogue()
 {
     $databaseCatalogue = new MessageCatalogue(self::FAKE_LOCALE);
     $messages = array('baz' => 'Baz is updated !');
     $databaseCatalogue->add($messages, 'messages');
     return $databaseCatalogue;
 }
Esempio n. 10
0
    public function testTargetAttributesMetadataIsSetInFile()
    {
        $catalogue = new MessageCatalogue('en_US');
        $catalogue->add(array('foo' => 'bar'));
        $catalogue->setMetadata('foo', array('target-attributes' => array('state' => 'needs-translation')));
        $tempDir = sys_get_temp_dir();
        $dumper = new XliffFileDumper();
        $dumper->dump($catalogue, array('path' => $tempDir, 'default_locale' => 'fr_FR'));
        $content = <<<EOT
<?xml version="1.0" encoding="utf-8"?>
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">
  <file source-language="fr-FR" target-language="en-US" datatype="plaintext" original="file.ext">
    <body>
      <trans-unit id="acbd18db4cc2f85cedef654fccc4a4d8" resname="foo">
        <source>foo</source>
        <target state="needs-translation">bar</target>
      </trans-unit>
    </body>
  </file>
</xliff>

EOT;
        $this->assertEquals($content, file_get_contents($tempDir . '/messages.en_US.xlf'));
        unlink($tempDir . '/messages.en_US.xlf');
    }
 /**
  * {@inheritDoc}
  * @see \Symfony\Component\Translation\Loader\LoaderInterface::load()
  */
 public function load($resource, $locale, $domain = 'messages')
 {
     $repo = $this->service->getResourceRepository();
     if (!$repo->contains($resource)) {
         throw new NotFoundResourceException(sprintf('File "%s" not found.', $resource));
     }
     // find file in puli repo
     $file = $repo->get($resource);
     $json = $file->getBody();
     $data = Json::decode($json);
     $messages = [];
     // flatten plural strings
     foreach ($data as $key => $value) {
         if (is_array($value)) {
             $vals = [];
             foreach ($value as $k => $v) {
                 $vals[] = sprintf('%s: %s', $k, $v);
             }
             $val = implode('|', $vals);
         } else {
             $val = $value;
         }
         $messages[$key] = str_replace(['{{', '}}'], '%', $val);
     }
     // put them into message catalog
     $catalogue = new MessageCatalogue($locale);
     $catalogue->add($messages, $domain);
     return $catalogue;
 }
 public function testLoadWithMetadata()
 {
     $expected = new MessageCatalogue('en');
     $expected->add(array('foo' => 'bar'));
     $file = $this->getInputFile('with_metadata');
     $expected->addResource(new FileResource($file));
     $this->assertEquals($expected, $this->load($file));
 }
Esempio n. 13
0
 public function testDump()
 {
     $catalogue = new MessageCatalogue('en');
     $catalogue->add(array('foo' => 'bar'), 'resources');
     $dumper = new QtTranslationsDumper();
     $dumperString = $dumper->dump($catalogue, 'resources');
     $this->assertEquals(file_get_contents(__DIR__ . '/../fixtures/resources.ts'), $dumperString);
 }
Esempio n. 14
0
    public function testFormatCatalogue()
    {
        $catalogue = new MessageCatalogue('en');
        $catalogue->add(array('foo' => 'bar', 'bar' => 'foo
foo', 'foo;foo' => 'bar'));
        $dumper = new CsvFileDumper();
        $this->assertStringEqualsFile(__DIR__ . '/../fixtures/valid.csv', $dumper->formatCatalogue($catalogue, 'messages'));
    }
 public function testLoadOldFormat()
 {
     $expected = new MessageCatalogue('en');
     $expected->add(array('foo1' => 'bar', 'foo2' => 'bar', 'foo3' => 'bar', 'foo4' => 'bar'));
     $file = __DIR__ . '/xliff/old_format.xml';
     $expected->addResource(new FileResource($file));
     $this->assertEquals($expected, $this->getLoader()->load($file, 'en'));
 }
Esempio n. 16
0
 public function testDumpWithCustomEncoding()
 {
     $catalogue = new MessageCatalogue('en');
     $catalogue->add(array('foo' => '"bar"'));
     $dumper = new JsonFileDumper();
     $dumper->dump($catalogue, array('path' => $this->tempDir, 'json_encoding' => JSON_HEX_QUOT));
     $this->assertEquals(file_get_contents(__DIR__ . '/../fixtures/resources.dump.json'), file_get_contents($this->tempDir . '/messages.en.json'));
 }
 public function load($resource, $locale, $domain = 'messages')
 {
     $catalogue = new MessageCatalogue($locale);
     if ($resource instanceof \Closure) {
         $catalogue->add($resource());
     }
     return $catalogue;
 }
Esempio n. 18
0
 public function testFormatCatalogueWithTargetAttributesMetadata()
 {
     $catalogue = new MessageCatalogue('en_US');
     $catalogue->add(array('foo' => 'bar'));
     $catalogue->setMetadata('foo', array('target-attributes' => array('state' => 'needs-translation')));
     $dumper = new XliffFileDumper();
     $this->assertStringEqualsFile(__DIR__ . '/../fixtures/resources-target-attributes.xlf', $dumper->formatCatalogue($catalogue, 'messages', array('default_locale' => 'fr_FR')));
 }
Esempio n. 19
0
    public function testDump()
    {
        $catalogue = new MessageCatalogue('en');
        $catalogue->add(array('foo' => 'bar', 'bar' => 'foo
foo', 'foo;foo' => 'bar'));
        $dumper = new CsvDumper();
        $dumperString = $dumper->dump($catalogue);
        $this->assertEquals(file_get_contents(__DIR__ . '/../fixtures/valid.csv'), $dumperString);
    }
Esempio n. 20
0
 public function testDump()
 {
     $catalogue = new MessageCatalogue('en');
     $catalogue->add(array('foo' => 'bar'));
     $tempDir = sys_get_temp_dir();
     $dumper = new JsonFileDumper();
     $dumper->dump($catalogue, array('path' => $tempDir));
     $this->assertEquals(file_get_contents(__DIR__ . '/../fixtures/resources.json'), file_get_contents($tempDir . '/messages.en.json'));
     unlink($tempDir . '/messages.en.json');
 }
Esempio n. 21
0
 public function testFormatCatalogue()
 {
     if (!function_exists('mb_convert_encoding')) {
         $this->markTestSkipped('This test requires mbstring to work.');
     }
     $catalogue = new MessageCatalogue('en');
     $catalogue->add(array('foo' => 'bar'));
     $dumper = new IcuResFileDumper();
     $this->assertStringEqualsFile(__DIR__ . '/../fixtures/resourcebundle/res/en.res', $dumper->formatCatalogue($catalogue, 'messages'));
 }
Esempio n. 22
0
 /**
  *
  * {@inheritDoc} @see ArrayLoader::load()
  */
 public function load($resource, $locale, $domain = 'messages')
 {
     $messages = array();
     $catalogue = new MessageCatalogue($locale);
     $catalogue->add($resource, $domain);
     $messages = $this->parse($resource);
     $catalogue = parent::load($messages, $locale, $domain);
     $catalogue->addResource(new FileResource($resource));
     return $catalogue;
 }
 public function testDump()
 {
     $catalogue = new MessageCatalogue('en');
     $catalogue->add(array('foo' => 'bar', 'key' => '', 'key.with.cdata' => '<source> & <target>'));
     $tempDir = sys_get_temp_dir();
     $dumper = new XliffFileDumper();
     $dumper->dump($catalogue, array('path' => $tempDir));
     $this->assertSame(file_get_contents(__DIR__ . '/../fixtures/resources-clean.xlf'), file_get_contents($tempDir . '/messages.en.xlf'));
     unlink($tempDir . '/messages.en.xlf');
 }
Esempio n. 24
0
 public function testDumpWithTargetAttributesMetadata()
 {
     $catalogue = new MessageCatalogue('en_US');
     $catalogue->add(array('foo' => 'bar'));
     $catalogue->setMetadata('foo', array('target-attributes' => array('state' => 'needs-translation')));
     $this->tempDir = sys_get_temp_dir();
     $dumper = new XliffFileDumper();
     $dumper->dump($catalogue, array('path' => $this->tempDir, 'default_locale' => 'fr_FR'));
     $this->assertEquals(file_get_contents(__DIR__ . '/../fixtures/resources-target-attributes.xlf'), file_get_contents($this->tempDir . '/messages.en_US.xlf'));
     unlink($this->tempDir . '/messages.en_US.xlf');
 }
Esempio n. 25
0
    public function testDump()
    {
        $catalogue = new MessageCatalogue('en');
        $catalogue->add(array('foo' => 'bar', 'bar' => 'foo
foo', 'foo;foo' => 'bar'));
        $tempDir = sys_get_temp_dir();
        $dumper = new CsvFileDumper();
        $dumperString = $dumper->dump($catalogue, array('path' => $tempDir));
        $this->assertEquals(file_get_contents(__DIR__ . '/../fixtures/valid.csv'), file_get_contents($tempDir . '/messages.en.csv'));
        unlink($tempDir . '/messages.en.csv');
    }
 /**
  * @requires extension mbstring
  */
 public function testDump()
 {
     $catalogue = new MessageCatalogue('en');
     $catalogue->add(array('foo' => 'bar'));
     $tempDir = sys_get_temp_dir() . '/IcuResFileDumperTest';
     $dumper = new IcuResFileDumper();
     $dumper->dump($catalogue, array('path' => $tempDir));
     $this->assertEquals(file_get_contents(__DIR__ . '/../fixtures/resourcebundle/res/en.res'), file_get_contents($tempDir . '/messages/en.res'));
     @unlink($tempDir . '/messages/en.res');
     @rmdir($tempDir . '/messages');
     @rmdir($tempDir);
 }
Esempio n. 27
0
 public function testDump()
 {
     $catalogue = new MessageCatalogue('en_US');
     $catalogue->add(array('foo' => 'bar', 'key' => '', 'key.with.cdata' => '<source> & <target>'));
     $catalogue->setMetadata('foo', array('notes' => array(array('priority' => 1, 'from' => 'bar', 'content' => 'baz'))));
     $catalogue->setMetadata('key', array('notes' => array(array('content' => 'baz'), array('content' => 'qux'))));
     $tempDir = sys_get_temp_dir();
     $dumper = new XliffFileDumper();
     $dumper->dump($catalogue, array('path' => $tempDir, 'default_locale' => 'fr_FR'));
     $this->assertEquals(file_get_contents(__DIR__ . '/../fixtures/resources-clean.xlf'), file_get_contents($tempDir . '/messages.en_US.xlf'));
     unlink($tempDir . '/messages.en_US.xlf');
 }
Esempio n. 28
0
 public function testDump()
 {
     if (PHP_VERSION_ID < 50400) {
         $this->markTestIncomplete('PHP below 5.4 doesn\'t support JSON pretty printing');
     }
     $catalogue = new MessageCatalogue('en');
     $catalogue->add(array('foo' => 'bar'));
     $tempDir = sys_get_temp_dir();
     $dumper = new JsonFileDumper();
     $dumper->dump($catalogue, array('path' => $tempDir));
     $this->assertEquals(file_get_contents(__DIR__ . '/../fixtures/resources.json'), file_get_contents($tempDir . '/messages.en.json'));
     unlink($tempDir . '/messages.en.json');
 }
Esempio n. 29
0
 public function testDumpCreatesNestedDirectoriesAndFile()
 {
     $tempDir = sys_get_temp_dir();
     $translationsDir = $tempDir . '/test/translations';
     $file = $translationsDir . '/messages.en.concrete';
     $catalogue = new MessageCatalogue('en');
     $catalogue->add(array('foo' => 'bar'));
     $dumper = new ConcreteFileDumper();
     $dumper->setRelativePathTemplate('test/translations/%domain%.%locale%.%extension%');
     $dumper->dump($catalogue, array('path' => $tempDir));
     $this->assertTrue(file_exists($file));
     @unlink($file);
     @rmdir($translationsDir);
 }
 public function testDump()
 {
     if (!extension_loaded('mbstring')) {
         $this->markTestSkipped('This test requires mbstring to work.');
     }
     $catalogue = new MessageCatalogue('en');
     $catalogue->add(array('foo' => 'bar'));
     $tempDir = sys_get_temp_dir();
     $dumper = new IcuResFileDumper();
     $dumper->dump($catalogue, array('path' => $tempDir));
     $this->assertEquals(file_get_contents(__DIR__ . '/../fixtures/resourcebundle/res/en.res'), file_get_contents($tempDir . '/messages/en.res'));
     unlink($tempDir . '/messages/en.res');
     rmdir($tempDir . '/messages');
 }