K
K
korvin22022-01-10 11:45:57
Java
korvin2, 2022-01-10 11:45:57

How to display city name in English?

You need to get the name of the city of the current location in English using Geocoder (not necessarily with it. If there is another simple option, please write), but it outputs in transliteration. For example, you need "Saint-Peterburg", but I get "Sankt-Peterburg" or instead of "Moscow" it gives "Moskva"
Code:

val aLocale = Locale.Builder().setLanguage("en").setRegion("US").setScript("Latn").build()
            val gcd = Geocoder(requireContext(), aLocale)
            var addresses: List<Address>? = null
            try {
                addresses = gcd.getFromLocation(it.latitude, it.longitude, 1)
            } catch (e: IOException) {
                e.printStackTrace()
            }
            if (addresses != null && addresses.isNotEmpty()) {
                val locality: String = addresses[0].locality
                Log.d("LOCALE", "city: $locality")
            }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
korvin2, 2022-01-16
@korvin2

The problem was solved simply) Initially, it was necessary to compare the user's city with the cities coming from the api. As a result, for each city from the api, I created my own Geocoder and checked it with the user

A
Alexander Interesting, 2022-01-11
@Swimergg

I see this in my head: either some kind of xml file with data, or like this:

строка вход = КакойтоТранслейтор.перевести('Moscow');
список abc = ['a', 'b', 'c'];
список абв = ['a', 'б', 'с'];

вход.заменить(abc, абв);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question