Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,22 @@ public void onStyleLoaded(@NonNull Style style) {
mapView.addOnWillStartLoadingMapListener(styleLoadListener);
}

private MapLocale getChineseMapLocale(MapLocale mapLocale, boolean isStreetsV7) {
if (isStreetsV7) {
// streets v7 supports name_zh(MapLocale.CHINA) and name_zh-Hans(MapLocale.CHINESE_HANS)
// https://docs.mapbox.com/vector-tiles/reference/mapbox-streets-v7/#name-fields
if (mapLocale.equals(MapLocale.CHINESE_HANS)) {
return mapLocale;
} else {
return MapLocale.CHINA;
}
} else {
// streets V6 only supports name_zh(MapLocale.CHINA)
// https://docs.mapbox.com/vector-tiles/reference/mapbox-streets-v6/#name-fields
return MapLocale.CHINA;
}
}

/*
* Map languages
*/
Expand Down Expand Up @@ -190,9 +206,10 @@ public void setMapLanguage(@NonNull Locale locale) {
public void setMapLanguage(@NonNull Locale locale, boolean acceptFallback) {
MapLocale mapLocale = MapLocale.getMapLocale(locale, acceptFallback);
if (mapLocale != null) {
Timber.d("Locale: %s, set MapLocale: %s", locale.toString(), mapLocale.getMapLanguage());
setMapLanguage(mapLocale);
} else {
Timber.e("Couldn't match Locale %s to a MapLocale", locale.getDisplayName());
Timber.e("Couldn't match Locale %s %s to a MapLocale", locale.toString(), locale.getDisplayName());
}
}

Expand Down Expand Up @@ -221,7 +238,8 @@ public void setMapLanguage(@NonNull MapLocale mapLocale) {
if (isStreetsV8) {
convertExpressionV8(mapLocale, layer, textFieldProperty);
} else {
convertExpression(mapLocale, layer, textFieldProperty);
boolean isStreetsV7 = sourceIsStreetsV7(source);
convertExpression(mapLocale, layer, textFieldProperty, isStreetsV7);
}
}
}
Expand All @@ -240,10 +258,19 @@ public void setMapLanguage(@NonNull MapLocale mapLocale) {
}
}

