Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
fchris82 committed Jan 4, 2017
2 parents 7872fd5 + 5b128e4 commit 6988c50
Show file tree
Hide file tree
Showing 18 changed files with 244 additions and 27 deletions.
41 changes: 41 additions & 0 deletions Annotation/AltTrans.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/*
* Copyright 2011 Johannes M. Schmitt <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace JMS\TranslationBundle\Annotation;

use Doctrine\Common\Annotations\Annotation\Required;

/**
* @Annotation
*
* @author Krisztián Ferenczi <[email protected]>
*/
final class AltTrans
{
/**
* @var string
* @Required
*/
public $text;

/**
* @var string
* @Required
*/
public $locale;
}
41 changes: 41 additions & 0 deletions Model/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ class Message
/** The placeholders */
protected $placeholders = array();

/**
* The key is the `locale` value, the value is the `translation`
*
* @var array
*/
protected $alternativeTranslations = array();

/**
* @static
*
Expand Down Expand Up @@ -424,6 +431,40 @@ public function getPlaceholders()
return $this->placeholders;
}

public function getAlternativeTranslation($locale, $default = null)
{
return array_key_exists($locale, $this->alternativeTranslations)
? $this->alternativeTranslations[$locale]
: $default;
}

/**
* @return array
*/
public function getAlternativeTranslations()
{
return $this->alternativeTranslations;
}

/**
* @param array $alternativeTranslations
*
* @return $this
*/
public function setAlternativeTranslations($alternativeTranslations)
{
$this->alternativeTranslations = $alternativeTranslations;

return $this;
}

public function addAlternativeTranslation($locale, $translation)
{
$this->alternativeTranslations[$locale] = $translation;

return $this;
}

