Android: Change to unsupported locale - android

I been trying to change locale using the next code:
private void changeLocal(Locale locale){
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
refresh();
}
private void refresh() {
finish();
Intent myIntent = new Intent(this, getClass());
startActivity(myIntent);
}
It works for French but not for Hebrew. So I looked in to device supported locales by calling Locale.getAvailableLocales() and I found out that French is there but Hebrew not.
If I put hardcoded Hebrew text I can see it, so it is installed, but I do not know how to force the device to use it.
Please help me force the device using Hebrew.
P.S
Wasn't been able to make this work without the refresh even when adding
android:configChanges="locale|layoutDirection"
to my Manifest it does not call the onConfigurationChanged of my Activity

Since there are 2 distinct standards, you have to double your values folder.
Add your Hebrew strings to these folders:
values-he
and
values-iw
Some devices will use one folder. Other devices will use the other one.
Note that he is actually deprecated. So, most devices will use iw

Related

RTL is not working properly

I am currently working on an Android app that needs to support languages English and Arabic, i am using this code to switch from a language to the other.
String languageToLoad; // your language
if (languageSwitch.isChecked()) {
languageToLoad = "ar";//arabic
} else {
languageToLoad = "en";//english
}
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
// store it in the cache for any further use
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
SharedPreferencesUtil.putString(this, "language", languageToLoad);
It was working fine, i've made 2 layouts folders, a normal one and a layout-ar. It was working correctly but sometimes it gets messed up and instead of showing the Arabic one, it shows the English layout and the app continues on running in the English mode only.
At the end you should restart you activity. Try this.
public void updateActivity() {
Intent intent = new Intent(getActivity(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
It turns out that the webview is the one responsible for the problem, once the webview is generated it deletes all the overrided local data and inserts the one from the device,in order to correct the problem you need to follow the steps found in this link.

make multi language android application

I created multi language (English, Russian, Uzbek) app. I put 4 string resoureses in 4 folders (values, values-en, values-ru, values-uz) as docs. When I change app language updates resourses configuration in App Controller like below:
Settings.LANGUAGE = prefs.getString(User.LANG, Settings.RUSSIAN);
Locale locale = new Locale(Settings.LANGUAGE);
Locale.setDefault(locale);
Configuration configuration = new Configuration();
configuration.locale = locale;
getBaseContext().getResources().updateConfiguration(configuration,
getBaseContext().getResources().getDisplayMetrics());
After that App restarts by calling App controller's method like below:
public void reStart() {
Intent i = getBaseContext().getPackageManager()
.getLaunchIntentForPackage(getBaseContext().getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
After them It works well almost all devises. But on Samsung Galaxy S6 (SM-G920F), it works as crazy. Some words are in english and others are in Uzbek and ets.
So, How to fix this error? isn't the concepts of "Supporting Different Languages" supported by (applicable to) all devices?
By the way, I have checked that all resources are given in corresponding languages (as shown in attached image):
From my observations, weird behaviour was affecting only Activity titles, and I found that I was setting translations of activity titles in Manifest file. Only these translations were misbehaving. All other dynamically set translations were working fine.
So, to fix the problem, I removed all activity labels from Manifest file, then set activity titles in onCreate method as below:
getSupportActionBar().setTitle(R.string.title_activity_followers);
Problem solved.

How to change language setting of the whole system in android without Intent?

I'm trying to change the language of the whole system of Android Phone on my application, cause our goal is to customized a Settings application.
I've tried this, but didn't work:
Configuration conf = Resources.getSystem().getConfiguration();
conf.locale = toSet; //toSet is a Locale which I want to set to the system
conf.setToDefaults();
the other try didn't work either:
Locale.setDefault(toSet)
There are ways to change the language of application, but not systems.
Is there any ways that I can reach the goal?
Will getting the root permissions works for me?
Or is there any ways to modify the UI interface of the Intent-Activity?
I've found that, in the source code, "platform/packages/apps/Settings/src/com/android/settings/LocalePicker.java", "com.android.internal.app.LocalePicker" might help, and in the "platform/frameworks/base/core/java/com/android/internal/app", there is a function which named "updateLocale", I guess it might help, but the class such as "IActivityManager" cause compile error in the Eclipse.
Does anyone have any ideas? Thanks!
try this
String languageToLoad = "zh";
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, null);
AndroidManifest:
add android:configChanges="locale"

changing locale in app on Android 2.2 platform (on Android 4.0 is ok) in my case

I would like to change my app locale programmatically. I use the following code to do the task which is in onCreate() method of Activity:
#Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
Resources res = context.getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration config= new Configuration();
config.locale = new Locale("fr"); //use French locale
res.updateConfiguration(config, dm);
//following code will navigate among fragments
...
}
My app only has one Activity which hosts several fragments.
If I run my app on Android 4.0 platform, it is working nicely, every fragment display with "French" language.
But if I run on Android 2.2 platform, only the first fragment display in French language, the next replaced fragment still show English (my phone setting use English locale).
Why it works only on Android 4.0 platform ??
It looks like some bug in Android 2.x
Because if you change locale not in onCreate() but in createMenu() or inside some spinner.setOnItemSelectedListener() it will work.
It is workaround but try to call some event after start your app and change locale in it.

Localize Android application so I can switch locale inside app

How do I localize application so it uses specific locale regardless of what locale set on device? I want make it possible for users to set language of their choice.
So far I have code like this in my Application class:
#Override
public void onCreate()
{
//Set locale
String l = Preferences.getLocale(getApplicationContext());
if (!l.equals(""))
{
Locale locale = new Locale(l);
Locale.setDefault(locale);
Configuration config = getBaseContext().getResources().getConfiguration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(
config, getBaseContext().getResources().getDisplayMetrics());
}
LogData.InsertMessage(getApplicationContext(), "Application started");
}
Problem that I have is that it seems like I display in set locale just fine (TextViews)
But Menu captions and toasts will fall to default locale.
Is there any 1-2-3 on how to get it working properly? I uses 2.2 version
This post explains how to force localization in your app.
Ok, I figured why I had this problem.. I needed to override onConfigurationChanged in my application class. That is much more elegant solution than to specify locale on each Activity.

Categories

Resources