Skip to content

Experimental and non-supported code to localize standard style #2288

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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 @@ -5,65 +5,73 @@ import android.view.Menu
import android.view.MenuItem
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.mapbox.bindgen.Value
import com.mapbox.common.MapboxCommonSettings
import com.mapbox.common.SettingsServiceFactory
import com.mapbox.common.SettingsServiceStorageType
import com.mapbox.maps.MapboxMap
import com.mapbox.maps.Style
import com.mapbox.maps.extension.localization.localizeLabels
import com.mapbox.maps.testapp.R
import com.mapbox.maps.testapp.databinding.ActivityMapLocalizationBinding
import java.util.*
import java.util.Locale

/**
* Example showcasing how to localize a map client side to a specific locale using Style#localizeLabels(locale: Locale).
* This function will attempt to localize a map into a selected locale if the symbol layers are using
* a Mapbox source and the locale is being provided as part of the vector source data.
* Experimental not fully supported example showcasing how to localize a map client side to a specific locale.
*
* This feature supports both v7 and v8 of Mapbox style spec version and does not support [Style.STANDARD].
* This feature supports [Style.STANDARD].
*/
class LocalizationActivity : AppCompatActivity() {
private lateinit var mapboxMap: MapboxMap
private var applySelectedLanguage: Boolean = false
private var index: Int = 0
private val styles =
arrayOf(
Style.MAPBOX_STREETS,
MAPBOX_STREETS_V10
Style.STANDARD,
Style.MAPBOX_STREETS
)

private val nextStyle: String
get() {
return styles[index++ % styles.size]
}
private lateinit var locale: Locale
private lateinit var deviceLocale: Locale
private lateinit var selectedLocale: Locale
private var layerIdList = mutableSetOf<String>()

private fun applyLocale(selectedLocale: Locale) {
val settingsService = SettingsServiceFactory.getInstance(SettingsServiceStorageType.PERSISTENT)
settingsService.set(MapboxCommonSettings.LANGUAGE, Value.valueOf(selectedLocale.toLanguageTag()))
// Force style reload
mapboxMap.style?.let {
mapboxMap.loadStyle(it.styleURI) {
// re-apply here any custom style
}
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding = ActivityMapLocalizationBinding.inflate(layoutInflater)
setContentView(binding.root)
@Suppress("DEPRECATION")
locale = resources.configuration.locale
selectedLocale = locale
deviceLocale = resources.configuration.locale
selectedLocale = deviceLocale
applySelectedLanguage = false
Toast.makeText(this, R.string.change_language_instruction, Toast.LENGTH_LONG).show()
mapboxMap = binding.mapView.mapboxMap
mapboxMap.loadStyle(nextStyle) {
it.localizeLabels(locale)
}
applyLocale(deviceLocale)
mapboxMap.loadStyle(nextStyle)
binding.fabStyles.setOnClickListener {
val styleUri = nextStyle
mapboxMap.loadStyle(styleUri) {
it.localizeLabels(selectedLocale)
}
mapboxMap.loadStyle(styleUri)
Toast.makeText(this, styleUri, Toast.LENGTH_SHORT).show()
}
binding.fabLocalize.setOnClickListener {
applySelectedLanguage = if (!applySelectedLanguage) {
mapboxMap.style?.localizeLabels(selectedLocale)
applyLocale(selectedLocale)
Toast.makeText(this, R.string.map_not_localized, Toast.LENGTH_SHORT).show()
true
} else {
mapboxMap.style?.localizeLabels(locale)
applyLocale(deviceLocale)
Toast.makeText(this, R.string.map_localized, Toast.LENGTH_SHORT).show()
false
}
Expand All @@ -77,25 +85,6 @@ class LocalizationActivity : AppCompatActivity() {

override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.groupId) {
R.id.layers -> {
item.isChecked = !item.isChecked
when (item.itemId) {
R.id.country_label -> {
if (item.isChecked) {
layerIdList.add(COUNTRY_LABEL)
} else {
layerIdList.remove(COUNTRY_LABEL)
}
}
R.id.state_label -> {
if (item.isChecked) {
layerIdList.add(STATE_LABEL)
} else {
layerIdList.remove(STATE_LABEL)
}
}
}
}
R.id.group -> {
applySelectedLanguage = true
item.isChecked = true
Expand All @@ -105,25 +94,17 @@ class LocalizationActivity : AppCompatActivity() {
R.id.french -> Locale.FRENCH
R.id.german -> Locale.GERMAN
R.id.russian -> Locale("ru", "RU")
R.id.chinese -> Locale.CHINESE
R.id.simplified_chinese -> Locale.SIMPLIFIED_CHINESE
R.id.portuguese -> Locale("pt", "PT")
R.id.japanese -> Locale.JAPANESE
R.id.korean -> Locale.KOREAN
R.id.vietnamese -> Locale("vi", "VN")
R.id.italian -> Locale.ITALIAN
else -> locale
else -> deviceLocale
}
}
else -> return super.onOptionsItemSelected(item)
}
mapboxMap.style?.localizeLabels(selectedLocale, layerIdList.toList())
applyLocale(selectedLocale)
return true
}

companion object {
private const val MAPBOX_STREETS_V10 = "mapbox://styles/mapbox/streets-v10"
private const val STATE_LABEL = "state-label"
private const val COUNTRY_LABEL = "country-label"
}
}
28 changes: 0 additions & 28 deletions app/src/main/res/menu/menu_languages.xml
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<group
android:id="@+id/layers"
android:checkableBehavior="all">
<item
android:id="@+id/country_label"
android:title="@string/country_label"
app:showAsAction="never" />
<item
android:id="@+id/state_label"
android:title="@string/state_label"
app:showAsAction="never" />
</group>
<group
android:id="@+id/group"
android:checkableBehavior="single">
Expand Down Expand Up @@ -41,17 +29,6 @@
android:title="@string/russian"
app:showAsAction="never" />

<item
android:id="@+id/chinese"
android:title="@string/chinese"
app:showAsAction="never" />

<item
android:id="@+id/simplified_chinese"
android:title="@string/simplified_chinese"
app:showAsAction="never" />


<item
android:id="@+id/portuguese"
android:title="@string/portuguese"
Expand All @@ -76,10 +53,5 @@
android:id="@+id/italian"
android:title="@string/italian"
app:showAsAction="never" />

<item
android:id="@+id/local"
android:title="@string/local_names"
app:showAsAction="never" />
</group>
</menu>