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

Commit

Permalink
useSourceOrder only when requested (otherwise assumes rhbz#712281 fix…
Browse files Browse the repository at this point in the history
…ed on server)
  • Loading branch information
seanf committed Jun 15, 2011
1 parent c451672 commit 263b895
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 54 deletions.
Expand Up @@ -45,18 +45,24 @@ public PoReader2()
{
}

public TranslationsResource extractTarget(InputSource inputSource, Resource srcDoc)
public TranslationsResource extractTarget(InputSource inputSource, Resource srcDoc, boolean useSourceOrder)
{
TranslationsResource document = new TranslationsResource();
MessageStreamParser messageParser = createParser(inputSource);

List<TextFlow> resources = srcDoc.getTextFlows();
List<String> textFlowIds = new ArrayList<String>();
for (TextFlow res : resources)

List<String> textFlowIds = null;
Map<String, TextFlowTarget> targets = null;
if (useSourceOrder)
{
textFlowIds.add(res.getId());
textFlowIds = new ArrayList<String>();
for (TextFlow res : resources)
{
textFlowIds.add(res.getId());
}
targets = new HashMap<String, TextFlowTarget>();
}
Map<String, TextFlowTarget> targets = new HashMap<String, TextFlowTarget>();

boolean headerFound = false;
while (messageParser.hasNext())
Expand Down Expand Up @@ -85,7 +91,7 @@ else if (message.isPlural())
else
{
String id = createId(message);
if (!textFlowIds.contains(id))
if (useSourceOrder && !textFlowIds.contains(id))
{
// TODO append obsolete
}
Expand All @@ -101,18 +107,23 @@ else if (message.isPlural())

// add the PO comment
tfTarget.getExtensions(true).add(new SimpleComment(StringUtils.join(message.getComments(), "\n")));
targets.put(id, tfTarget);
if (useSourceOrder)
targets.put(id, tfTarget);
else
document.getTextFlowTargets().add(tfTarget);
}
}
}
// this ensures that the TextFlowTargets have the same order as the
// TextFlows in the Document:
for (String id : textFlowIds)
if (useSourceOrder)
{
TextFlowTarget tfTarget = targets.get(id);
document.getTextFlowTargets().add(tfTarget);
// this ensures that the TextFlowTargets have the same order as the
// TextFlows in the Document:
for (String id : textFlowIds)
{
TextFlowTarget tfTarget = targets.get(id);
document.getTextFlowTargets().add(tfTarget);
}
}

return document;
}

Expand Down
Expand Up @@ -50,7 +50,7 @@ public void extractTemplateThenAdd2Targets() throws IOException, JAXBException
inputSource = new InputSource(new File(testDir, locale + "/RPM.po").toURI().toString());
inputSource.setEncoding("utf8");
System.out.println("extracting target: " + locale);
TranslationsResource targetDoc = poReader.extractTarget(inputSource, doc);
TranslationsResource targetDoc = poReader.extractTarget(inputSource, doc, false);
List<TextFlowTarget> textFlowTargets = targetDoc.getTextFlowTargets();
assertThat(textFlowTargets.size(), is(137));
TextFlowTarget target = textFlowTargets.iterator().next();
Expand Down
@@ -1,9 +1,5 @@
package org.zanata.adapter.xliff;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
Expand Down
Expand Up @@ -232,7 +232,8 @@ else if (opts.isInteractiveMode())
{
InputSource inputSource = new InputSource(bis2);
inputSource.setEncoding("utf8");
targetDoc = poReader.extractTarget(inputSource, srcDoc);
// NB we always use source order in this impl
targetDoc = poReader.extractTarget(inputSource, srcDoc, true);
}
finally
{
Expand Down
Expand Up @@ -24,7 +24,6 @@
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -127,7 +126,7 @@ private List<LocaleMapping> findLocales()
}

