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.
Related
I have an app we need to translate to several languages. So, I made values-en, values-pt, etc. One of the languages is turkish. So, I made my folder values-tr, and inside that, strings.xml file. Everything goes ok, except for one particular Activity. The strings that appear there are the ones in spanish (the default language in our app). BUT, if I change to another language (say, russian) and back to turkish, the strings are shown ok.
TextView mTitle = toolbarTop.findViewById(R.id.toolbar_title);
mTitle.setText(getString(R.string.mapproviders));
(In the strings.xml in values-tr):
<string name="mapproviders">Tedarikçi Haritası</string>
It works ok for other languages, it only fails in turkish and in that particular Activity.
Anyone has faced a similar problem?
UPDATE:
This is the way I am changing Locale:
public static void changeLang(String lang, Context ctx) {
Configuration config = ctx.getResources().getConfiguration();
if (!"".equals(lang) && !config.locale.getLanguage().equals(lang)) {
Locale locale = new Locale(lang);
Locale.setDefault(locale);
Configuration conf = new Configuration(config);
conf.locale = locale;
ctx.getResources().updateConfiguration(conf, ctx.getResources().getDisplayMetrics());
}
}
I am adding a new feature to an android application, that enables the users to switch between two languages.
So far to test out the change of Locale I have been using the following piece of code in one of my Activities.
Locale myLocale = new Locale("ru");
Locale.setDefault(myLocale);
Resources res = this.getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
Locale current = getResources().getConfiguration().locale;
if (current.getLanguage().equals(myLocale.getLanguage())){
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
conf.setLocale(myLocale);
createConfigurationContext(conf);
recreate();
}else{
conf.locale = myLocale;
res.updateConfiguration(conf, res.getDisplayMetrics());
recreate();
}
}
setContentView(R.layout.activity_main);
The reason for checking the Version Code is that createConfigurationContext() requires API Level 17 and I'm using API Level 16. So this conditional lets me use the deprecated updateConfiguration() and also the createConfigurationContext().
I have also created the strings.xml for the relevant language and added a few values for testing.
What has been achieved so far is that the Dates are being shown in the translated language, so that tells me that the Locale has actually been changed to the correct one. Other strings that are supposedly taken from strings.xml (ru) aren't changing at all.
Are strings with multiple translations called in a different way from the layout xmls (See code below)? or maybe my structure isn't right?
android:text="#string/upcoming_title"
After posting the question I kept trying some stuff, and decided to try to add the following answer from another post:
https://stackoverflow.com/a/40704077/2199589
This worked as expected.
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.
I have been spending the last few hours trying to figure out why my Arabic strings are not appearing on the ArcGis map.
The rest of the app is happily displaying Arabic (included the map "callout"). I thought it might have something to do with:
TextSymbol textSymbol = new TextSymbol(22, name, Color.BLACK);
Where name is something like:
name: المدينة المنورة
Finally, I found a single line in the sparse ArcGis Android documentation:
"The language used by the app is determined by your device's language setting."
I have been setting the localization in the app explicitly with:
private void setArabic() {
Resources res = getResources();
// Change locale settings in the app.
DisplayMetrics dm = res.getDisplayMetrics();
android.content.res.Configuration conf = res.getConfiguration();
conf.locale = new Locale("ar");
res.updateConfiguration(conf, dm);
}
The ArcGis documentation seems to suggest that the app localization is ignored and Arabic needs to be set on the phone settings.
If this is true, I need to determine the standard way to do this and the user's of the app will need to do the same.
The Play Store has hundreds of localization apps that say they will add Arabic but is there a way to do this using Google code rather than untrusted 3rd parties?
Sincerely, FeiCui
I have a setting in my app that allows user to select different localization (language), ie Chinese, German, etc.
What i would like to do is that once the user makes their choice, to immediately update the layout with strings in the currently selected language. Of course, i want the lang change propagated to ALL current activities, without reloading the app.
I found this (havent tried yet), but was wondering if there is a cleaner way of doing it.
http://www.tutorialforandroid.com/2009/01/force-localize-application-on-android.html
Gracias
I also had this issue.I used the code below and then it changed the language without refreshing the activity
public void setLocale(String lang) {
myLocale = new Locale(lang);
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
onConfigurationChanged(conf);
/*Intent refresh = new Intent(this, AndroidLocalize.class);
startActivity(refresh);*/
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
// refresh your views here
lblLang.setText(R.string.langselection);
super.onConfigurationChanged(newConfig);
}
I hope it would help you.......
There are some steps that you should implement
First, you need to change the locale of your configuration
Resources resources = context.getResources();
Configuration configuration = resources.getConfiguration();
configuration.locale = new Locale(language);
resources.updateConfiguration(configuration, resources.getDisplayMetrics());
Second, if you want your changes to apply directly to the layout that is visible, you either can update the views directly or you can just call activity.recreate() to restart the current activity.
And also you have to persist your changes because after user closes your application then you would lose the language change.
I explained more detailed solution on my blog post Change Language Programmatically in Android
Basically, you just call LocaleHelper.onCreate() on your application class and if you want to change locale on the fly you can call LocaleHelper.setLocale()