I use below code to force my textview to show its text numbers with English digits:
txtScale.setText("x" + String.format(Locale.ENGLISH, String.valueOf(scaleValue)));
but it is not working and keep showing the numbers with selected locale. If application locale set to Arabic, it show numbers to Arabic, if set to English it show them English but I want to force to show numbers in English at all states.
For example I want to show below text in Arabic:
۱۲۳۴ //are one two three four
As:
1234
If it helps, I use below code for changing the language of the app manually:
Locale locale = new Locale(CurrentLanguage);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
context.getResources().updateConfiguration(config,
context.getResources().getDisplayMetrics());
Not very sure what you need, but I guess you need this:
txtScale.setText(String.format(Locale.ENGLISH, "x%d", scaleValue));
String.valueOf(scaleValue) there error was here, where you have converted the number based on default Locale
I have had the same problem, the solution was to concatenate the number variable with an empty string.
For example like this :
public void displayPoints(){
TextView scoreA = findViewById(R.id.score_id);
scoreA.setText(""+points);
}
I used this
scoreA.setText(""+points);
instead of this
scoreA.setText(String.format("%d",points));
this will even give you a warning that hardcoded text can not be properly translated to other languages, which exactly what we want here :) .
The problem is also with the locale of the TextView. In API level 17 or after, it is possible to individually set locale of the TextView: http://developer.android.com/reference/android/widget/TextView.html#setTextLocale(java.util.Locale)
I faced a similar problem and it was fixed by using:
public static String nFormate(double d) {
NumberFormat nf = NumberFormat.getInstance(Locale.ENGLISH);
nf.setMaximumFractionDigits(2);
String st= nf.format(d);
return st;
}
The string value could be viewed without problems.
with set font you can achieve this, but to convert non-english digits to English digits I use Long.parseLong("۱۰۰۰") or Integer.parseInt("۱۰۰۰") that the output will be 1000.
But note that Double.parseDouble("۱۰۰۰") will return same ۱۰۰۰
Related
I am trying to create multilangualge app and faced with a problem! I have a string in values\strings.xml translated in German language values-de\strings.xml. I am trying to compare user input with those strings. If my input is in English and device's language is also English, everything works fine, but if I switch device's language to German and input a string in English, contains() and equals() methods will return false. Is there a way to compare strings in different languages? Thanks in advance! Also, sorry for my English!
if (mystring.contains(context.getResources().getString(R.string.testString))) {
check = true;
}
if (mystring.equals(context.getResources().getString(R.string.testString))) {
check = true;
}
In android when you call getResources() it will always get the resources of the default locale, to get one from other locales you must specify explicitly which locale you want to use, and you can find how to do it here :
https://stackoverflow.com/a/33629163/6171845
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.
My problem is that i have a Diary application in which i am adding notes by selecting a particular date from Custom Calendar View.
The problem starts when user changes his phone language from English to any other, If user has selected Japanese as his phone's new language, Month's name would be converted in Japanese language.
Now i want that if user selects any other language other than English than also Month's name should be shown in English only.
I searches a lot but could not find anything helpful.
Any positive response will be highly appreciated.
Thanks.
You should use custom calender and when you display your calendar change Locale(Language) to English programatically.
How to change Locale programatically in android.
Hope it helps you.
Edit :
You can change Language using following code :
public void changeLang(String lang)
{
if (lang.equalsIgnoreCase(""))
return;
myLocale = new Locale(lang);
saveLocale(lang);
Locale.setDefault(myLocale);
android.content.res.Configuration config = new android.content.res.Configuration();
config.locale = myLocale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
updateTexts();
}
Call this method by passing your language Like :
changeLang("en");
Note : You must define your string in res/string.xml file for english language. And you should set your string from String.xml not static.
For example for your concern Your calendar Names are set like...
yourtextview.getResources().getString(R.string.january);
I have a TextView that displays a message to the user depending on the situation. I get the string to change depending on the situation and that's working great.
My problem is, how do I manage that for different languages?
I've looked around and most suggestions are to use a separate string.xml depending on the locale. But since this is only one string resource that changes its content programmatically it did not really work.
Is there a way to have something like
if(language is blabla)
change the text to "this"
or something of the sort.
You can programatically check the user's language by using class:
http://developer.android.com/reference/java/util/Locale.html
i.e.
Locale.getDefault().toString()
Next you should use path to the appropriate string.xml
You only change bold part:
values-en-rMU
I managed this by using SQLite database. I had 5 different tables for 5 languages. In my listAdapter I imported tables name from query:
SELECT * FROM dbname.sqlite_master WHERE type='table'
Next, in one singleton class called OutputStrings I import all used strings for UI.
From each Activity Views are reading strings from OutputStrings.
Best Regards,
You can easily change your local to your desired local:
Locale locale = new Locale("en");
Locale.setDefault(locale);
Configuration mConfig = new Configuration();
mConfig.locale = locale;
getBaseContext().getResources().updateConfiguration(mConfig,
getBaseContext().getResources().getDisplayMetrics());
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.