@Override
public void visitTranslationResources(String docUri, String docName, Resource srcDoc, TranslationResourcesVisitor callback) throws IOException
public void visitTranslationResources(String docName, Resource srcDoc, TranslationResourcesVisitor callback) throws IOException
{
for (LocaleMapping locale : findLocales())
{
Expand All @@ -140,7 +139,7 @@ public void visitTranslationResources(String docUri, String docName, Resource sr
{
InputSource inputSource = new InputSource(bis);
inputSource.setEncoding("utf8");
TranslationsResource targetDoc = poReader.extractTarget(inputSource, srcDoc);
TranslationsResource targetDoc = poReader.extractTarget(inputSource, srcDoc, opts.getUseSourceOrder());
callback.visit(locale, targetDoc);
}
finally
Expand Down
Expand Up @@ -106,20 +106,26 @@ private Resource loadResource(String docName, File propFile) throws IOException
Properties props = loadPropFile(propFile);
for (String key : props.keySet())
{
String content = props.getProperty(key);
LocaleId sourceLoc = new LocaleId(opts.getSourceLang());
TextFlow textflow = new TextFlow(key, sourceLoc, content);
String comment = props.getComment(key);
if (comment != null)
{
SimpleComment simpleComment = new SimpleComment(comment);
textflow.getExtensions(true).add(simpleComment);
}
TextFlow textflow = propEntryToTextFlow(props, key);
doc.getTextFlows().add(textflow);
}
return doc;
}

private TextFlow propEntryToTextFlow(Properties props, String key)
{
String content = props.getProperty(key);
LocaleId sourceLoc = new LocaleId(opts.getSourceLang());
TextFlow textflow = new TextFlow(key, sourceLoc, content);
String comment = props.getComment(key);
if (comment != null)
{
SimpleComment simpleComment = new SimpleComment(comment);
textflow.getExtensions(true).add(simpleComment);
}
return textflow;
}

@Override
public Resource loadSrcDoc(File sourceDir, String docName) throws IOException
{
Expand All @@ -128,31 +134,46 @@ public Resource loadSrcDoc(File sourceDir, String docName) throws IOException
return loadResource(docName, propFile);
}

private TranslationsResource loadTranslationsResource(Resource srcDoc, File transFile) throws IOException
private TranslationsResource loadTranslationsResource(Resource srcDoc, File transFile, boolean useSourceOrder) throws IOException
{
// TODO consider using PropReader
TranslationsResource targetDoc = new TranslationsResource();
Properties props = loadPropFile(transFile);
for (TextFlow tf : srcDoc.getTextFlows())
if (opts.getUseSourceOrder())
{
for (TextFlow tf : srcDoc.getTextFlows())
{
String key = tf.getId();
addPropEntryToDoc(targetDoc, props, key);
}
}
else
{
String key = tf.getId();
String content = props.getProperty(key);
if (content == null)
continue;
TextFlowTarget textFlowTarget = new TextFlowTarget(key);
textFlowTarget.setContent(content);
textFlowTarget.setState(ContentState.Approved);
String comment = props.getComment(key);
if (comment != null)
for (String key : props.keySet())
{
SimpleComment simpleComment = new SimpleComment(comment);
textFlowTarget.getExtensions(true).add(simpleComment);
addPropEntryToDoc(targetDoc, props, key);
}
targetDoc.getTextFlowTargets().add(textFlowTarget);
}
return targetDoc;
}

private void addPropEntryToDoc(TranslationsResource targetDoc, Properties props, String key)
{
String content = props.getProperty(key);
if (content == null)
return;
TextFlowTarget textFlowTarget = new TextFlowTarget(key);
textFlowTarget.setContent(content);
textFlowTarget.setState(ContentState.Approved);
String comment = props.getComment(key);
if (comment != null)
{
SimpleComment simpleComment = new SimpleComment(comment);
textFlowTarget.getExtensions(true).add(simpleComment);
}
targetDoc.getTextFlowTargets().add(textFlowTarget);
}

private String removeDotProperties(String propFileName)
{
return propFileName.substring(0, propFileName.length() - ".properties".length());
Expand All @@ -165,15 +186,15 @@ public void setPushOptions(PushOptions opts)
}

@Override
public void visitTranslationResources(String docUri, String docName, Resource srcDoc, TranslationResourcesVisitor callback) throws IOException
public void visitTranslationResources(String docName, Resource srcDoc, TranslationResourcesVisitor callback) throws IOException
{
for (LocaleMapping locale : opts.getLocales())
{
String filename = docNameToFilename(docName, locale);
File transFile = new File(opts.getTransDir(), filename);
if (transFile.exists())
{
TranslationsResource targetDoc = loadTranslationsResource(srcDoc, transFile);
TranslationsResource targetDoc = loadTranslationsResource(srcDoc, transFile, opts.getUseSourceOrder());
callback.visit(locale, targetDoc);
}
else
Expand Down
Expand Up @@ -169,7 +169,7 @@ public void run() throws Exception

if (opts.getPushTrans())
{
strat.visitTranslationResources(docUri, docName, srcDoc, new TranslationResourcesVisitor()
strat.visitTranslationResources(docName, srcDoc, new TranslationResourcesVisitor()
{
@Override
public void visit(LocaleMapping locale, TranslationsResource targetDoc)
Expand Down
Expand Up @@ -12,6 +12,7 @@ public interface PushOptions extends ConfigurableProjectOptions
public String getProjectType();
public boolean getPushTrans();
public boolean getCopyTrans();
public boolean getUseSourceOrder();
public String getMergeType();
public String getSourcePattern();
}
Expand Down
Expand Up @@ -35,7 +35,5 @@ interface PushStrategy
StringSet getExtensions();
Set<String> findDocNames(File srcDir) throws IOException;
Resource loadSrcDoc(File sourceDir, String docName) throws IOException;

// TODO remove docUri param
void visitTranslationResources(@Deprecated String docUri, String docName, Resource srcDoc, TranslationResourcesVisitor visitor) throws IOException;
void visitTranslationResources(String docName, Resource srcDoc, TranslationResourcesVisitor visitor) throws IOException;
}
Expand Up @@ -4,14 +4,12 @@
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.AndFileFilter;
import org.apache.commons.io.filefilter.NotFileFilter;
import org.apache.commons.io.filefilter.TrueFileFilter;
import org.apache.commons.io.filefilter.WildcardFileFilter;
import org.apache.commons.lang.StringUtils;
Expand Down Expand Up @@ -98,7 +96,7 @@ public Resource loadSrcDoc(File sourceDir, String docName) throws IOException
}

@Override
public void visitTranslationResources(String docUri, String docName, Resource srcDoc, TranslationResourcesVisitor visitor) throws FileNotFoundException
public void visitTranslationResources(String docName, Resource srcDoc, TranslationResourcesVisitor visitor) throws FileNotFoundException
{
for (LocaleMapping locale : opts.getLocales())
{
Expand All @@ -107,6 +105,7 @@ public void visitTranslationResources(String docUri, String docName, Resource sr
{
InputSource inputSource = new InputSource(new FileInputStream(transFile));
inputSource.setEncoding("utf8");
// TODO opts.getUseSourceOrder()
TranslationsResource targetDoc = reader.extractTarget(inputSource);
visitor.visit(locale, targetDoc);
}
Expand Down
Expand Up @@ -72,6 +72,14 @@ public PushCommand initCommand()
*/
private boolean copyTrans;

/**
* Should we ensure that translations appear in the same order as the source
* strings? This is only needed for compatibility with Zanata server < 1.4.
*
* @parameter expression="${zanata.useSourceOrder}" default-value="false"
*/
private boolean useSourceOrder;

/**
* Merge type: "auto" (default) or "import" (DANGER!).
*
Expand Down Expand Up @@ -134,4 +142,10 @@ public String getSourcePattern()
return sourcePattern;
}

@Override
public boolean getUseSourceOrder()
{
return useSourceOrder;
}

}

0 comments on commit 263b895

Please sign in to comment.