From bf7f0424df983152017281c99db73fdaefaacecc Mon Sep 17 00:00:00 2001 From: Chumva Date: Sat, 17 Mar 2018 17:51:10 +0200 Subject: [PATCH] Remove search by matching poi key name and instead use english translation (preparation for synonyms support) --- .../src/net/osmand/osm/AbstractPoiType.java | 5 ++ .../src/net/osmand/osm/MapPoiTypes.java | 17 +++++++ .../osmand/search/core/SearchCoreFactory.java | 9 ++-- .../src/net/osmand/plus/AppInitializer.java | 46 +++++++++++++++++++ .../android/sample1/SampleApplication.java | 18 ++++++++ 5 files changed, 91 insertions(+), 4 deletions(-) diff --git a/OsmAnd-java/src/net/osmand/osm/AbstractPoiType.java b/OsmAnd-java/src/net/osmand/osm/AbstractPoiType.java index bb5e2ea215..77988f04ae 100644 --- a/OsmAnd-java/src/net/osmand/osm/AbstractPoiType.java +++ b/OsmAnd-java/src/net/osmand/osm/AbstractPoiType.java @@ -69,6 +69,11 @@ public abstract class AbstractPoiType { return registry.getTranslation(this); } + + public String getEnTranslation() { + return registry.getEnTranslation(this); + } + public String getPoiAdditionalCategoryTranslation() { if (poiAdditionalCategory != null) { return registry.getPoiTranslation(poiAdditionalCategory); diff --git a/OsmAnd-java/src/net/osmand/osm/MapPoiTypes.java b/OsmAnd-java/src/net/osmand/osm/MapPoiTypes.java index 83da14f21a..d82b6ae350 100644 --- a/OsmAnd-java/src/net/osmand/osm/MapPoiTypes.java +++ b/OsmAnd-java/src/net/osmand/osm/MapPoiTypes.java @@ -54,6 +54,9 @@ public class MapPoiTypes { String getTranslation(AbstractPoiType type); String getTranslation(String keyName); + String getEnTranslation(AbstractPoiType type); + + String getEnTranslation(String keyName); } public static MapPoiTypes getDefaultNoInit() { @@ -696,6 +699,16 @@ public class MapPoiTypes { } + public String getEnTranslation(AbstractPoiType abstractPoiType) { + if (poiTranslator != null) { + String translation = poiTranslator.getEnTranslation(abstractPoiType); + if (!Algorithms.isEmpty(translation)) { + return translation; + } + } + return getBasePoiName(abstractPoiType); + } + public String getTranslation(AbstractPoiType abstractPoiType) { if (poiTranslator != null) { String translation = poiTranslator.getTranslation(abstractPoiType); @@ -703,6 +716,10 @@ public class MapPoiTypes { return translation; } } + return getBasePoiName(abstractPoiType); + } + + private String getBasePoiName(AbstractPoiType abstractPoiType) { String name = abstractPoiType.getKeyName(); if(name.startsWith("osmand_")) { name = name.substring("osmand_".length()); diff --git a/OsmAnd-java/src/net/osmand/search/core/SearchCoreFactory.java b/OsmAnd-java/src/net/osmand/search/core/SearchCoreFactory.java index aeb04761a0..6d8d227193 100644 --- a/OsmAnd-java/src/net/osmand/search/core/SearchCoreFactory.java +++ b/OsmAnd-java/src/net/osmand/search/core/SearchCoreFactory.java @@ -616,13 +616,13 @@ public class SearchCoreFactory { List results = new ArrayList() ; NameStringMatcher nm = phrase.getNameStringMatcher(); for (PoiFilter pf : topVisibleFilters) { - if (!phrase.isUnknownSearchWordPresent() || nm.matches(pf.getTranslation())) { + if (!phrase.isUnknownSearchWordPresent() || nm.matches(pf.getTranslation()) || nm.matches(pf.getEnTranslation())) { results.add(pf); } } if (phrase.isUnknownSearchWordPresent()) { for (PoiCategory c : categories) { - if (!results.contains(c) && nm.matches(c.getTranslation())) { + if (!results.contains(c) && (nm.matches(c.getTranslation()) || nm.matches(c.getEnTranslation()) ) ) { results.add(c); } } @@ -631,13 +631,14 @@ public class SearchCoreFactory { Entry e = it.next(); PoiType pt = e.getValue(); if (pt.getCategory() != types.getOtherMapCategory()) { - if (!results.contains(pt) && (nm.matches(e.getKey()) || nm.matches(pt.getTranslation()))) { + if (!results.contains(pt) && ( nm.matches(pt.getEnTranslation()) || nm.matches(pt.getTranslation()) )) { results.add(pt); } List additionals = pt.getPoiAdditionals(); if (additionals != null) { for (PoiType a : additionals) { - if (!a.isReference() && !results.contains(a) && (nm.matches(a.getKeyName().replace('_', ' ')) || nm.matches(a.getTranslation()))) { + if (!a.isReference() && !results.contains(a) && + ( nm.matches(a.getEnTranslation()) || nm.matches(a.getTranslation()) )) { results.add(a); } } diff --git a/OsmAnd/src/net/osmand/plus/AppInitializer.java b/OsmAnd/src/net/osmand/plus/AppInitializer.java index 4a0b65cc15..3cfba9db6f 100644 --- a/OsmAnd/src/net/osmand/plus/AppInitializer.java +++ b/OsmAnd/src/net/osmand/plus/AppInitializer.java @@ -9,7 +9,11 @@ import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; +import android.content.res.Configuration; +import android.content.res.Resources; +import android.os.Build; import android.support.v7.app.AlertDialog; +import android.util.DisplayMetrics; import net.osmand.IProgress; import net.osmand.IndexConstants; @@ -58,6 +62,7 @@ import java.io.IOException; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.List; +import java.util.Locale; import java.util.Random; import btools.routingapp.BRouterServiceConnection; @@ -306,6 +311,17 @@ public class AppInitializer implements IProgress { } } + Resources getLocalizedResources(String loc) { + if(Build.VERSION.SDK_INT < 17) { + return null; + } + Locale desiredLocale = new Locale(loc); + Configuration conf = app.getResources().getConfiguration(); + conf = new Configuration(conf); + conf.setLocale(desiredLocale); + Context localizedContext = app.createConfigurationContext(conf); + return localizedContext.getResources(); + } private void initPoiTypes() { if(app.getAppPath("poi_types.xml").exists()) { @@ -313,6 +329,9 @@ public class AppInitializer implements IProgress { } else { app.poiTypes.init(); } + + final Resources en = getLocalizedResources("en"); + app.poiTypes.setPoiTranslator(new MapPoiTypes.PoiTranslator() { @@ -337,6 +356,33 @@ public class AppInitializer implements IProgress { } return null; } + + @Override + public String getEnTranslation(AbstractPoiType type) { + if(type.getBaseLangType() != null) { + return getEnTranslation(type.getBaseLangType()) + " (" + app.getLangTranslation(type.getLang()).toLowerCase() +")"; + } + return getEnTranslation(type.getIconKeyName()); + } + + + @Override + public String getEnTranslation(String keyName) { + if(en == null) { + return Algorithms.capitalizeFirstLetter( + keyName.replace('_', ' ')); + } + try { + Field f = R.string.class.getField("poi_" + keyName); + if (f != null) { + Integer in = (Integer) f.get(null); + return en.getString(in); + } + } catch (Exception e) { + LOG.debug("No translation for "+ keyName + " " + e.getMessage()); + } + return null; + } }); } diff --git a/OsmAndCore-sample/src/net/osmand/core/samples/android/sample1/SampleApplication.java b/OsmAndCore-sample/src/net/osmand/core/samples/android/sample1/SampleApplication.java index 2b6a51d859..368f6dac5f 100644 --- a/OsmAndCore-sample/src/net/osmand/core/samples/android/sample1/SampleApplication.java +++ b/OsmAndCore-sample/src/net/osmand/core/samples/android/sample1/SampleApplication.java @@ -212,6 +212,24 @@ public class SampleApplication extends Application { public String getTranslation(String keyName) { return null; } + + + @Override + public String getEnTranslation(AbstractPoiType type) { + if(type.getBaseLangType() != null) { + return getEnTranslation(type.getBaseLangType()) + " (" + getLangTranslation(type.getLang()).toLowerCase() +")"; + } + return getEnTranslation(type.getIconKeyName()); + } + + + @Override + public String getEnTranslation(String keyName) { + return Algorithms.capitalizeFirstLetter( + keyName.replace('_', ' ')); + + } + }); searchUICore.initSearchUICore();