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

Commit

Permalink
Upload translated ts file to update textflow targets
Browse files Browse the repository at this point in the history
  • Loading branch information
djansen-redhat committed Apr 21, 2016
1 parent cb4d964 commit cad00a2
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -296,10 +296,13 @@ public TranslationsResource parseTranslationFile(URI fileUri,
RawDocument rawDoc =
new RawDocument(fileUri, "UTF-8",
net.sf.okapi.common.LocaleId.fromString("en"));
if (rawDoc.getTargetLocale() == null) {
rawDoc.setTargetLocale(net.sf.okapi.common.LocaleId.fromString(localeId));
}
return parseTranslationFile(rawDoc, filterParams);
}

private TranslationsResource parseTranslationFile(RawDocument rawDoc,
protected TranslationsResource parseTranslationFile(RawDocument rawDoc,
Optional<String> params) {
TranslationsResource transRes = new TranslationsResource();
List<TextFlowTarget> translations = transRes.getTextFlowTargets();
Expand Down
60 changes: 60 additions & 0 deletions zanata-war/src/main/java/org/zanata/adapter/TSAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,20 @@
import net.sf.okapi.filters.ts.TsFilter;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.StringUtils;
import org.zanata.common.ContentState;
import org.zanata.common.DocumentType;
import org.zanata.common.HasContents;
import org.zanata.common.LocaleId;
import org.zanata.exception.FileFormatAdapterException;
import org.zanata.model.HDocument;
import org.zanata.rest.dto.resource.TextFlow;
import org.zanata.rest.dto.resource.TextFlowTarget;
import org.zanata.rest.dto.resource.TranslationsResource;

import javax.annotation.Nonnull;

import java.net.URI;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
Expand Down Expand Up @@ -148,6 +152,10 @@ protected String getTranslatableText(TextUnit tu) {
return tu.getSource().getFirstContent().getText();
}

private String getTranslatedText(TextContainer tc) {
return tc.getFirstContent().getText();
}

@Override
protected TextFlow processTextFlow(TextUnit tu, String content, String subDocName, LocaleId sourceLocale) {
TextFlow tf = new TextFlow(getIdFor(tu, content,
Expand All @@ -171,4 +179,56 @@ private String stripPath(String name) {
return name;
}
}

@Override
protected TranslationsResource parseTranslationFile(RawDocument rawDoc,
Optional<String> params) {
TranslationsResource transRes = new TranslationsResource();
List<TextFlowTarget> translations = transRes.getTextFlowTargets();

Map<String, HasContents> addedResources =
new HashMap<String, HasContents>();
IFilter filter = getFilter();

try {
filter.open(rawDoc);
String subDocName = "";
while (filter.hasNext()) {
Event event = filter.next();
if (event.getEventType() == EventType.START_SUBDOCUMENT) {
StartSubDocument startSubDoc =
(StartSubDocument) event.getResource();
subDocName = stripPath(startSubDoc.getName());
} else if (event.getEventType() == EventType.TEXT_UNIT) {
TextUnit tu = (TextUnit) event.getResource();
if (!tu.getSource().isEmpty() && tu.isTranslatable()) {
String content = getTranslatableText(tu);
TextContainer translation = tu.getTarget(rawDoc.getTargetLocale());
if (!content.isEmpty()) {
TextFlowTarget tft =
new TextFlowTarget(getIdFor(tu, content, subDocName));
// TODO: Change this
tft.setState(ContentState.NeedReview);
String resId = tft.getResId();
if (addedResources.containsKey(resId)) {
List<String> currentStrings = new ArrayList<>(addedResources.get(resId).getContents());
currentStrings.add(getTranslatedText(translation));
tft.setContents(currentStrings);
} else {
tft.setContents(getTranslatedText(translation));
}
addedResources.put(tft.getResId(), tft);
translations.add(tft);
}
}
}
}
} catch (OkapiIOException e) {
throw new FileFormatAdapterException(
"Unable to parse translation file", e);
} finally {
filter.close();
}
return transRes;
}
}

0 comments on commit cad00a2

Please sign in to comment.