/**
* Allows us to use this with existing message catalogues.
*
Expand Down
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,20 @@ Differences:
- Command performance upgrade: scan files only ones and not per languages!
- Sort source
- Collect the placeholders (`<jms:placeholder>` element)
- Move meaning to `<note>` element
- Move meaning to `jms:meaning` attribute
- Custom translated form fields
- Add new configurations: `output_options` -> `add_date` and `add_filerefs`
- Handle `addViolation()`, `addViolationAt()` and `buildViolation()` functions
- Add new Annotation: `AltTrans` . You can add basic translations:

```php
<?php

/** @AltTrans("User has been created: <a href=""mailto:%email%"">%email%</a>", locale="en") */
$this->trans('user.create.success.%email%', ['%email%' => $user->getEmail()]);
```

> The double `""` sign is the escaped `"` in the `AltTrans` annotation value.
JMSTranslationBundle [![Build Status](https://secure.travis-ci.org/schmittjoh/JMSTranslationBundle.png?branch=master)](http://travis-ci.org/schmittjoh/JMSTranslationBundle) [![Join the chat at https://gitter.im/schmittjoh/JMSTranslationBundle](https://badges.gitter.im/schmittjoh/JMSTranslationBundle.svg)](https://gitter.im/schmittjoh/JMSTranslationBundle?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
====================
Expand Down
62 changes: 62 additions & 0 deletions Tests/Translation/Dumper/XliffDumperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,68 @@ public function testDumpStructureWithoutReference()
$this->assertEquals($this->getOutput('structure_without_reference'), $dumper->dump($catalogue, 'messages'));
}

/**
* Test existing alternative translation with different language --> nothing
*/
public function testDumpAlternativeTranslation1()
{
$catalogue = new MessageCatalogue();
$catalogue->setLocale('en');

$message = new Message('foo');
$message->addAlternativeTranslation('de', 'der Foo');
$catalogue->add($message);

$this->assertEquals($this->getOutput('simple'), $this->getDumper()->dump($catalogue, 'messages'));
}

/**
* Test existing alternative translation --> changing
*/
public function testDumpAlternativeTranslation2a()
{
$catalogue = new MessageCatalogue();
$catalogue->setLocale('en');

$message = new Message('foo');
$message->addAlternativeTranslation('en', 'alternate');
$catalogue->add($message);

$this->assertEquals($this->getOutput('alternative_translation'), $this->getDumper()->dump($catalogue, 'messages'));
}

/**
* Test existing alternative translation --> changing
*/
public function testDumpAlternativeTranslation2b()
{
$catalogue = new MessageCatalogue();
$catalogue->setLocale('en');

$message = new Message('foo');
$message->setLocaleString('foo');
$message->addAlternativeTranslation('en', 'alternate');
$catalogue->add($message);

$this->assertEquals($this->getOutput('alternative_translation'), $this->getDumper()->dump($catalogue, 'messages'));
}

/**
* Test existing alternative translation, with translated message --> nothing
*/
public function testDumpAlternativeTranslation3()
{
$catalogue = new MessageCatalogue();
$catalogue->setLocale('en');

$message = new Message('foo');
$message->setLocaleString('alternate');
$message->addAlternativeTranslation('en', 'original alternate');
$catalogue->add($message);

$this->assertEquals($this->getOutput('alternative_translation'), $this->getDumper()->dump($catalogue, 'messages'));
}

/**
* Get the catalogue used for the structure tests
*
Expand Down
15 changes: 15 additions & 0 deletions Tests/Translation/Dumper/xliff/alternative_translation.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:jms="urn:jms:translation" version="1.2">
<file source-language="en" target-language="en" datatype="plaintext" original="not.available">
<header>
<tool tool-id="JMSTranslationBundle" tool-name="JMSTranslationBundle" tool-version="1.1.0-DEV"/>
<note>The source node in most cases contains the sample message as written by the developer. If it looks like a dot-delimitted string such as "form.label.firstname", then the developer has not provided a default message.</note>
</header>
<body>
<trans-unit id="0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33" resname="foo">
<source>foo</source>
<target state="new">alternate</target>
</trans-unit>
</body>
</file>
</xliff>
3 changes: 1 addition & 2 deletions Tests/Translation/Dumper/xliff/structure_with_metadata.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
<source>Foo</source>
<target state="new">Foo</target>
</trans-unit>
<trans-unit id="5deead3ddeb268e61dbdf8809681afc61eabf8b4" resname="foo.bar.moo">
<trans-unit id="5deead3ddeb268e61dbdf8809681afc61eabf8b4" resname="foo.bar.moo" jms:meaning="Bar">
<source>foo.bar.moo</source>
<target state="new">foo.bar.moo</target>
<note>Bar</note>
</trans-unit>
<trans-unit id="cbcf5a897f5c5d204d78c3c4b2fdd517559cb1b9" resname="foo.baz">
<source>foo.baz</source>
Expand Down
3 changes: 1 addition & 2 deletions Tests/Translation/Dumper/xliff/with_metadata.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
<note>The source node in most cases contains the sample message as written by the developer. If it looks like a dot-delimitted string such as "form.label.firstname", then the developer has not provided a default message.</note>
</header>
<body>
<trans-unit id="0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33" resname="foo">
<trans-unit id="0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33" resname="foo" jms:meaning="baz">
<source>bar</source>
<target state="new">bar</target>
<note>baz</note>
</trans-unit>
</body>
</file>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ final protected function getDocParser()
'desc' => 'JMS\TranslationBundle\Annotation\Desc',
'meaning' => 'JMS\TranslationBundle\Annotation\Meaning',
'ignore' => 'JMS\TranslationBundle\Annotation\Ignore',
'alttrans' => 'JMS\TranslationBundle\Annotation\AltTrans',
));
$docParser->setIgnoreNotImportedAnnotations(true);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ public function testExtractController()
$message->addPlaceholder('%foo%');
$expected->add($message);

$message = new Message('text.alternative.en');
$message->addSource($fileSourceFactory->create($fixtureSplInfo, 88));
$message->addPlaceholder('%bar%');
$message->addAlternativeTranslation('en', 'This is the alternative <a href="http://example.com">translations &raquo;</a>');
$expected->add($message);

$this->assertEquals($expected, $catalogue);
}

Expand Down
6 changes: 6 additions & 0 deletions Tests/Translation/Extractor/File/Fixture/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,10 @@ public function assignToVar()
/** @Desc("The var %foo% should be assigned.") */
return $this->translator->trans('text.var.assign', array('%foo%' => 'fooVar'));
}

