mirror of
https://codeberg.org/comaps/comaps
synced 2025-12-21 05:43:37 +00:00
[android] Add function to get all android system languages
Signed-off-by: gekeleda <git@davidgekeler.eu>
This commit is contained in:
@@ -9,8 +9,7 @@
|
|||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
/// This function is called from native c++ code
|
std::vector<std::string> GetAndroidSystemLanguages()
|
||||||
std::string GetAndroidSystemLanguage()
|
|
||||||
{
|
{
|
||||||
static char const * DEFAULT_LANG = "en";
|
static char const * DEFAULT_LANG = "en";
|
||||||
|
|
||||||
@@ -18,20 +17,31 @@ std::string GetAndroidSystemLanguage()
|
|||||||
if (!env)
|
if (!env)
|
||||||
{
|
{
|
||||||
LOG(LWARNING, ("Can't get JNIEnv"));
|
LOG(LWARNING, ("Can't get JNIEnv"));
|
||||||
return DEFAULT_LANG;
|
return {DEFAULT_LANG};
|
||||||
}
|
}
|
||||||
|
|
||||||
static jclass const languageClass = jni::GetGlobalClassRef(env, "app/organicmaps/sdk/util/Language");
|
static jclass const languageClass = jni::GetGlobalClassRef(env, "app/organicmaps/sdk/util/Language");
|
||||||
static jmethodID const getDefaultLocaleId =
|
|
||||||
jni::GetStaticMethodID(env, languageClass, "getDefaultLocale", "()Ljava/lang/String;");
|
|
||||||
|
|
||||||
jni::TScopedLocalRef localeRef(env, env->CallStaticObjectMethod(languageClass, getDefaultLocaleId));
|
static jmethodID const getSystemLocalesId =
|
||||||
|
jni::GetStaticMethodID(env, languageClass, "getSystemLocales", "()[Ljava/lang/String;");
|
||||||
|
|
||||||
std::string res = jni::ToNativeString(env, (jstring)localeRef.get());
|
jni::TScopedLocalRef resultArray(env, env->CallStaticObjectMethod(languageClass, getSystemLocalesId));
|
||||||
if (res.empty())
|
|
||||||
res = DEFAULT_LANG;
|
|
||||||
|
|
||||||
return res;
|
jobjectArray array = static_cast<jobjectArray>(resultArray.get());
|
||||||
|
size_t len = env->GetArrayLength(array);
|
||||||
|
|
||||||
|
std::vector<std::string> languages;
|
||||||
|
languages.reserve(len);
|
||||||
|
|
||||||
|
for (size_t i = 0; i < len; ++i)
|
||||||
|
{
|
||||||
|
jni::TScopedLocalRef elem(env, env->GetObjectArrayElement(array, i));
|
||||||
|
std::string lang = jni::ToNativeString(env, static_cast<jstring>(elem.get()));
|
||||||
|
|
||||||
|
languages.push_back(lang);
|
||||||
|
}
|
||||||
|
|
||||||
|
return languages;
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace platform
|
namespace platform
|
||||||
|
|||||||
@@ -1,11 +1,16 @@
|
|||||||
package app.organicmaps.sdk.util;
|
package app.organicmaps.sdk.util;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.content.res.Resources;
|
||||||
|
import android.os.Build;
|
||||||
|
import android.os.LocaleList;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
import android.view.inputmethod.InputMethodManager;
|
import android.view.inputmethod.InputMethodManager;
|
||||||
import android.view.inputmethod.InputMethodSubtype;
|
import android.view.inputmethod.InputMethodSubtype;
|
||||||
import androidx.annotation.Keep;
|
import androidx.annotation.Keep;
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
public class Language
|
public class Language
|
||||||
@@ -33,6 +38,36 @@ public class Language
|
|||||||
return lang;
|
return lang;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Keep
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
@NonNull
|
||||||
|
public static String[] getSystemLocales()
|
||||||
|
{
|
||||||
|
List<String> result = new ArrayList<>();
|
||||||
|
|
||||||
|
// Check for Android version high enough to support Locale Preference list
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
|
||||||
|
{
|
||||||
|
// Gets user language preference list, on system level - app-specific changes are not applied
|
||||||
|
// (For that a Context object would be necessary)
|
||||||
|
LocaleList list = Resources.getSystem().getConfiguration().getLocales();
|
||||||
|
for (int i = 0; i < list.size(); i++)
|
||||||
|
{
|
||||||
|
String lang = list.get(i).toString();
|
||||||
|
if (lang.startsWith("in"))
|
||||||
|
lang = "id";
|
||||||
|
if (lang.startsWith("iw"))
|
||||||
|
lang = "he";
|
||||||
|
result.add(lang);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
result.add(Locale.getDefault().toString());
|
||||||
|
}
|
||||||
|
return result.toArray(new String[0]);
|
||||||
|
}
|
||||||
|
|
||||||
// After some testing on Galaxy S4, looks like this method doesn't work on all devices:
|
// After some testing on Galaxy S4, looks like this method doesn't work on all devices:
|
||||||
// sometime it always returns the same value as getDefaultLocale()
|
// sometime it always returns the same value as getDefaultLocale()
|
||||||
@NonNull
|
@NonNull
|
||||||
|
|||||||
@@ -24,7 +24,7 @@
|
|||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#elif defined(OMIM_OS_ANDROID)
|
#elif defined(OMIM_OS_ANDROID)
|
||||||
/// Body for this function is inside android/sdk/src/main/cpp sources
|
/// Body for this function is inside android/sdk/src/main/cpp sources
|
||||||
std::string GetAndroidSystemLanguage();
|
std::vector<std::string> GetAndroidSystemLanguages();
|
||||||
#else
|
#else
|
||||||
#error "Define language preferences for your platform"
|
#error "Define language preferences for your platform"
|
||||||
#endif
|
#endif
|
||||||
@@ -514,7 +514,9 @@ struct SystemLanguages
|
|||||||
}
|
}
|
||||||
|
|
||||||
#elif defined(OMIM_OS_ANDROID)
|
#elif defined(OMIM_OS_ANDROID)
|
||||||
m_langs.push_back(GetAndroidSystemLanguage());
|
std::vector<std::string> system_langs = GetAndroidSystemLanguages();
|
||||||
|
for (auto const & lang : system_langs)
|
||||||
|
m_langs.push_back(lang);
|
||||||
#else
|
#else
|
||||||
#error "Define language preferences for your platform"
|
#error "Define language preferences for your platform"
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user