Force app to read from layout-ar - android

I am asking if There is a way to force the android application to read from Arabic layout folders and Arabic values folder whatever the device language ?

You can force a different locale like this:
protected static void setLocale(final Context ctx, final String lang)
{
final Locale loc = new Locale(lang);
Locale.setDefault(loc);
final Configuration cfg = new Configuration();
cfg.locale = loc;
ctx.getResources().updateConfiguration(cfg, null);
}
In your case, use it so
setLocale(getApplicationContext(), "ar")
This will take the values from your "ar" folders (drawables-ar, values-ar, layout-ar, ...)

Related

Changing Locale but keep left-to-right and other phone orientations

I have an app that is available in two languages - English and Hebrew.
I added Hebrew strings using the Translation Editor and I am changing the Locale according to the user selection.
When changing the Locale, it sets the strings to Hebrew like I wanted, but its also changes the toolbar orientation to right-to-left for Hebrew and brings the title and back-button to the right.
English Locale (Default):
Hebrew Locale:
Is there a way to keep the toolbar orientation like the English one?
I want to keep the back button and the title in the left of the toolbar.
Edit: after adding either android:layoutDirection="ltr" or android:supportsRtl="false" to the toolbar xml. arrow is backwards. how ti fix it?
Change Locale but keep left-to-right and other phone orientations
Specifically, add android:supportsRtl="false" to the <application> element in your manifest file.
For more information Link
Add android:layoutDirection="ltr" to your appbar layout. That will force ltr in any layout direction
I had this problem too. I had a method to change the locale of the language and the application configuration :
private String loadPreference() {
SharedPreferences shp = getSharedPreferences("CommonPrefs", Activity.MODE_PRIVATE);
String language = shp.getString("Language","fa");
Locale myLocale = new Locale(language);
Locale.setDefault(myLocale);
Configuration config = new Configuration();
config.locale = myLocale;
getResources().updateConfiguration(config, getResources().getDisplayMetrics());
String locale = getResources().getConfiguration().locale.getDisplayName();
System.out.println(locale);
return locale;
}
It is changing locale and simultaneously changing the layoutDirection, so you can solve this by setting the direction manually:
private String loadPreference() {
SharedPreferences shp = getSharedPreferences("CommonPrefs", Activity.MODE_PRIVATE);
String language = shp.getString("Language","fa");
Locale myLocale = new Locale(language);
Configuration config = new Configuration();
config.setLocale(myLocale);
//manually set layout direction to a LTR location
config.setLayoutDirection(new Locale("en"));
getResources().updateConfiguration(config, getResources().getDisplayMetrics());
String locale = getResources().getConfiguration().locale.getDisplayName();
return locale;
}

Android Gujrati and Hindi Supported Application

I am creating an app which will support Hindi and Gujrati language. I am setting the application language from my app settings screen. like i given a option to user to select a language among English/Hindi/Gujrati.
I am setting Locale on radio button selection basis. I am saving the selection in persistence and on that basis i am changing the typeface of all of textviews in my application.
EVERYTHING IS WORKING FINE.. but it changes the language to english in between the app running. suppose i selected the hindi language from my settings screen and running my app. suddenly after 10-15 min it takes text values from "values" directory, not from "values-hi". I really don't understand why its taking from default values directory. my applications dynamic data is working fine. its coming in hindi and even my app drawables are also working fine but the problem is only that it takes the values from "values" directory.
THIS METHOD IS USED WHEN USER SELECT THE LANGUAGE FROM MY APP SETTINGS SCREEN.
public void setLocale(Context context, String lang) {
Locale myLocale = new Locale(lang);
Resources res = context.getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
}
THIS METHOD IS USED TO SETTYPEFACE OF TEXTVIEW IN ONCREATE METHOD
public static void setTypeface(TextView textView, Context context) {
SharedPreferences sp = context.getSharedPreferences("language_selection", context.MODE_PRIVATE);
String language = sp.getString("language", "English");
if (language != null) {
if (language.equalsIgnoreCase("Hindi")) {
textView.setTypeface(Typeface.createFromAsset(context.getAssets(), "gargi.ttf"));
}
if (language.equalsIgnoreCase("Gujrati")) {
textView.setTypeface(Typeface.createFromAsset(context.getAssets(), "SHRUTI.TTF"));
}
}
}
Try to set your selected Language on this way:
Locale locale = new Locale("YourSelectedLang");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());

change Language in android using Localization

I need to change localization of labels in my app. I have created two string folders for English and Hindi language. When I change Localization it’s working fine and all label show in hindi.
private void setLanguage(Boolean changeLang) {
if (changeLang) {
Locale.setDefault(new Locale("hi", "IN"));
Configuration config = new Configuration();
config = viewDashboard.getBaseContext().getResources().getConfiguration();
config.locale = new Locale("hi", "IN");
viewDashboard
.getBaseContext()
.getResources()
.updateConfiguration(
config,
viewDashboard.getBaseContext().getResources()
.getDisplayMetrics());
} else {
Locale.setDefault(new Locale("en", "EN"));
Configuration config = new Configuration();
config = viewDashboard.getBaseContext().getResources().getConfiguration();
config.locale = new Locale("en", "EN");
viewDashboard
.getBaseContext()
.getResources()
.updateConfiguration(
config,
viewDashboard.getBaseContext().getResources()
.getDisplayMetrics());
}
}
But It also change date picker language to Hindi which I don’t need.
In same way few textviews also show Hindi fonts i.e where I have set date from date picker or show total amount after calculation and Dialog box ‘OK’ and ‘Cancel’ convert into Hindi that I don’t want.
My question is how can I prevent date picker and dialog box button label from converting to Hindi after changing localization programmatically.
I have done it like this i think it will be help full for u.Just pass two parameter to this method one is language and other is activity where you are calling this method.
public static void setLocale(String language, Activity activity)
{
Locale locale= new Locale(language);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
thisActivity = activity;
thisActivity.getBaseContext().getResources().updateConfiguration(config, thisActivity.getBaseContext().getResources().getDisplayMetrics());
}