public function testAlternativeTranslations()
{
/** @AltTrans("This is the alternative <a href=""http://example.com"">translations &raquo;</a>", locale="en") */
return $this->translator->trans('text.alternative.en', array('%bar%' => 'barVar'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function buildForm(FormBuilder $builder, array $options)
'label' => /** @Desc("Lastname") */ 'form.label.lastname',
))
->add('street', 'text', array(
'label' => /** @Desc("Street") */ 'form.label.street',
'label' => /** @Desc("Street") @AltTrans("Street", locale="en") */ 'form.label.street',
'translation_domain' => 'address'
))
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function buildForm(FormBuilder $builder, array $options)
'placeholder' => /** @Desc("Choice field with a placeholder") */ 'form.choice_placeholder'
))
->add('field_empty_value', 'choice', array(
'empty_value' => /** @Desc("Choice field with an empty_value") */ 'form.choice_empty_value'
'empty_value' => /** @Desc("Choice field with an empty_value") @AltTrans("Empty field", locale="en") */ 'form.choice_empty_value'
))
;
}
Expand Down
2 changes: 2 additions & 0 deletions Tests/Translation/Extractor/File/FormExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public function testPlaceholderExtract()
$message = new Message('form.choice_empty_value');
$message->setDesc('Choice field with an empty_value');
$message->addSource($fileSourceFactory->create($fixtureSplInfo, 40));
$message->addAlternativeTranslation('en', 'Empty field');
$expected->add($message);

$this->assertEquals($expected, $this->extract('MyPlaceholderFormType.php'));
Expand Down Expand Up @@ -218,6 +219,7 @@ public function testExtractWithDefaultDomain()
$message = new Message('form.label.street', 'address');
$message->setDesc('Street');
$message->addSource($fileSourceFactory->create($fixtureSplInfo, 37));
$message->addAlternativeTranslation('en', 'Street');
$expected->add($message);

$this->assertEquals($expected, $this->extract('MyFormTypeWithDefaultDomain.php'));
Expand Down
21 changes: 11 additions & 10 deletions Translation/Dumper/XliffDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,18 @@ public function dump(MessageCatalogue $catalogue, $domain = 'messages', $filePat
}

$unit->appendChild($target = $doc->createElement('target'));
if (preg_match('/[<>&]/', $message->getLocaleString())) {
$target->appendChild($doc->createCDATASection($message->getLocaleString()));
$localeString = $message->getLocaleString();
if ($localeString == $message->getSourceString()
&& $altTrans = $message->getAlternativeTranslation($catalogue->getLocale())
) {
$localeString = $altTrans;
}
if (preg_match('/[<>&]/', $localeString)) {
$target->appendChild($doc->createCDATASection($localeString));
} else {
$target->appendChild($doc->createTextNode($message->getLocaleString()));
$target->appendChild($doc->createTextNode($localeString));

if (preg_match("/\r\n|\n|\r|\t/", $message->getLocaleString())) {
if (preg_match("/\r\n|\n|\r|\t/", $localeString)) {
$target->setAttribute('xml:space', 'preserve');
}
}
Expand Down Expand Up @@ -239,12 +245,7 @@ public function dump(MessageCatalogue $catalogue, $domain = 'messages', $filePat
}

if ($meaning = $message->getMeaning()) {
$unit->appendChild($meaningNode = $doc->createElement('note'));
if (preg_match('/[<>&]/', $meaning)) {
$meaningNode->appendChild($doc->createCDATASection($meaning));
} else {
$meaningNode->appendChild($doc->createTextNode($meaning));
}
$unit->setAttribute('jms:meaning', $meaning);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

use JMS\TranslationBundle\Exception\RuntimeException;
use JMS\TranslationBundle\Model\Message;
use JMS\TranslationBundle\Annotation\AltTrans;
use JMS\TranslationBundle\Annotation\Meaning;
use JMS\TranslationBundle\Annotation\Desc;
use JMS\TranslationBundle\Annotation\Ignore;
Expand Down Expand Up @@ -171,6 +172,7 @@ public function enterNode(Node $node)

$ignore = false;
$desc = $meaning = null;
$alternativeTranslations = [];
if ($docComment = $node->getDocComment()) {
foreach ($this->docParser->parse($docComment->getText(), 'file '.$this->file.' near line '.$node->getLine()) as $annot) {
if ($annot instanceof Ignore) {
Expand All @@ -179,6 +181,8 @@ public function enterNode(Node $node)
$desc = $annot->text;
} elseif ($annot instanceof Meaning) {
$meaning = $annot->text;
} elseif ($annot instanceof AltTrans) {
$alternativeTranslations[$annot->locale] = $annot->text;
}
}
}
Expand All @@ -202,6 +206,7 @@ public function enterNode(Node $node)
->setDesc($desc)
->setMeaning($meaning)
->addSource($this->fileSourceFactory->create($this->file, $node->expr->getLine()))
->setAlternativeTranslations($alternativeTranslations)
;

$this->catalogue->add($message);
Expand Down
Loading

0 comments on commit 6988c50

Please sign in to comment.