Can I set the string resource to a different language programatically? - android

I have a arabic strings.xml file. I also have an English one.
In my settings activity, I want to be able to change the language used by the user. Like, is there a method like setStringResource(string.xml-ar)?

duplication of Load language specific string from resource?
check this one out.
Of course you must use Locale.AR or something like that for Arabic resource file.

Keep your language specific xml strings in the necessary directories under resources
And change the locale as required
String language = "en";
String country = "US";
Locale locale = new Locale(language , country);

Related

Xamarin Android Localization not changing

I have created a folder values-fr for French localization and placed String.xml with french strings in it. When I change the language in my testing device to french it's not changing. PS: Xamarin Android.
Just to post the answer from the comments.
Try this to get the Locale language (just to check), put it in to OnCreate() method:
Android.Content.Res.Configuration conf = res.Configuration;
var languageCodeValue = conf.Locale;
if the language it's ok, then check the folder name, you can see more information here https://learn.microsoft.com/es-es/xamarin/android/app-fundamentals/localization
Then check the name of the file, Xaml can't recognize the UpperCase, so you need to define all in minus, check if you have named the file strings.xml

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

localizing application non default strings?

Hi I am trying to localize my android application and I read up on how by reading this tutorial http://developer.android.com/guide/topics/resources/localization.html but after looking through it, I couldn't figure out how to change the language. I have the default values in res/values/strings.xml and I have my other strings in res/values-latin/strings.xml. but I cant figure out how to make my application use the strings in res/values-latin/strings.xml instead of the default. Could someone explain this to me?
How about if someone clicks a change language button how could I inform the application to change all the strings?
Thank you!
Android will do it for you automatically.
When a user runs your application:
The Android system selects which resources to load, based on the
device's locale.
Example:
Suppose that your application's default language is English. Suppose also that you want to localize all the text in your application to French. In this case, you could create two alternative strings.xml files, each stored in a locale-specific resource directory:
res/values/strings.xml
Contains English text for all the strings that the application uses.
res/values-fr/strings.xml
Contain French text for all the strings.
If your Java code refers to R.string.title, here is what will happen at runtime:
If the device is set to any language other than French, Android will load English title from the res/values/strings.xml file.
If the device is set to French, Android will load French title from the res/values-fr/strings.xml file.
With latin it will not work. You should search alternative method.
My solution in java:
public final class English {
String moon = "Moon";
String earth = "Earth";
String mars = "Mars";
}
public final class Latin {
String moon = "Luna";
String earth = "Terrae";
String mars = "Mars in Latin";
}
Then in your code you can aceess them:
TextView textView = (TextView) findViewById(R.id.my_text);
textView.setText( Latin.moon );

How to enter RTL string for Android TextView

I have an Android application which I wrote for english language now I want to convert it to farsi/persian language. I want to know how can i type persian text for the TextView text.How can i maintain both the english and persian String.xml.please help.
cheers
Zolf
Locale locale = new Locale("fa-IR");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getApplicationContext().getResources().updateConfiguration(config, null);
where do i put this code,now i put this code in the OnCreate and it does not change the text of the TextView to persian
For showing correct form of Persian characters use this solution.
Update
And for changing current resources that is loaded in your UI:
tf = Farsi.GetFarsiFont(this);
tvTitle01 = (TextView) findViewById(R.id.tvTitle01);
tvTitle01.setTypeface(tf);
tvTitle01.setText(Farsi.Convert(tvTitle01.getText().toString()));
Android has built-in mechanism of localization.
http://developer.android.com/guide/topics/resources/localization.html
you can create the different folder of values as per language and can keep different string.xml file for each language. Example:
res/values/strings.xml
Contains English text for all the strings that the application uses, including text for a string named title.
res/values-fr/strings.xml
Contain French text for all the strings, including title.
res/values-ja/strings.xml
Contain Japanese text for all the strings except title.
If your Java code refers to R.string.title, here is what will happen at runtime:
If the device is set to any language other than French, Android will load title from the res/values/strings.xml file.
If the device is set to French, Android will load title from the res/values-fr/strings.xml file.

Get language in android?

In my android app I need to know which is the language of the phone in this format :fr (for francais), en (for english),etc.. How can I do this? I know that I can display language with this :
Locale.getDefault().getDisplayLanguage()
but the result is francais,english,...
Can I obtain somehow the initials of languages?
Locale.getDefault().getLanguage()
Returns the language code for this Locale or the empty string if no language was set.
http://developer.android.com/reference/java/util/Locale.html#getLanguage()
How about using
Locale.getDefault().getLanguage();
I think you can try one of these two.
Locale.getDefault().getLanguage();
This will give language iso code i.e. "de", "ru". OR
Resources.getSystem().getConfiguration().locale;
This returns global shared Resources object that provides access to only system resources.

Categories

Resources