I have a localized string resource A. I noticed that after I clear app data/cache (under setting), and open my activity (which belongs to that app), I always see resource A in en locale regardless of the current device language. If I go to setting again, change device language manually to whatever, and go back to my activity, then resource A is localized properly again.
I wondering why locale is set to default after app data/cache is cleared and is there a way to fix this? Thanks.
This is a hacky solution but you could set the locale in the launcher activity like this:
Configuration config = res.getConfiguration();
Configuration configuration = new Configuration();
config.locale = config.locale;
getBaseContext().getResources().updateConfiguration(configuration,
getBaseContext().getResources().getDisplayMetrics());
You can also check the device locale to be absolutely sure that the device locale has changed.
Related
Is there a way to force using english as the locale for my android app even if the phone's locale is something other that english? The reason I want to do this is that I don't want my UI to change orientatoin from ltr to rtl.
this line
mStrLocale = Locale.getDefault().getLanguage();
will return your current language, lets say EN-US
and with this you can hardcode your locale
Locale locale = new Locale("ru");
Locale.setDefault(locale);
Configuration config = getBaseContext().getResources().getConfiguration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
and we use Configuration because of this
This class describes all device configuration information that can
impact the resources the application retrieves. This includes both
user-specified configuration options (locale list and scaling) as well
as device configurations (such as input modes, screen size and screen
orientation).
is stated at this doc https://developer.android.com/reference/android/content/res/Configuration.html
I've created a values-zh_CN directory in my res folder for Simplified Chinese localization. Eclipse does not accept that folder name, it marks it as an error, the directory itself.
The problem is definitely with the directory name, if I change the directory name to values-nl for example the error comes off.
The only name Eclipse accepts is values-zh-rCN which compiles fine but the actual locale is not loaded (Default en is loaded instead).
If you named dir for example values-zh it will be loaded only when Chinese is chosen in system language settings. You should know about that.
Value zh-rCN is correct and everything should work correctly. Read my notice above.
Use following code its working for me for traditional and simplified chinese.
if(selectedLanguage.equals("zh_CN"))
locale = Locale.SIMPLIFIED_CHINESE;
else if(selectedLanguage.equals("zh_TW"))
locale = Locale.TRADITIONAL_CHINESE;
else
locale = new Locale(selectedLanguage);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
baseContext.getResources().updateConfiguration(config, baseContext.getResources().getDisplayMetrics());
The correct locales are zh-rCN and zh-rTW,so then whatever Android software your using isn't correctly setting locale values.
Look into settings -> Language & Input to double check that Language is on Chinese, and if that fails look in the market for an application called MoreLocales2, it allows you to get around some of those stock Samsung softwares that prevent locale changing from working.
In my app, i am asking for language, can be either English or German. The selected language and its iso code is saved into preferences. On the basis of selected language i need to change all the texts into corresponding language.
For this, i have created res/values and res/values-de; each folder containing a strings.xml file. Issues are:
1) I am opening camera as well as a screen using opengl. After navigating via both of them, the texts does not change completely into german(if was chosen). Some text values change into German, rest not even on the same page.
2) Even without going through camera and opengl screens, the results are not achieved 100% always but gives a better result always as compared to case 1.
My implementations:
1) in onResume() of splash screen, i am changing locale based on preferences with the help of config.locale().
2) in manifest file, each activity is set with activity:configChanges="locale".
3) in camera activity and opengl activity, onConfigurationChanged() is overridden in which i am again setting locale as per preferences.
please guide how to solve the locale issue.
Check Point 1:
Partial updation of resources. Make sure you have resources named correctly in all your languages.
Also, you can give this code a try. It is how i update locale of my app:
public void updateLocale(String language) {
Locale myLocale = new Locale(language);
Locale.setDefault(myLocale);
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
}
Let me know if it helps !
I believe you need to add something to your AndroidManifest file indicating locale change. Here is an example: http://android.programmerguru.com/android-localization-at-runtime/
I had a similar problem with my app. App has force language changing option between Turkish and English. If my device language is English and if I use the app in Turkish, I can easily convert the language with the code below:
public void setAppLanguage(String languageCode) {
String countryCode;
if (languageCode.equals("tr")){
countryCode = "TR";
}else{
countryCode = "US";
}
Locale locale = new Locale(languageCode, countryCode);
Locale.setDefault(locale);
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = locale;
res.updateConfiguration(conf, dm);
loginPrefsEditor.putString("uLang",languageCode);
loginPrefsEditor.apply();
}
However, when I open the camera and return from it, app language changes back to default phone language. App kills my activity when I open the camera. After return from the camera, it refreshes everything. So in order to solve this problem, I put my force change language method in onResume() method of every activity.
By doing this I would be able to solve this issue.
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"
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.