How to enter RTL string for Android TextView - android

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.

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

Android. How change default locale fo string.xml in values

I have an application with only russian locale. If I am not mistaken, the string.xml in the res/values is the english locale by default. But english and russian have different plurals. For example:
In russian:
1,21,31..x1 книга
2-4, 2(2-4), 3(2-4), .., x(2-4) книги
in other cases - книг
In english:
1 book
n books
Problems begin when the user changes system language from russian to other language.
How can I change default language for my application? Or maybe it is possible to force the application to use the russian plurals?
I finally found the answer:
https://developer.android.com/studio/write/tool-attributes#toolslocale
<resources xmlns:tools="http://schemas.android.com/tools"
tools:locale="es">
From the doc:
This tells the tools what the default language/locale is for the resources in the given element (because the tools otherwise assume English) in order to avoid warnings from the spell checker. The value must be a valid locale qualifier.
you can create different String.xml files
like this
values/
strings.xml
values-es/
strings.xml
values-fr/
strings.xml
Check google official document https://developer.android.com/training/basics/supporting-devices/languages.html
& https://developer.android.com/guide/topics/resources/localization.html
create new value folder values-ru and place string.xml in that.
in java change your app language programatically with this code. for change to rushion call method with ru parameter like this changeLanguage("ru").
public void changeLanguage(String languageToLoad) {
//for language change
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getResources().updateConfiguration(config, getResources().getDisplayMetrics());
}

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

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

Is possible to set only default English language of application in android

I only want set only default English language of application in android.
After Supporting Different Languages, I also use values/strings.xml to store characters in that file.
If in English language mode of System Settings, I can show correct words.
If in Chinese language mode of System Settings, The English words was translated to Chinese.
I don't want it translate automatically.
People who know how to set only default English language,
Please help me.
Thanks,
I can get correct answer now via the codes in below :
public static void setDefaultLanguage(Context context, String lang) {
Locale locale = new Locale(lang);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
context.getResources().updateConfiguration(config,
context.getResources().getDisplayMetrics());
}
In that, what is lang value? You should get correct Language System to pass this value to lang parameter.
Thanks,
Are you storing all the strings for both languages in one .xml? If so that is probably your issue…
res/values/strings.xml
Contains English text for all the strings that the application uses,
res/values-ja/strings.xml - for japanese, but I am sure you can find one for the language you are looking for! Android looks for the "values-ja" tag.

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

Categories

Resources