Skip to content

Commit

Permalink
Pass I18NString parameters to LocalizedString
Browse files Browse the repository at this point in the history
StreetVertex used a lambda to pass String parameters to
LocalizedString(), which meant that toString() wasn't overridden.

By using I18NString parameters in LocalizedString the need for the
lambda is removed, while generalising the code.
  • Loading branch information
flaktack committed Mar 26, 2022
1 parent 6df316d commit 79ed523
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package org.opentripplanner.routing.vertextype;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.locationtech.jts.geom.Coordinate;
import org.opentripplanner.model.FlexStopLocation;
import org.opentripplanner.routing.core.TraverseMode;
import org.opentripplanner.routing.edgetype.StreetEdge;
import org.opentripplanner.routing.graph.Edge;
import org.opentripplanner.routing.graph.Graph;
import org.opentripplanner.routing.graph.Vertex;

import org.locationtech.jts.geom.Coordinate;
import org.opentripplanner.util.I18NString;
import org.opentripplanner.util.LocalizedString;

import java.util.*;

/**
* Abstract base class for vertices in the street layer of the graph.
* This includes both vertices representing intersections or points (IntersectionVertices)
Expand Down Expand Up @@ -44,7 +45,6 @@ public StreetVertex(Graph g, String label, double x, double y, I18NString street
* @return already localized street names and non-localized corner of x and unnamedStreet
*/
public I18NString getIntersectionName() {
I18NString calculatedName = null;
// generate names for corners when no name was given
Set<I18NString> uniqueNameSet = new HashSet<>();
for (Edge e : getOutgoing()) {
Expand All @@ -55,16 +55,12 @@ public I18NString getIntersectionName() {
List<I18NString> uniqueNames = new ArrayList<>(uniqueNameSet);

if (uniqueNames.size() > 1) {
calculatedName = locale -> new LocalizedString("corner", new String[]{
uniqueNames.get(0).toString(locale),
uniqueNames.get(1).toString(locale)
}).toString(locale);
return new LocalizedString("corner", uniqueNames.get(0), uniqueNames.get(1));
} else if (uniqueNames.size() == 1) {
calculatedName = uniqueNames.get(0);
return uniqueNames.get(0);
} else {
calculatedName = new LocalizedString("unnamedStreet");
return new LocalizedString("unnamedStreet");
}
return calculatedName;
}

public boolean isConnectedToWalkingEdge() {
Expand Down
56 changes: 27 additions & 29 deletions src/main/java/org/opentripplanner/util/LocalizedString.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class LocalizedString implements I18NString, Serializable {
//Key which specifies translation
private final String key;
//Values with which tagNames are replaced in translations.
private final String[] params;
private final I18NString[] params;

/**
* Creates String which can be localized
Expand All @@ -54,7 +54,7 @@ public LocalizedString(String key) {
* @param key key of translation for this way set in {@link org.opentripplanner.graph_builder.module.osm.DefaultWayPropertySetSource} and translations read from from properties Files
* @param params Values with which tagNames are replaced in translations.
*/
public LocalizedString(String key, String[] params) {
public LocalizedString(String key, I18NString ... params) {
this.key = key;
this.params = params;
}
Expand All @@ -74,20 +74,15 @@ public LocalizedString(String key, String[] params) {
*/
public LocalizedString(String key, OSMWithTags way) {
this.key = key;
List<String> lparams = new ArrayList<>(4);
List<I18NString> lparams = new ArrayList<>(4);
//Which tags do we want from way
List<String> tag_names = getTagNames();
if (tag_names != null) {
for(String tag_name: tag_names) {
String param = way.getTag(tag_name);
if (param != null) {
lparams.add(param);
} else {
lparams.add("");
}
}
for (String tag_name : tag_names) {
String param = way.getTag(tag_name);
lparams.add(new NonLocalizedString(Objects.requireNonNullElse(param, "")));
}
this.params = lparams.toArray(new String[lparams.size()]);

this.params = lparams.toArray(new I18NString[0]);
}

/**
Expand Down Expand Up @@ -151,20 +146,23 @@ public String toString() {
* with tag_names replaced with values
* @return
*/
@Override
public String toString(Locale locale) {
if (this.key == null) {
return null;
}
//replaces {name}, {ref} etc with %s to be used as parameters
//in string formatting with values from way tags values
String translation = ResourceBundleSingleton.INSTANCE.localize(this.key, locale);
if (this.params != null) {
translation = patternMatcher.matcher(translation).replaceAll("%s");
return String.format(translation, (Object[]) params);
} else {
return translation;
}
}

@Override
public String toString(Locale locale) {
if (this.key == null) {
return null;
}
//replaces {name}, {ref} etc with %s to be used as parameters
//in string formatting with values from way tags values
String translation = ResourceBundleSingleton.INSTANCE.localize(this.key, locale);
if (this.params != null) {
translation = patternMatcher.matcher(translation).replaceAll("%s");
return String.format(
translation,
Arrays.stream(params).map(i -> i.toString(locale)).toArray(Object[]::new)
);
}
else {
return translation;
}
}
}
14 changes: 14 additions & 0 deletions src/main/java/org/opentripplanner/util/TranslatedString.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@ public int hashCode() {
return Objects.hash(translations);
}

public static I18NString getI18NString(String untranslated, String... translations) {
if (translations.length % 2 != 0) {
throw new IllegalStateException("An even number of translations must be supplied.");
}

var map = new HashMap<String, String>();
map.put(null, untranslated);

for (int i = 0; i < translations.length - 1; i += 2) {
map.put(translations[i], translations[i + 1]);
}
return getI18NString(map);
}

/**
* Gets an interned I18NString.
* If the translations only have a single value, return a NonTranslatedString, otherwise a TranslatedString
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.List;
import java.util.Locale;
import java.util.Set;
import org.opentripplanner.util.NonLocalizedString;

public class TestOpenStreetMapGraphBuilder extends TestCase {

Expand Down Expand Up @@ -298,8 +299,11 @@ public void testCreativeNaming() {

@Test
public void testLocalizedString() {
LocalizedString localizedString = new LocalizedString("corner",
new String[]{"first", "second"});
LocalizedString localizedString = new LocalizedString(
"corner",
new NonLocalizedString("first"),
new NonLocalizedString("second")
);

assertEquals("Kreuzung first mit second",
localizedString.toString(new Locale("de")));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@ class LocalizedStringTest {
public void locale() {
assertEquals(
"corner of First and Second",
new LocalizedString("corner", new String[] {"First", "Second"}).toString()
new LocalizedString("corner", TranslatedString.getI18NString("First", "de", "erste"), TranslatedString.getI18NString("Second", "de", "zweite")).toString()
);
}

@Test
public void localeWithTranslation() {
assertEquals(
"Kreuzung First mit Second",
new LocalizedString("corner", new String[] {"First", "Second"}).toString(Locale.GERMANY)
"Kreuzung Erste mit Zweite",
new LocalizedString("corner", TranslatedString.getI18NString("First", "de", "Erste"), TranslatedString.getI18NString("Second", "de", "Zweite")).toString(Locale.GERMANY)
);
}

@Test
public void localeWithoutTranslation() {
assertEquals(
"corner of First and Second",
new LocalizedString("corner", new String[] {"First", "Second"}).toString(Locale.CHINESE)
new LocalizedString("corner", TranslatedString.getI18NString("First", "de", "erste"), TranslatedString.getI18NString("Second", "de", "zweite")).toString(Locale.CHINESE)
);
}

Expand Down

0 comments on commit 79ed523

Please sign in to comment.