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

Commit

Permalink
Merge branch 'plurals' into rhbz803572
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Huang committed Mar 23, 2012
2 parents ea4174d + c1453bb commit 71ddfbd
Show file tree
Hide file tree
Showing 45 changed files with 1,545 additions and 261 deletions.
Expand Up @@ -67,7 +67,7 @@ private void addPropEntryToDoc(TranslationsResource doc, Properties props, Strin
if (content == null)
return;
TextFlowTarget textFlowTarget = new TextFlowTarget(key);
textFlowTarget.setContent(content);
textFlowTarget.setContents(content);
if (!content.isEmpty())
{
textFlowTarget.setState(contentState);
Expand Down
Expand Up @@ -9,6 +9,7 @@
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.fedorahosted.openprops.Properties;
Expand Down Expand Up @@ -61,7 +62,12 @@ private static void write(final Resource doc, final File baseDir, boolean utf8)
Properties props = new Properties();
for (TextFlow textFlow : doc.getTextFlows())
{
props.setProperty(textFlow.getId(), textFlow.getContent());
List<String> contents = textFlow.getContents();
if (contents.size() != 1)
{
throw new RuntimeException("file format does not support plural forms: resId=" + textFlow.getId());
}
props.setProperty(textFlow.getId(), textFlow.getContents().get(0));
SimpleComment simpleComment = textFlow.getExtensions(true).findByType(SimpleComment.class);
if (simpleComment != null && simpleComment.getValue() != null)
props.setComment(textFlow.getId(), simpleComment.getValue());
Expand Down Expand Up @@ -140,7 +146,7 @@ private static void storeProps(Properties props, File file, boolean utf8) throws

private static void textFlowTargetToProperty(String resId, TextFlowTarget target, Properties targetProp, boolean createSkeletons)
{
if (target == null || target.getState() != ContentState.Approved)
if (target == null || target.getState() != ContentState.Approved || target.getContents() == null || target.getContents().size() == 0)
{
// don't save fuzzy or empty values
if (createSkeletons)
Expand All @@ -149,7 +155,12 @@ private static void textFlowTargetToProperty(String resId, TextFlowTarget target
}
return;
}
targetProp.setProperty(target.getResId(), target.getContent());
List<String> contents = target.getContents();
if (contents.size() != 1)
{
throw new RuntimeException("file format does not support plural forms: resId=" + resId);
}
targetProp.setProperty(target.getResId(), contents.get(0));
SimpleComment simpleComment = target.getExtensions(true).findByType(SimpleComment.class);
if (simpleComment != null && simpleComment.getValue() != null)
{
Expand Down
@@ -1,5 +1,9 @@
package org.zanata.adapter.xliff;

import static java.util.Arrays.asList;

import java.util.List;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
Expand Down Expand Up @@ -76,7 +80,10 @@ else if (xmlr.isStartElement() && xmlr.getLocalName().equals(ELE_TRANS_UNIT))
{
TextFlowTarget tfTarget = new TextFlowTarget();
extractTransUnit(xmlr, tfTarget);
if(!StringUtils.isEmpty(tfTarget.getContent())){
List<String> contents = tfTarget.getContents();
boolean targetEmpty = contents.isEmpty() || StringUtils.isEmpty(contents.get(0));
if (!targetEmpty)
{
tfTarget.setState(ContentState.Approved);
transDoc.getTextFlowTargets().add(tfTarget);
}
Expand Down Expand Up @@ -111,7 +118,7 @@ private void extractTransUnit(XMLStreamReader xmlr, TextFlow textFlow) throws XM
{
if (xmlr.isStartElement() && xmlr.getLocalName().equals(ELE_SOURCE))
{
textFlow.setContent(getElementValue(xmlr));
textFlow.setContents(getElementValue(xmlr));
}
else if (xmlr.isStartElement() && xmlr.getLocalName().equals(ELE_CONTEXT_GROUP))
{
Expand All @@ -122,10 +129,10 @@ else if (xmlr.isStartElement() && xmlr.getLocalName().equals(ELE_CONTEXT_GROUP))
textFlow.setLang(srcLang);
}

private void extractTransUnit(XMLStreamReader xmlr, TextFlowTarget textFlow) throws XMLStreamException
private void extractTransUnit(XMLStreamReader xmlr, TextFlowTarget textFlowTarget) throws XMLStreamException
{
Boolean endTransUnit = false;
textFlow.setResId(getAttributeValue(xmlr, ATTRI_ID));
textFlowTarget.setResId(getAttributeValue(xmlr, ATTRI_ID));

while (xmlr.hasNext() && !endTransUnit)
{
Expand All @@ -136,11 +143,11 @@ private void extractTransUnit(XMLStreamReader xmlr, TextFlowTarget textFlow) thr
{
if (xmlr.isStartElement() && xmlr.getLocalName().equals(ELE_TARGET))
{
textFlow.setContent(getElementValue(xmlr));
textFlowTarget.setContents(asList(getElementValue(xmlr)));
}
else if (xmlr.isStartElement() && xmlr.getLocalName().equals(ELE_CONTEXT_GROUP))
{
textFlow.getExtensions(true).addAll(extractContextList(xmlr));
textFlowTarget.getExtensions(true).addAll(extractContextList(xmlr));
}
}
}
Expand Down
Expand Up @@ -6,6 +6,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.stream.XMLOutputFactory;
Expand Down Expand Up @@ -88,14 +89,24 @@ private static void writeTransUnits(IndentingXMLStreamWriter writer, Resource do
private static void writeTransUnitSource(IndentingXMLStreamWriter writer, TextFlow textFlow) throws XMLStreamException
{
writer.writeStartElement(ELE_SOURCE);
writer.writeCharacters(textFlow.getContent());
List<String> contents = textFlow.getContents();
if (contents.size() != 1)
{
throw new RuntimeException("file format does not support plural forms: resId=" + textFlow.getId());
}
writer.writeCharacters(contents.get(0));
writer.writeEndElement();// end source tag
}

private static void writeTransUnitTarget(IndentingXMLStreamWriter writer, TextFlowTarget target) throws XMLStreamException
{
writer.writeStartElement(ELE_TARGET);
writer.writeCharacters(target.getContent());
List<String> contents = target.getContents();
if (contents.size() != 1)
{
throw new RuntimeException("file format does not support plural forms: resId=" + target.getResId());
}
writer.writeCharacters(contents.get(0));
writer.writeEndElement();// end target tag
}

Expand Down
@@ -1,5 +1,6 @@
package org.zanata.adapter.xliff;

import static java.util.Arrays.asList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
Expand Down Expand Up @@ -47,8 +48,8 @@ public void templateFirstAndLastTextFlowTest() throws FileNotFoundException
TextFlow firstTextFlow = doc.getTextFlows().get(0);
TextFlow lastTextFlow = doc.getTextFlows().get(doc.getTextFlows().size() - 1);

assertThat(firstTextFlow.getContent(), equalTo("Translation Unit 1"));
assertThat(lastTextFlow.getContent(), equalTo("Translation Unit 3"));
assertThat(firstTextFlow.getContents(), equalTo(asList("Translation Unit 1")));
assertThat(lastTextFlow.getContents(), equalTo(asList("Translation Unit 3")));
}

@Test
Expand All @@ -70,8 +71,8 @@ public void targetFirstAndLastTextFlowTest() throws FileNotFoundException
TextFlowTarget firstTextFlow = tr.getTextFlowTargets().get(0);
TextFlowTarget lastTextFlow = tr.getTextFlowTargets().get(tr.getTextFlowTargets().size() - 1);

assertThat(firstTextFlow.getContent(), equalTo("Translation 1"));
assertThat(lastTextFlow.getContent(), equalTo("Translation 3"));
assertThat(firstTextFlow.getContents(), equalTo(asList("Translation 1")));
assertThat(lastTextFlow.getContents(), equalTo(asList("Translation 3")));
}


Expand Down
@@ -1,5 +1,6 @@
package org.zanata.adapter.xliff;

import static java.util.Arrays.asList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
Expand Down Expand Up @@ -36,8 +37,8 @@ public void checkTransUnit() throws FileNotFoundException
TextFlow firstTextFlow = doc.getTextFlows().get(0);
TextFlow lastTextFlow = doc.getTextFlows().get(doc.getTextFlows().size() - 1);

assertThat(firstTextFlow.getContent(), equalTo("Translation Unit 1"));
assertThat(lastTextFlow.getContent(), equalTo("Translation Unit 3"));
assertThat(firstTextFlow.getContents(), equalTo(asList("Translation Unit 1")));
assertThat(lastTextFlow.getContents(), equalTo(asList("Translation Unit 3")));
}

@Test
Expand Down
Expand Up @@ -70,7 +70,7 @@ private TextFlow newTextFlow(String key, String value)
{
TextFlow tf = new TextFlow();
tf.setId(key);
tf.setContent(value);
tf.setContents(value);
return tf;
}

Expand Down
@@ -1,5 +1,6 @@
package org.zanata.client.commands.push;

import static java.util.Arrays.asList;
import static org.testng.Assert.*;

import java.io.File;
Expand Down Expand Up @@ -82,9 +83,9 @@ public void latin1() throws Exception
private void checkResults(Resource doc)
{
assertEquals(doc.getTextFlows().get(0).getId(), "key");
assertEquals(doc.getTextFlows().get(0).getContent(), "value");
assertEquals(doc.getTextFlows().get(0).getContents(), asList("value"));
assertEquals(doc.getTextFlows().get(1).getId(), "unicode");
assertEquals(doc.getTextFlows().get(1).getContent(), "レス");
assertEquals(doc.getTextFlows().get(1).getContents(), asList("レス"));
}

}
Expand Up @@ -85,7 +85,15 @@ else if (message.isPlural())
tfTarget.setResId(id);
tfTarget.setDescription(ShortString.shorten(message.getMsgid()));

tfTarget.setContent(message.getMsgstr());
if (message.isPlural())
{
tfTarget.setContents(message.getMsgstrPlural());
}
else
{
tfTarget.setContents(message.getMsgstr());
}

tfTarget.setState(getContentState(message));

// add the PO comment
Expand Down Expand Up @@ -209,7 +217,14 @@ else if (message.isPlural())
String id = createId(message);
// add the content (msgid)
TextFlow tf = new TextFlow(id, sourceLocaleId);
tf.setContent(message.getMsgid());
if (message.isPlural())
{
tf.setContents(message.getMsgid(), message.getMsgidPlural());
}
else
{
tf.setContents(message.getMsgid());
}
resources.add(tf);

// add the entry header POT fields
Expand Down
Expand Up @@ -170,23 +170,55 @@ private void write(Writer writer, String charset, Resource document, Translation
PotEntryHeader entryData = textFlow.getExtensions(true).findByType(PotEntryHeader.class);
SimpleComment srcComment = textFlow.getExtensions().findByType(SimpleComment.class);
Message message = new Message();
message.setMsgid(textFlow.getContent());
List<String> tfContents = textFlow.getContents();
switch (tfContents.size())
{
case 2:
message.setMsgidPlural(tfContents.get(1));
// fall through...
case 1:
message.setMsgid(tfContents.get(0));
break;
default:
throw new RuntimeException("POT format only supports 2 plural forms: resId=" + textFlow.getId());
}

message.setMsgstr("");
if (targetDoc != null)
{
TextFlowTarget contentData = targets.get(textFlow.getId());
if (contentData != null)
TextFlowTarget tfTarget = targets.get(textFlow.getId());
if (tfTarget != null)
{
if (entryData == null)
{
log.warn("Missing POT entry for text-flow ID " + textFlow.getId());
}
else if (!contentData.getResId().equals(textFlow.getId()))
else if (!tfTarget.getResId().equals(textFlow.getId()))
{
throw new RuntimeException("ID from target doesn't match text-flow ID");
}
message.setMsgstr(contentData.getContent());
SimpleComment poComment = contentData.getExtensions().findByType(SimpleComment.class);
List<String> tftContents = tfTarget.getContents();

if (message.isPlural())
{
for (int i = 0; i < tftContents.size(); i++)
{
message.addMsgstrPlural(tftContents.get(i), i);
}
}
else
{
if (tftContents.size() != 0)
{
message.setMsgstr(tftContents.get(0));
if (tftContents.size() > 1)
{
throw new RuntimeException("plural forms not enabled for this text flow: resId=" + textFlow.getId());
}
}
}

SimpleComment poComment = tfTarget.getExtensions().findByType(SimpleComment.class);
if (poComment != null)
{
String[] comments = poComment.getValue().split("\n");
Expand All @@ -202,7 +234,7 @@ else if (!contentData.getResId().equals(textFlow.getId()))
}
}
}
if (contentData.getState() == ContentState.NeedReview)
if (tfTarget.getState() == ContentState.NeedReview)
{
message.setFuzzy(true);
}
Expand Down
@@ -1,5 +1,6 @@
package org.zanata.adapter.po;

import static java.util.Arrays.asList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
Expand Down Expand Up @@ -80,9 +81,9 @@ public void extractTarget() throws IOException, JAXBException
List<TextFlow> resources = doc.getTextFlows();

TextFlow tf1 = resources.get(3);
assertThat(tf1.getContent(), equalTo("Important"));
assertThat(tf1.getContents(), equalTo(asList("Important")));
TextFlowTarget tfTarget = textFlowTargets.get(3);
assertThat(tfTarget.getContent(), equalTo("キーのインポート"));
assertThat(tfTarget.getContents(), equalTo(asList("キーのインポート")));

// TODO test PO headers and attributes
}
Expand Down

0 comments on commit 71ddfbd

Please sign in to comment.