I have ran into a little tricky thing here.
The thing is that I want to get the Locale Currency of the User, so I do this:
final Locale currentLocale = getResources().getConfiguration().locale;
final Currency currency = Currency.getInstance(currentLocale);
mCurrencyCode = currency.getCurrencyCode();
Which gives me: AUD as for Australia
The problem? Well, I am in New Zealand and the Currency Code is NZD
How could I get the right Currency Code?
Cheers
Related
I was struggling with date formatting in Kotlin.
Does someone know why using :
val locale = ConfigurationCompat.getLocales(Resources.getSystem().configuration).get(0)
java.text.DateFormat.getDateInstance(java.text.DateFormat.SHORT, locale).format(Date())
give me :
In FR_fr = 08/09/2022 (expected)
In EN_gb = 08/09/2022 (unexpected)
BUT
val currentLanguage = ConfigurationCompat.getLocales(Resources.getSystem().configuration).get(0).language
val locale = Locale(currentLanguage)
java.text.DateFormat.getDateInstance(java.text.DateFormat.SHORT, locale).format(Date())
gives me :
In FR_fr = 08/09/2022 (expected)
In EN_gb = 9/8/2022 (expected)
Is there a simpler way?
In the second one, while you are doing this,
ConfigurationCompat.getLocales(Resources.getSystem().configuration).get(0).language
it will give language as en.It will not give country variant english.It will return generic english locale.
So you should create locale like below.
val currentLanguage = ConfigurationCompat.getLocales(Resources.getSystem().configuration).get(0)
if (currentLanguage!=null) {
val currentLocale = Locale(currentLanguage.language, currentLanguage.country, currentLanguage.variant)
}
THis will create locale with en-GB in your case.
In the first one you are already getting locale object with en-GB.
en-GB and en has different formats of date.
08/09/2022 : en-GB
9/8/2022 : en
en-IN (Indian English) also will give 08/09/2022 as result.
The reason for the different results is because
in you first example, you work with actual en_GB/fr_FR locale,
but in the second example, your locale is only en/ fr.
That's because language/getLanguage() in this instance returns only en/ fr, without country specification.
So, I'd say, the first result (08/09/2022) is the correct one for en_GB. If you want a different date format you probably have to create your own one.
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 ۱۰۰۰
I saw in the forum that others have the same problem, but no one explains how he solved.
I have a TextView in my Activity, with a string in currency format
For example, if the user has the phone set to U.S. English language, and yet want to display currency in € or more, how can I do?
Double value_tot = Double.valueOf(totale);
NumberFormat formatter = NumberFormat.getCurrencyInstance();
String super_totale = formatter.format(value_tot );
aTotale.setText(super_totale);
You can configure the currency to use in the NumberFormat class, like this:
NumberFormat nf = NumberFormat.getCurrencyInstance();
nf.setCurrency(Currency.getInstance("EUR"));
Then nf.format(123.5) will produce "€123.50"
What I want do do is to provide a ISO 3166-1 country code and retrive the name of that country in the current locale. For those of you who are familiar with iPhone, I have an an example of exactly what I want to do:
NSLocale* currentLocale = [NSLocale currentLocale];
NSString* countryName = [currentLocale displayNameForKey:NSLocaleCountryCode value:#"NO"];
In this case the variable countryName would contain "Norway" given the iPhone was running in an english locale.
What I have understood so far is that to get the current locale in the Android SDK by a simple static method of the Locale class.
Locale currentLocale = Locale.getDefault();
But Im stuck here...
Locale l = new Locale("en", "NO");
String norway = l.getDisplayCountry();
works for me. Just replace "NO" by the country you want and it should give you the name in your current default locale. The "en" is just there to fill in some language but it should not matter which you use (At least I hope that works for all combinations - have not tested them all)
In my app there are a lot of textviews which contain a currency symbol.
Now I want the user to set the symbol. I created a sharedpreference and added there all the existing currencies. Unfortunatelly there are approximately 20 different currencies out there.
Therefor I am struggling how to change dynamically the currency symbol in all my textviews.
I could create for each a big switch case statement (with 20 cases) but this will blow up my code extremly.
Is there another technique how I can change the symbol. E.g. with help of an xml file like the different languages...
The efficient way to do this is by using the Locale and Currency classes.
1.Create the Locale object based on the user input
Example:
Locale locale=new Locale("en", "US");
2.based on this get the currency symbol and display it.
Example:
Currency currency=Currency.getInstance(locale);
String symbol = currency.getSymbol();
I think you will need a formatted string from an xml, something like that from the documentation :
<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>
Then in your activity :
Resources res = getResources();
String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);