changing the language of app without changing the settings of device - android

I am making an android app in which i want to change the language of app on selecting a particular language in spinner without changing the language of device.I have made different string files for all language. Now what to do next??
Can anyone please help me over this?
thanks

Most simple way is just to change VM's locale, like:
Locale locale=new Locale("zh"); //Chinese
Locale.setDefault(locale); //set VM's default locale

Once I wanted to implement a multiple language feature for my application. Where that language was not even supported by android.
I made the appropriate strings for all the application and stored them in my resources.
Then I kept the language selection in my shared preferences, so that when user opens the app the next time, we can show the previously selected language. I implemented the code and language changing in my OnResume() function of Activity. the code was like:
if(SelectedLanguage.compareTo("ar")==0)
{
String text = getString(R.string.ar_Options);
tv_Options.setText(ArabicUtilities.reshape(text));
text = getString(R.string.ar_Minimize);
tv_Minimize.setText(ArabicUtilities.reshape(text));
}
else
{
String text = getString(R.string.en_Options);
tv_Options.setText(text);
text = getString(R.string.en_Minimize);
tv_Minimize.setText(text);
}
I hope you get the basic idea. In this way you don't have to change the lanuguage of your device and you can provide multiple languages for your application.

Related

How to test the android app language changed or not?

In my android app, I have 3 options for language selection.i.e; Hindi,English,French
Programmatically changed default language(English) to Hindi.
How to verify whether app language changed to Hindi or not.(programmatically)
please help
To check whether language changed or not there may be many ways to verify but simply you can assert page title or that drop down text with expected text.
When the language is changed, take the reference of any text or tile or any element placeholder or label, and verify the string by using equalsIgnoreCase("") method.

How to change Locale without re-launching application?

Basically what I am trying to do is,
I start with MainActivity(activity_main) go to SettingActivity(activity_setting) & change my locale from given options(like french, english, dutch etc.)
So what I have done till now is...
OnClick of language name it re-create SettingActivity(activity-setting) & change it's string values according to language selected.
What I really want is Without re-creating Activity, All string values should be applied according to that selected language.
All suggestions & Answers are greatly appreciated.
Thank you in Advance.
Simply change the language property first.
Then call a new function that sets all strings displayed in the UI to the right language.
Pseudocode:
TextView myTextView = (TextView) findViewbyId(R.id.tv1);
myTextView.setText(yourCustomGetLocaleFunction("some id of the string you want to display", "some language name"));
But that's a bad approach. You should let Android handle the language of your app. Just localize the strings.xml file (you can load strings from the file from code).
Edit:
To change the language of your app for the moment, you can use the Code from this solution:
How to change android app language without changing phone language?

How to get locale with region?

I am trying to get the current device locale with the region like "en_us","en_gb".
I am calling Locale.getDefault().getLanguage() and it returns only the two letters code en.
Format like "en_us" or "en_gb" has "language code"_"country code"
A Locale object contains both country code and language code.
So you can use below snippet to format your own code..
String cCode = Locale.getDefault().getCountry();
String lCode = Locale.getDefault().getLanguage();
String code = lCode+"_"+cCode;
or
you can use toString() method on Locale object to get the data
String code = Locale.getDefault().toString();
The default Locale is constructed statically at runtime for your application process from the system property settings, so it will represent the Locale selected on that device when the application was launched. Typically, this is fine, but it does mean that if the user changes their Locale in settings after your application process is running, the value of getDefaultLocale() probably will not be immediately updated.
If you need to trap events like this for some reason in your application, you might instead try obtaining the Locale available from the resource Configuration object, i.e.
Locale current = getResources().getConfiguration().locale;
You may find that this value is updated more quickly after a settings change if that is necessary for your application.
i have tested this :)
i have got from this as link may be deleted so Answer copied :)
It's worth pointing out that locale codes and language tags are related but different. Locale codes have an underscore separator (e.g. fr_CA), and language tags have a dash separator (e.g. fr-ca). I'm sure there are some deeper differences but that's beyond my pay grade.
This answer gives the result of various methods on the Locale class: https://stackoverflow.com/a/23168383
It looks like you want the toString() method (to get the locale code) or the toLanguageTag() (to get the language tag).
Use
Locale.getDisplayName();
This is shorthand for
Locale.getDisplayName(Locale.getDefault());
The documentation is in here:http://developer.android.com/reference/java/util/Locale.html

How can I select the resources of my application prgramatically?

I know if I define two resources layout-en and layout-fr, Android selects the best-matching resources according to the user language selection in his device settings. Now I want to force Android to select a special resource according to my application settings.
For example the default language of my user's device is English and I have 2 options in my application for user to select his application language and assume he selects the french.
How can I force the Android to use fr resources without any changes in user's device settings?
you can set the locale to french. Try something like this
Configuration c = new Configuration(getResources().getConfiguration());
c.locale = yourLocale;
getResources().updateConfiguration(c, getResources().getDisplayMetrics());
try using Locale.setDefault( Locale.FRANCE )

How to change localization in my android app without going to the settings? [duplicate]

This question already has answers here:
Changing Locale within the app itself
(6 answers)
Closed 8 years ago.
Ok i have an application with 2 different languages (english and german), how to change them from my application?
When i click the Language button im using intent to com.android.settings.LocalePicker and from there i select the language.
So instead of that i want to select English and German options from dialog box.
I know how to create the dialog box, but don't know how to change the locale.
Application resources are fetched using the system local which isn't changeable from within an application.
The system settings screen uses a class (ActivityManagerNative) which isn't available via the SDK and thus can not be guaranteed to work between releases, and hence shouldn't be used in your code.
So your options are;
Don't offer the functionality in your app
Implement your own system for determining what setting the user has selected in your app and pulling the appropriate resources using your own code.
try this :
create a normal android dialog with radio button selection of English & German
OnCheckedChange ()
write this to change the Language
Locale myLocale = new Locale(/*String selected*/);
Locale.setDefault(myLocale );
Configuration config2 = new Configuration();
config2.locale = myLocale ;
getBaseContext().getResources().updateConfiguration(config2,
getBaseContext().getResources().getDisplayMetrics());

Categories

Resources