Changing strings.xml on the fly - android

i am trying to post my question understable.
I have 2 strings.xml one is for english and another is for Indian Local Language, let it be Tamil.
I have kept the meanings of all the attributes of strings.xml(english) in my tamil strings.xml
Initially my application will load english strings.xml (as normal)
I have button somewhere which should be used to change the language (tamil in this case)
Upon clicking that - my whole app should be reading my tamil strings.xml ..
I know below code only works for default LOCALE
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = Locale.GERMANY;
res.updateConfiguration(conf, dm);
Can this be tweaked to read the my customized strings.xml on the fly?
Any ideas are greatly appreciated.

That is not possible. It's only possible to change the language of your phone and than you will change the language for your app.
why don't you make a settings activity and in there you put an option to change the language that will transfer you user to a locale and text in the settings page on phone?
Tell me if you need a code or something like that.

Do you mean you want to change your language on demand from English to Tamil within your app?
I call this method in my activity onCreate:
public void setupLocale(Activity c, String NewLocale) {
Resources res = c.getBaseContext().getResources();
Configuration newConfig = new Configuration(res.getConfiguration());
newConfig.locale = new Locale(NewLocale);
res.updateConfiguration(newConfig, null);
}
Where c is my activity and NewLocale is the locale string I want to use. e.g "fr" for french, "da" for Danish. Don't know what Tamil is but you probably do.

Related

Android Multi Language Support

In my android application, I've to provide two language versions (GERMAN,ENGLISH) of the application.In the German Version ,Some extra categories(extra screens) are available compared to English Version.What are the best practices,I should follow?
You have to make two folder in the res directory.
values (This is the by default, put English Language here.)
values-in(This is for Indonesia language), similarlyyou can find for other languages too.
In both the folder, you will having one file named strings.xml.
Now put all your strings in this two files, with the same refrence name.
Like below.
in values-in/strings.xml
<string name="date">Tanggal</string>
in values/strings.xml
<string name="date">Date</string>
and use this method in your MainActivity Class.
public void changeLanguage(String lang) {
Locale myLocale;
if (lang.equalsIgnoreCase(""))
return;
myLocale = new Locale(lang);
Locale.setDefault(myLocale);
android.content.res.Configuration config = new android.content.res.Configuration();
config.locale = myLocale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
}
You can get the user language using -
Locale.getDefault().getDisplayLanguage();
and then maybe using if else and a boolean data type you may program your system accordingly.
Or you may want the user to select his or her language themselves using RadioButtons.
Use #strings for the Text Content as then you can translate them easily using coding
You need to create string.xml file for both GERMAN,ENGLISH language with same key with different language text.
You will have to create two folder's in your resource directory named as 'values' and 'values-de' for English and German Language respectively. Also, you can design layouts based on device language. for ex. layout folder for English and layout-de folder for the German Language. Same way to set the font sizes of TextViews you can create a file named as dimen.xml in values folder for respective languages.
// I added this code in every activity and it worked and ofcoarse u need to add a translation string first
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_log_in)
changeLang = findViewById(R.id.changeLang)
changeLang.setOnClickListener {
baseContext.setAppLocale("am")
recreate() }
}
private fun Context.setAppLocale(language: String): Context {
val locale = Locale(language)
Locale.setDefault(locale)
val config = resources.configuration
config.setLocale(locale)
config.setLayoutDirection(locale)
return createConfigurationContext(config)
}

Xamarin Android change languagee folder on button click

I'm working on a app in Xamarin.Android and it has to support English and Bulgarian. Most of the text is stored in the Strings.xml in the values folder.
The English text is in the default values folder and the Bulgarian text is in values-bg.
The filename for both is String.xml, and the name of each string is the same as it's translated version.
Now how can I make it so that when i click one button it reads the strings from values-bg and when i click another it reads from the default again? Also I would like that to be so for every activity in the app.
I've been testing how to change locale programmatically, and I finally found a solution that worked for me.
As you said, you need a values folder, containing strings.xml with default values, which would be english, then a values-bg folder, with the same file but with Bulgarian translated values. It is important that strings have the same name, so you did it right.
Then, in your click event, you can add this :
var locale = new Java.Util.Locale ("bg");
Android.Content.Res.Configuration conf = Resources.Configuration;
conf.Locale = locale;
DisplayMetrics dm = Resources.DisplayMetrics;
Resources.UpdateConfiguration (conf, dm);
Recreate ();
First, that code will declare a Locale variable for "bg" locale. Then, it gets the actual configuration of the device, and apply the new Locale to it. Using UpdateConfiguration, it save the changes. The Recreate is optional, it just, as its name explains it all, recreates the current activity, which will be loaded using the new configuration, with a locale "bg" instead of whatever default locale was applied. It will now fetch strings in the values-bg folder.
NOTE : I've read in the comments of the accepted answer here that on some devices, the configuration will reset to default after some time in the app. I'm not sure why this happens, but you might want to check if the locale has been changed somehow when loading a new activity, to prevent that rollback.

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());

Can you programmatically switch between xml resource files on android?

Android has built in functionality to switch between resources based on a users device language (http://developer.android.com/training/basics/supporting-devices/languages.html), but is it possible to switch the resources manually?
For example if I have:
yProject/
res/
values/
strings.xml
values-es/
strings.xml
values-fr/
strings.xml
Can I change which string file is used based on the users preference rather than their device language? So someone using a french language device can choose to use the English text if they want?
I know I can do it with string variables in my code rather than using the xml, but I feel the xml would be neater.
Yes you can. This is a copy/paste from a project doing so, based on user preference, to be put in onCreate():
Resources res = getResources();
Configuration conf = res.getConfiguration();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
String def = Locale.getDefault().getDisplayLanguage();
String lang = prefs.getString("LANGUAGE", def);
conf.locale = new Locale(lang);
Log.v("myapp", lang+" = "+conf.locale+" = "+conf.locale.getDisplayName());
res.updateConfiguration(conf, res.getDisplayMetrics());
By setting a user preference named LANGUAGE to the two-letter code of the desired language, and then restarting the Activity, you manually override set the language. By removing the preference you get the system default.

How can I change language of my application [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Change language programatically in Android
I am new to Android. In my application user can select a language from three languages. Based on the language selected by user, the entire application's language should be change. How can I do this?
Use this to change the language 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 country code of the language in place of "en_US" for whatever language you want. For example, for Japanese, ja_JP; for Arabic, ar. Check this link for a list.
And make a folder in res/values-ja for Japanese or res/values-ar for Arabic..
And make a string.xml file, and put whatever languages you want on your layout. It will fetch the default language from values folder otherwise if you want it manually, then it will fetch from your external folder values-ar, etc.
An example of res/values-ar for Arabic:
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<string name="label">حسب</string>
<string name="name">بحث</string>
<string name="search">بحث :</string>
</resource>
You can set the locale.
Resources res = context.getResources();
// Change locale settings in the app.
DisplayMetrics dm = res.getDisplayMetrics();
android.content.res.Configuration conf = res.getConfiguration();
conf.locale = new Locale(language_code.toLowerCase());
res.updateConfiguration(conf, dm);
If you have language specific content - you can change that base on the setting.
for more detail you can see Locale and this also

Categories

Resources