load resources according to users preference

I wanted to make android app which supports English and Swedish. I have gone through the localization concept. But i wonted to have two buttons on click on English button should load the strings according to English and on click on Swedish button should load the Swedish strings. how can i do this?
Sv=Swedish... en=English...
enter your language code in languageToLoad :
String languageToLoad = "Sv"; // your language
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
this.setContentView(R.layout.main);
Declare two buttons, one for english and another for swedish. your code for english en and for swedish sv and then you have use Localization,this way..
Locale myLocale = new Locale(lang);
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
check this simple example to achieve your goal.
My answer is similar to others but I have added some extras that may be important
Warning
Everything I have read says that its not a good idea to let your app change the language because it isn't supported by the Android framework and it may cause problems.
With that being said, I have done it in my app and, while it has been a bit of a pain, it seems to be working so far. Here is how I did it in case you want to do it this way. You need a separate strings.xml file for each language. strings.xml in values folder as your default then maybe a strings.xml in say a values-es folder for Spanish strings. I have used the following code to change the configuration settings depending on a radio button that the user selects
`
final Configuration LANG_CONFIG = ChooseLocale.this.getResources().getConfiguration();
Locale newLocale = new Locale("English");
curLang = ChooseLocale.this.getLanguage();
if ((curLang.equals("English")) || (curLang.equalsIgnoreCase("Ingles")))
{
newLocale = new Locale("en_US");
}
else
{
newLocale = new Locale("es");
}
Toast.makeText(getApplicationContext(), newLangToast + " " + curLang , Toast.LENGTH_SHORT).show();
Configuration config = getBaseContext().getResources().getConfiguration();
Locale.setDefault(newLocale);
config.locale = newLocale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
SharedPreferences.Editor editor = langPref.edit();
editor.putString(LANG_PREF, curLang);
editor.commit();
`
With this line being the most important to update the config
getBaseContext().getResources().updateConfiguration(config,getBaseContext().getResources().getDisplayMetrics());
I get the local from my getLanguage() function which can be handled however you want. I also had to add
#Override public void onConfigurationChanged(Configuration newConfig) { newConfig = Globals.getUserLanguage(this); super.onConfigurationChanged(newConfig); to every activity that allows orientation change and add this to my onCreate() of each
final SharedPreferences langPref = getSharedPreferences (LANG_PREF, 0); if (Globals.langConfig != null) this.onConfigurationChanged(Globals.langConfig);
I also added android:configChanges="orientation|locale" to each activity in the manifest that allows orientation change. I store the users language preference in a DB that can be changed on the website or within the app. Hope this helps.

How to get string from specific "values" folder?

For example: I have a multi-language app and I want to get some string from random language and user have to guess what language it is.
How to get single string from other language?
The language-specific strings the app uses depends on the system local which is set up in the android system (Link). So Android automatically pics up the correct value. You cannot hardcode which specific (or random) value to be used.
One way to get things done is to change the local setting of android programmatically:
Locale locale = new Locale("fr");
Configuration config = new Configuration();
config.locale = locale;
getResources().updateConfiguration(config, null);
getResources().getString(R.string.local_string);
In this case the only thing to do is to shuffle the local identifier. Of course you should safe an instance of local right before changing it, so that you can reset it to original state. Good luck ;)
edit:
here is some dirty code I wrote to verify:
Locale orgLocal = getResources().getConfiguration().locale;
String[] availLocales = { "fr", "de", "en" };
Locale tempLocale = new Locale(availLocales[new Random().nextInt(availLocales.length)]);
Configuration config = new Configuration();
config.locale = tempLocale;
getResources().updateConfiguration(config, null);
String randLangString = getResources().getString(R.string.local_string);
config.locale = orgLocal;
getResources().updateConfiguration(config, null);
Toast.makeText(this, randLangString, Toast.LENGTH_LONG).show();
First Use this Code to change the Language Setting
Locale locale = new Locale("kn");
Configuration config = new Configuration();
config.locale = locale;
getResources().updateConfiguration(config, null);
getResources().getString(R.string.local_string);
Then For Support all type of language use
Unicode Fonts like-- "ARIALUNI.TTF" Arial Unicode Font(copy into asset folder)
Typeface font= Typeface.createFromAsset(getAssets(), "ARIALUNI.TTF"); TextView tv1.setTypeface(font);
tv1.setText(R.string.txt);
*Take R.string.text value from values-en-for english and values-kn folder for kannada language *
Its quite surprising and belive to that we can only alter configuration by to enable which language we want . Depending on need
Locale locale;
locale = new Locale("fr","FR");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
this.getActivity().getResources().updateConfiguration(config, this.getActivity().getResources().getDisplayMetrics());

Categories

Resources