Skip to content

Commit

Permalink
TextUnit <-> AndroidString mappers
Browse files Browse the repository at this point in the history
  • Loading branch information
comunacho authored and aurambaj committed Aug 5, 2020
1 parent 8bde641 commit db1863f
Show file tree
Hide file tree
Showing 10 changed files with 645 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class AndroidPlural extends AbstractAndroidString {

Expand All @@ -26,9 +27,13 @@ public Map<AndroidPluralQuantity, AndroidPluralItem> getItems() {
}

public void forEachItemSorted(Consumer<AndroidPluralItem> itemConsumer){
items.values().stream()
.sorted(Comparator.comparingInt(item -> item.getQuantity().ordinal()))
.forEach(itemConsumer);
sortedStream().forEach(itemConsumer);
}

public Stream<AndroidPluralItem> sortedStream(){
return items.values()
.stream()
.sorted(Comparator.comparingInt(item -> item.getQuantity().ordinal()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void addSingular(AndroidStringElement element, Node comment) {
addSingular(new AndroidSingular(element.getIdAttribute(),
element.getNameAttribute(),
element.getUnescapedContent(),
comment.getTextContent()));
comment != null ? comment.getTextContent() : null));
}

public void addPlural(AndroidPlural plural) {
Expand All @@ -51,7 +51,9 @@ public void addPlural(AndroidStringElement element, Node comment) {

AndroidPlural.AndroidPluralBuilder builder = AndroidPlural.builder();
builder.setName(element.getNameAttribute());
builder.setComment(comment.getTextContent());
if (comment != null) {
builder.setComment(comment.getTextContent());
}

element.forEachPluralItem(builder::addItem);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
package com.box.l10n.mojito.android.strings;

import com.box.l10n.mojito.service.tm.PluralNameParser;
import com.box.l10n.mojito.service.tm.search.TextUnitDTO;
import com.google.common.base.Strings;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.box.l10n.mojito.android.strings.AndroidPluralQuantity.OTHER;

public class AndroidStringDocumentMapper {

private static final String DEFAULT_ASSET_DELIMITER = "#@#";

private final String pluralSeparator;
private final String assetDelimiter;
private final String locale;
private final String repositoryName;
private final PluralNameParser pluralNameParser;

public AndroidStringDocumentMapper(String pluralSeparator,
String assetDelimiter,
String locale,
String repositoryName) {
this.pluralSeparator = pluralSeparator;
this.assetDelimiter = Optional.ofNullable(Strings.emptyToNull(assetDelimiter)).orElse(DEFAULT_ASSET_DELIMITER);
this.locale = locale;
this.repositoryName = repositoryName;
this.pluralNameParser = new PluralNameParser();
}

public AndroidStringDocumentMapper(String pluralSeparator, String assetDelimiter) {
this(pluralSeparator, assetDelimiter, null, null);
}

public AndroidStringDocument readFromTextUnits(List<TextUnitDTO> textUnits, boolean useSource) {

AndroidStringDocument document = new AndroidStringDocument();
Map<String, AndroidPlural.AndroidPluralBuilder> pluralByOther = new HashMap<>();

for (TextUnitDTO textUnit : textUnits) {

if (isSingularTextUnit(textUnit)) {
document.addSingular(textUnitToAndroidSingular(textUnit, useSource));
} else {
pluralByOther.compute(textUnit.getPluralFormOther(), (key, builder) -> {
if (builder == null) {
builder = AndroidPlural.builder();
}

if (OTHER.name().equalsIgnoreCase(textUnit.getPluralForm())) {
String name = pluralNameParser.getPrefix(textUnit.getName(), pluralSeparator);
builder.setName(textUnit.getAssetPath() + assetDelimiter + name);
builder.setComment(textUnit.getComment());
}

builder.addItem(new AndroidPluralItem(
textUnit.getPluralForm(),
textUnit.getTmTextUnitId(),
useSource ? textUnit.getSource() : textUnit.getTarget()));

return builder;

});
}
}

pluralByOther.forEach((pluralFormOther, builder) -> document.addPlural(builder.build()));

return document;
}

public AndroidStringDocument readFromSourceTextUnits(List<TextUnitDTO> textUnits) {
return readFromTextUnits(textUnits, true);
}

public AndroidStringDocument readFromTargetTextUnits(List<TextUnitDTO> textUnits) {
return readFromTextUnits(textUnits, false);
}

public List<TextUnitDTO> mapToTextUnits(AndroidStringDocument document) {
return mapToTextUnitStream(document).collect(Collectors.toList());
}

public Stream<TextUnitDTO> mapToTextUnitStream(AndroidStringDocument document) {
return document.getStrings()
.stream()
.flatMap(this::stringToTextUnits);
}

TextUnitDTO addTextUnitDTOAttributes(TextUnitDTO textUnit) {

if (!Strings.isNullOrEmpty(locale)){
textUnit.setTargetLocale(locale);
}

if (!Strings.isNullOrEmpty(repositoryName)){
textUnit.setRepositoryName(repositoryName);
}

if (textUnit.getName().contains(assetDelimiter)){
String[] nameParts = textUnit.getName().split(assetDelimiter);

if (nameParts.length == 2) {
textUnit.setAssetPath(nameParts[0]);
textUnit.setName(nameParts[1]);
}
}

return textUnit;
}

Stream<TextUnitDTO> stringToTextUnits(AbstractAndroidString androidString) {
Stream<TextUnitDTO> result;

if (androidString.isSingular()){
result = singularToTextUnit((AndroidSingular) androidString);
} else {
result = pluralToTextUnits((AndroidPlural) androidString);
}

return result;
}

Stream<TextUnitDTO> singularToTextUnit(AndroidSingular singular) {
TextUnitDTO textUnit = new TextUnitDTO();

textUnit.setName(singular.getName());
textUnit.setComment(singular.getComment());
textUnit.setTmTextUnitId(singular.getId());
textUnit.setTarget(unescape(singular.getContent()));
addTextUnitDTOAttributes(textUnit);

return Stream.of(textUnit);
}

Stream<TextUnitDTO> pluralToTextUnits(AndroidPlural plural) {
return plural.sortedStream()
.map(item -> {

TextUnitDTO textUnit = new TextUnitDTO();
String quantity = item.getQuantity().toString();
String name = pluralNameParser.toPluralName(plural.getName(), quantity, pluralSeparator);
String pluralFormOther = pluralNameParser.toPluralName(plural.getName(), OTHER.toString(), pluralSeparator);

textUnit.setName(name);
textUnit.setComment(plural.getComment());
textUnit.setTmTextUnitId(item.getId());
textUnit.setPluralForm(quantity);
textUnit.setPluralFormOther(pluralFormOther);
textUnit.setTarget(unescape(item.getContent()));
addTextUnitDTOAttributes(textUnit);

return textUnit;

});
}

boolean isSingularTextUnit(TextUnitDTO textUnit) {
return Strings.isNullOrEmpty(textUnit.getPluralForm());
}

AndroidSingular textUnitToAndroidSingular(TextUnitDTO textUnit, boolean useSource) {
return new AndroidSingular(
textUnit.getTmTextUnitId(),
textUnit.getAssetPath() + assetDelimiter + textUnit.getName(),
removeBadCharacters(useSource ? textUnit.getSource() : textUnit.getTarget()),
textUnit.getComment());
}

static String removeBadCharacters(String source) {
return Strings.nullToEmpty(source)
.replaceAll("\u001d", "")
.replaceAll("\u001c", "")
.replaceAll("\u0000", "");
}

static String unescape(String str) {
return Strings.nullToEmpty(str)
.replaceAll("\\\\'", "'")
.replaceAll("\\\\\"", "\"")
.replaceAll("\\\\@", "@")
.replaceAll("\\\\n", "\n");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ private void setAttribute(Element element, String name, String value) {
}
}

private static String escapeQuotes(String str) {
static String escapeQuotes(String str) {
return Strings.nullToEmpty(str)
.replaceAll("\"", "\\\\\"")
.replaceAll("\n", "\\\\\n");
.replaceAll("\n", "\\\\n");
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,28 @@

import org.springframework.stereotype.Component;

import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Component
public class PluralNameParser {

public String getPrefix(String nameWithPluralForm) {
return nameWithPluralForm.replaceAll("_(zero|one|two|few|many|other)$", "");
}

public String getPrefix(String name, String separator) {

Pattern pattern = Pattern.compile("(.*)" + separator + "(zero|one|two|few|many|other)");

return Optional.of(pattern.matcher(name))
.filter(Matcher::matches)
.map(m -> m.group(1))
.orElse(name);
}

public String toPluralName(String prefix, String name, String separator){
return String.format("%s%s%s", prefix, separator, name);
}
}
Loading

0 comments on commit db1863f

Please sign in to comment.