How to load arabic string.xml when device language is English - android

It's very easy to place different string.xml files for multiple languages like:
English
Values
String.xml
Arabic
Values-ar
String.xml
but it only works when device language is changed. My scenario is I have to load a user selected language in the application, different from the one selected in the device so how can I load different String.xml files?

You can change the application language using the following code:
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
languageToLoad is a String variable like "en" or "ar".

Related

Is it possible to set a single default locale for an app?

I'm making an app which supports only 1 language which is RTL, but my device that i debug with locale is en-us, which makes it different from a user in the locale it's meant to be used in.
Is it possible to set a locale for the app so all the users will see the same thing?
I want to do this because i use some libraries that display strings based on the locale.
You can change your app locale configs using this
If you want to use RTL design and language change Locale instructor parameter to "ar" that means Arabic or "fa" means Farsi or etc.
Locale locale = new Locale("en");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());

How to change android app language without changing phone language?

I want user to select a language inside the app. Once the language is selected, I want the strings to use the particular language.
If I change the phone language, then my app runs on the set language.
I am not able to find any way to set a language without changing the phone language. In addition, the changes should be reflected once the language is set.
Could anyone please suggest a way to do it?
Try this
public static void changeLang(Context context, String lang) {
Locale myLocale = new Locale(lang);
Locale.setDefault(myLocale);
android.content.res.Configuration config = new android.content.res.Configuration();
config.locale = myLocale;
context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
}
Lang parameter should be "en" for English, "it" for Italian... After that you should restart your activity/fragment

Language of the default values/strings.xml

Which language does android treats default strings.xml as?
Specifically, when there are <plurals> tags inside default strings.xml, rules for which language will Android pick? Is there a way to specify that?
It doesn't treat default values folder as a language dependant. It's basically a fallback if it doesn't find a better match. You can read more here.
Android will pick plurals rules for language that is currently selected in the phone(e.g. if users has german language then android will pick german rules).
Use this to change the language by programmatically--
Locale locale = new Locale("en_US");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
context.getApplicationContext().getResources().updateConfiguration(config, null);
Write the countrycode of language in place of "en_US" whatever language you want...like for japanese--"ja_JP" For Arabic--"ar" or check this link for code of country--
http://code.google.com/apis/igoogle/docs/i18n.htmlenter code here

How to load alternative strings.xml from resources programmatically

I want to provide three different langauges. The default language is english so all my string values in the values folder are in enlish. I created two other folders
values-de for the german language
values-cn for the chinese language
each folder contains a strings.xml file where i defined the values in german for the de folder and in chinese for the cn folder.
My question now is: How can i load a different language programmatically because i want to provide buttons in my app interface where the user can switch the language. The settings of the device wont be editable for our users. Our users can just see the app itself and nothing else so i have to provide the language switching from within my application.
You can change the configuration at runtime
Do this in onCreate of the activity
String languageToUse = "de"; // The language you want to change
Locale locale = new Locale(languageToUse);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());

Android : Setting language in app

I want to change my app language in my app because android languages don't have all languages i want to use. So i got this to change:
String languageToLoad = "en";
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
However, when user opens app and change it. Then strings which is showing at that moment still are the older language it changes just when new Activity is created.
Other problem that i should somehow save what user language was select and then change language when app is started.
So how to improve this? I want that when user select language all strings would be taken from selection languages strings.xml and how to save which language user was selected?
You can use a SharedPreference to store the user's language. Here's the documentation about it: http://developer.android.com/reference/android/content/SharedPreferences.html
About the need to reload the Activity, you may find some answers here: Android: locale(system Language) change effect my application layouts
You can create a public class (public class Share) with static members like public static String language = "en"; You can easily access the fields of this class with using the code as Share.language in whole of your project. If you want to change the language you can set Share.language = "fa";. To reload the activity with selected language, you should place the following code before any activitie's super.onCreate(saveInstanceState); :
Locale l = new Locale(Shares.language);
Locale.setDefault(l);
Configuration config = new Configuration();
config.locale = l;
context.getApplicationContext().getResources()
.updateConfiguration(config, null);
Please note that, language can be set, if you have a folder like values-language in your /res folder.
Good luck, Hossein :)

Categories

Resources