Remove search by matching poi key name and instead use english translation (preparation for synonyms support)

This commit is contained in:
Chumva 2018-03-17 17:51:10 +02:00
parent 962af1ccc1
commit bf7f0424df
5 changed files with 91 additions and 4 deletions

View file

@ -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);

View file

@ -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());

View file

@ -616,13 +616,13 @@ public class SearchCoreFactory {
List<AbstractPoiType> results = new ArrayList<AbstractPoiType>() ;
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<String, PoiType> 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<PoiType> 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);
}
}

View file

@ -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;
}
});
}

View file

@ -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();