Skip to content

support usage of translation domain adding "resource_files" compiled debug container #1445

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.w3c.dom.NodeList;

import java.io.InputStream;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
Expand All @@ -19,7 +20,9 @@ public class DomainMappings extends AbstractServiceParser {

@Override
public String getXPathFilter() {
return "/container/services/service[@class=\"Symfony\\Bundle\\FrameworkBundle\\Translation\\Translator\"]//call[@method=\"addResource\"]";
return
"/container/services/service[@class=\"Symfony\\Bundle\\FrameworkBundle\\Translation\\Translator\"]//call[@method=\"addResource\"]" // Symfony < 4
+ " | /container/services/service[@class=\"Symfony\\Bundle\\FrameworkBundle\\Translation\\Translator\"]//argument[@key=\"resource_files\"]/argument/argument";
}

public void parser(InputStream file) {
Expand All @@ -32,17 +35,37 @@ public void parser(InputStream file) {

for (int i = 0; i < nodeList.getLength(); i++) {
Element node = (Element) nodeList.item(i);
NodeList arguments = node.getElementsByTagName("argument");
String tagName = node.getTagName();

if(arguments.getLength() == 4) {
this.domainFileMaps.add(new DomainFileMap(arguments.item(0).getTextContent(), arguments.item(1).getTextContent(), arguments.item(2).getTextContent(), arguments.item(3).getTextContent()));
if ("call".equals(tagName)) {
// Symfony < 4: via addResource
NodeList arguments = node.getElementsByTagName("argument");

if(arguments.getLength() == 4) {
this.domainFileMaps.add(new DomainFileMap(arguments.item(0).getTextContent(), arguments.item(1).getTextContent(), arguments.item(2).getTextContent(), arguments.item(3).getTextContent()));
}
} else if("argument".equals(tagName)) {
// Symfony 5: arguments in constructor

// normalize path name
String path = node.getTextContent().replace("\\", "/");

// get file name
int filenameStart = path.lastIndexOf("/");
if (filenameStart > 0) {
String filename = path.substring(filenameStart + 1);

// split by filename: validators.af.xlf
String[] split = filename.split("\\.");
if (split.length == 3) {
this.domainFileMaps.add(new DomainFileMap(split[2], path, split[1], split[0]));
}
}
}
}

}

public List<DomainFileMap> getDomainFileMaps() {
public Collection<DomainFileMap> getDomainFileMaps() {
return Collections.synchronizedList(domainFileMaps);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,37 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.List;

import java.util.Collection;

/**
* @author Daniel Espendiller <daniel@espendiller.net>
*/
public class DomainMappingsTest extends Assert {

@Test
public void testParser() throws FileNotFoundException {
File testFile = new File("src/test/java/fr/adrienbrault/idea/symfony2plugin/tests/translation/parser/appDevDebugProjectContainer.xml");

DomainMappings bla = new DomainMappings();
bla.parser(new FileInputStream(testFile));
List<DomainFileMap> map = bla.getDomainFileMaps();
DomainMappings domainMappings = new DomainMappings();
domainMappings.parser(new FileInputStream(testFile));
Collection<DomainFileMap> domainFileMaps = domainMappings.getDomainFileMaps();

assertEquals(10, map.size());
assertEquals(14, domainFileMaps.size());

DomainFileMap firstDomain = map.iterator().next();
DomainFileMap craueFormFlowBundle = domainFileMaps.stream().filter(domainFileMap -> domainFileMap.getDomain().equals("CraueFormFlowBundle")).findFirst().get();

assertEquals("CraueFormFlowBundle", firstDomain.getDomain());
assertEquals("de", firstDomain.getLanguageKey());
assertEquals("yml", firstDomain.getLoader());
assertTrue(firstDomain.getPath().endsWith("CraueFormFlowBundle.de.yml"));
assertEquals("CraueFormFlowBundle", craueFormFlowBundle.getDomain());
assertEquals("de", craueFormFlowBundle.getLanguageKey());
assertEquals("yml", craueFormFlowBundle.getLoader());
assertTrue(craueFormFlowBundle.getPath().endsWith("CraueFormFlowBundle.de.yml"));

}
DomainFileMap foobarDomain1 = domainFileMaps.stream().filter(domainFileMap -> domainFileMap.getDomain().equals("foobar_domain1")).findFirst().get();
assertEquals("foobar_domain1", foobarDomain1.getDomain());
assertEquals("af", foobarDomain1.getLanguageKey());
assertEquals("xlf", foobarDomain1.getLoader());
assertTrue(foobarDomain1.getPath().endsWith("foobar_domain1.af.xlf"));

assertNotNull(domainFileMaps.stream().filter(domainFileMap -> domainFileMap.getDomain().equals("foobar_domain2")).findFirst().get());
assertNotNull(domainFileMaps.stream().filter(domainFileMap -> domainFileMap.getDomain().equals("foobar_domain3")).findFirst().get());
assertNotNull(domainFileMaps.stream().filter(domainFileMap -> domainFileMap.getDomain().equals("foobar_domain4")).findFirst().get());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,57 @@
</call>
</service>

<service id="translator.default" class="Symfony\Bundle\FrameworkBundle\Translation\Translator" public="false">
<tag name="kernel.locale_aware"/>
<argument type="service" id=".service_locator.V.o0zE7"/>
<argument type="service" id="translator.formatter.default"/>
<argument>en-GB</argument>
<argument type="collection">
<argument key="translation.loader.php" type="collection">
<argument>php</argument>
</argument>
<argument key="translation.loader.yml" type="collection">
<argument>yaml</argument>
<argument>yml</argument>
</argument>
<argument key="translation.loader.xliff" type="collection">
<argument>xlf</argument>
<argument>xliff</argument>
</argument>
</argument>
<argument type="collection">
<argument key="cache_dir">/app/var/cache/dev/translations</argument>
<argument key="debug">true</argument>
<argument key="resource_files" type="collection">
<argument key="af" type="collection">
<argument>/app/vendor/symfony/validator/Resources/translations/foobar_domain1.af.xlf</argument>
</argument>
<argument key="ar" type="collection">
<argument>/app/vendor/symfony/validator/Resources/translations/foobar_domain2.ar.xlf</argument>
<argument>/app/vendor/symfony/security-core/Resources/translations/foobar_domain3.ar.xlf</argument>
</argument>
<argument key="pt_PT" type="collection">
<argument>/home/daniel/projects/symfony5/vendor/symfony/security-core/Resources/translations/foobar_domain4.pt_PT.xlf</argument>
</argument>
</argument>
<argument key="scanned_directories" type="collection">
<argument>/app/vendor/symfony/validator/Resources/translations</argument>
</argument>
<argument key="cache_vary" type="collection">
<argument key="scanned_directories" type="collection">
<argument>vendor/symfony/validator/Resources/translations</argument>
</argument>
</argument>
</argument>
<call method="setConfigCacheFactory">
<argument type="service" id="config_cache_factory"/>
</call>
<call method="setFallbackLocales">
<argument type="collection">
<argument>en_GB</argument>
<argument>en</argument>
</argument>
</call>
</service>
</services>
</container>