Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
rhbz986785 Ensure that each <tu> has a srclang
Browse files Browse the repository at this point in the history
  • Loading branch information
seanf committed Jul 22, 2013
1 parent 0d3615a commit 9414b3b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
Expand Up @@ -24,6 +24,7 @@
import lombok.extern.slf4j.Slf4j;
import net.sf.okapi.common.filterwriter.TMXWriter;
import net.sf.okapi.common.resource.ITextUnit;
import net.sf.okapi.common.resource.Property;
import net.sf.okapi.common.resource.TextFragment;
import net.sf.okapi.common.resource.TextUnit;

Expand Down Expand Up @@ -59,6 +60,7 @@ public ExportTUStrategy(LocaleId localeId)
*/
public void exportTranslationUnit(TMXWriter tmxWriter, SourceContents tf, net.sf.okapi.common.LocaleId sourceLocaleId)
{
assert tmxWriter.isWriteAllPropertiesAsAttributes();
String tuid = tf.getQualifiedId();
// Perhaps we could encode plurals using TMX attributes?
String srcContent = tf.getContents().get(0);
Expand All @@ -69,6 +71,7 @@ public void exportTranslationUnit(TMXWriter tmxWriter, SourceContents tf, net.sf
}

ITextUnit textUnit = new TextUnit(tuid, srcContent);
setSrcLang(textUnit, sourceLocaleId);
textUnit.setName(tuid);
if (localeId != null)
{
Expand All @@ -93,6 +96,11 @@ public void exportTranslationUnit(TMXWriter tmxWriter, SourceContents tf, net.sf
}
}

private void setSrcLang(ITextUnit textUnit, net.sf.okapi.common.LocaleId sourceLocaleId)
{
textUnit.setProperty(new Property("srclang", sourceLocaleId.toBCP47()));
}

private void addTargetToTextUnit(ITextUnit textUnit, TargetContents tfTarget)
{
if (tfTarget != null && tfTarget.getState().isTranslated())
Expand Down
Expand Up @@ -84,6 +84,7 @@ public void write(OutputStream output) throws IOException, WebApplicationExcepti
XMLWriter xmlWriter = new XMLWriter(writer);
@Cleanup
TMXWriter tmxWriter = new TMXWriter(xmlWriter);
tmxWriter.setWriteAllPropertiesAsAttributes(true);
String segType = "block"; // TODO other segmentation types
String dataType = "unknown"; // TODO track data type metadata throughout the system

Expand Down
Expand Up @@ -25,6 +25,7 @@
import org.custommonkey.xmlunit.exceptions.XpathException;
import org.testng.annotations.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.zanata.common.LocaleId;
Expand Down Expand Up @@ -164,13 +165,15 @@ private void assertContainsGermanTUs(Document doc) throws XpathException, SAXExc
}

@SuppressWarnings("deprecation") // Eclipse seems to confuse org.junit with junit.framework
private static void assertSingleTU(String docId, String resId, Document doc) throws XpathException, SAXException, IOException
private void assertSingleTU(String docId, String resId, Document doc) throws XpathException, SAXException, IOException
{
String xpath = "//tu[@tuid='"+docId+":"+resId+"']";
XpathEngine simpleXpathEngine = XMLUnit.newXpathEngine();
NodeList nodeList = simpleXpathEngine.getMatchingNodes(xpath, doc);
int matches = nodeList.getLength();
assertEquals("Should be only one tu node per docId:resId", 1, matches);
Node srclang = nodeList.item(0).getAttributes().getNamedItem("srclang");
assertEquals(sourceLocale.getId(), srclang.getNodeValue());
}

private static void assertTUContainsSegment(String segmentText, String docId, String resId, String lang, Document doc) throws XpathException, SAXException, IOException
Expand Down

0 comments on commit 9414b3b

Please sign in to comment.