private void convertExpression(@NonNull MapLocale mapLocale, Layer layer, PropertyValue<?> textFieldProperty) {
private void convertExpression(@NonNull MapLocale mapLocale, Layer layer,
PropertyValue<?> textFieldProperty, boolean isStreetsV7) {
Expression textFieldExpression = textFieldProperty.getExpression();
if (textFieldExpression != null) {
String text = textFieldExpression.toString().replaceAll(EXPRESSION_REGEX, mapLocale.getMapLanguage());
MapLocale newMapLocale = mapLocale;
String mapLanguage = mapLocale.getMapLanguage();
if (mapLanguage.startsWith("name_zh")) {
// need to re-get mapLocale, since the default is for street-v8
newMapLocale = getChineseMapLocale(mapLocale, isStreetsV7);
Timber.d("reset mapLocale to: %s", newMapLocale.getMapLanguage());
}

String text = textFieldExpression.toString().replaceAll(EXPRESSION_REGEX, newMapLocale.getMapLanguage());
if (text.startsWith("[\"step") && textFieldExpression.toArray().length % 2 == 0) {
// got an invalid step expression from core, we need to add an additional name_x into step
text = text.replaceAll(STEP_REGEX, STEP_TEMPLATE);
Expand All @@ -260,11 +287,9 @@ private void convertExpressionV8(@NonNull MapLocale mapLocale, Layer layer, Prop

String mapLanguage = mapLocale.getMapLanguage();
if (!mapLanguage.equals(MapLocale.ENGLISH)) {
if (mapLanguage.equals(MapLocale.CHINESE)) {
// in streets v8 tiles chinese is declared as "name_zh-Hant" instead of "name_zh"
mapLanguage = MapLocale.CHINESE_V8;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still needed, otherwise, we'll break the compatibility of MapLocale.CHINESE with streets-v8 sources.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be ignored if the proposed solution is implemented.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the current implementation, this is still needed.

if (mapLanguage.equals("name_zh")) {
mapLanguage = MapLocale.SIMPLIFIED_CHINESE;
}

stringExpression = stringExpression.replaceAll(EXPRESSION_V8_REGEX_BASE,
String.format(Locale.US,
EXPRESSION_V8_TEMPLATE_LOCALIZED,
Expand Down Expand Up @@ -349,6 +374,14 @@ private boolean sourceIsFromMapbox(Source singleSource) {
return false;
}

private boolean sourceIsStreetsV7(Source source) {
if (source instanceof VectorSource) {
String url = ((VectorSource) source).getUrl();
return url != null && url.contains("mapbox.mapbox-streets-v7");
}
return false;
}

private boolean sourceIsStreetsV8(Source source) {
if (source instanceof VectorSource) {
String url = ((VectorSource) source).getUrl();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ public final class MapLocale {
public static final String CHINESE = "name_zh";

/**
* Chinese (if available)
* Traditional Chinese (if available)
*/
static final String CHINESE_V8 = "name_zh-Hant";
static final String TRADITIONAL_CHINESE = "name_zh-Hant";

/**
* Simplified Chinese (if available)
Expand All @@ -106,8 +106,8 @@ public final class MapLocale {
public static final String KOREAN = "name_ko";

@Retention(SOURCE)
@StringDef( {LOCAL_NAME, ENGLISH, FRENCH, SIMPLIFIED_CHINESE, ARABIC, SPANISH, GERMAN, PORTUGUESE,
RUSSIAN, CHINESE, JAPANESE, KOREAN})
@StringDef( {LOCAL_NAME, ARABIC, CHINESE, SIMPLIFIED_CHINESE, TRADITIONAL_CHINESE, ENGLISH,
FRENCH, GERMAN, JAPANESE, KOREAN, PORTUGUESE, RUSSIAN, SPANISH})
public @interface Languages {
}

Expand Down Expand Up @@ -143,6 +143,13 @@ public final class MapLocale {
.include(new LatLng(53.56086, 73.557693))
.include(new LatLng(15.775416, 134.773911)).build();

/**
* Taiwan Bounding Box extracted from Open Street Map
*/
static final LatLngBounds TAIWAN_BBOX = new LatLngBounds.Builder()
.include(new LatLng(26.389444, 118.115255566105))
.include(new LatLng(21.733333, 122.107778)).build();

/**
* Germany Bounding Box extracted from Open Street Map
*/
Expand Down Expand Up @@ -171,13 +178,6 @@ public final class MapLocale {
.include(new LatLng(51.092804, -5.142222))
.include(new LatLng(41.371582, 9.561556)).build();

/**
* Peoples Republic of China Bounding Box extracted from Open Street Map
*/
static final LatLngBounds PRC_BBOX = new LatLngBounds.Builder()
.include(new LatLng(53.56086, 73.557693))
.include(new LatLng(15.775416, 134.773911)).build();

/**
* Russian Bounding box extracted from Open Street Map
*/
Expand Down Expand Up @@ -224,7 +224,17 @@ public final class MapLocale {
/**
* Useful constant for country.
*/
public static final MapLocale PRC = new MapLocale(SIMPLIFIED_CHINESE, PRC_BBOX);
public static final MapLocale TAIWAN = new MapLocale(TRADITIONAL_CHINESE, TAIWAN_BBOX);

/**
* Useful constant for country. General Simplified Chinese
*/
public static final MapLocale CHINESE_HANS = new MapLocale(SIMPLIFIED_CHINESE);

/**
* Useful constant for country. General Traditional Chinese
*/
public static final MapLocale CHINESE_HANT = new MapLocale(TRADITIONAL_CHINESE);

/**
* Useful constant for country.
Expand Down Expand Up @@ -267,15 +277,37 @@ public final class MapLocale {
LOCALE_SET.put(Locale.US, MapLocale.US);
LOCALE_SET.put(Locale.CANADA_FRENCH, MapLocale.CANADA_FRENCH);
LOCALE_SET.put(Locale.CANADA, MapLocale.CANADA);
LOCALE_SET.put(Locale.CHINA, MapLocale.CHINA);
LOCALE_SET.put(Locale.PRC, MapLocale.PRC);
LOCALE_SET.put(Locale.CHINA, MapLocale.CHINESE_HANS);
LOCALE_SET.put(Locale.TAIWAN, MapLocale.TAIWAN);
LOCALE_SET.put(Locale.UK, MapLocale.UK);
LOCALE_SET.put(Locale.JAPAN, MapLocale.JAPAN);
LOCALE_SET.put(Locale.KOREA, MapLocale.KOREA);
LOCALE_SET.put(Locale.GERMANY, MapLocale.GERMANY);
LOCALE_SET.put(Locale.FRANCE, MapLocale.FRANCE);
LOCALE_SET.put(new Locale("ru", "RU"), RUSSIA);
LOCALE_SET.put(new Locale("es", "ES"), SPAIN);

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
Locale zh_CN_Hans = new Locale.Builder().setLanguage("zh").setRegion("CN").setScript("Hans").build();
Locale zh_HK_Hans = new Locale.Builder().setLanguage("zh").setRegion("HK").setScript("Hans").build();
Locale zh_MO_Hans = new Locale.Builder().setLanguage("zh").setRegion("MO").setScript("Hans").build();
Locale zh_SG_Hans = new Locale.Builder().setLanguage("zh").setRegion("SG").setScript("Hans").build();

Locale zh_TW_Hant = new Locale.Builder().setLanguage("zh").setRegion("TW").setScript("Hant").build();
Locale zh_HK_Hant = new Locale.Builder().setLanguage("zh").setRegion("HK").setScript("Hant").build();
Locale zh_MO_Hant = new Locale.Builder().setLanguage("zh").setRegion("MO").setScript("Hant").build();

// streets v8 supports name_zh-Hans(MapLocale.CHINESE_HANS) and name_zh-Hant(MapLocale.CHINESE_HANT)
// https://docs.mapbox.com/vector-tiles/reference/mapbox-streets-v8/#name-text--name_lang-code-text
LOCALE_SET.put(zh_CN_Hans, MapLocale.CHINESE_HANS);
LOCALE_SET.put(zh_HK_Hans, MapLocale.CHINESE_HANS);
LOCALE_SET.put(zh_MO_Hans, MapLocale.CHINESE_HANS);
LOCALE_SET.put(zh_SG_Hans, MapLocale.CHINESE_HANS);

LOCALE_SET.put(zh_TW_Hant, MapLocale.TAIWAN);
LOCALE_SET.put(zh_HK_Hant, MapLocale.CHINESE_HANT);
LOCALE_SET.put(zh_MO_Hant, MapLocale.CHINESE_HANT);
}
}

private final LatLngBounds countryBounds;
Expand Down Expand Up @@ -413,4 +445,4 @@ private static MapLocale getMapLocaleFallback(@NonNull Locale locale) {
}
return foundMapLocale;
}
}
}