How can i get the language of locale? [duplicate] - java

This question already has answers here:
Get the current language in device
(30 answers)
Closed 3 years ago.
Ask the title, how can i get the languge from locale in android? I used countrycodepicker to get country code and used locale.getLanguage to get language of that country. But when i choose usa, france, british, ... it get wrong language such as USA - ikt; France - gsw?
String countryCode = ccp.getSelectedCountryNameCode();
String languageCode = null;
Locale[] all = Locale.getAvailableLocales();
for (Locale locale : all) {
String country = locale.getCountry();
if(country.equalsIgnoreCase(countryCode)){
languageCode = locale.getLanguage();
}
}

To get the current Locale language, Please use the below method:
Locale.getDefault().getLanguage()

Related

How to use string format %s and '%' in the same line? [duplicate]

This question already has answers here:
How to escape % in String.Format?
(3 answers)
Closed 4 months ago.
Here is simple string format and it works.
String format = "Your name is %s";
String text = String.format(format, "Harry");
System.out.print(text);//print "Your name is Harry"
But when I use "%" in the same line
String format = "Your name is %s, 100%point";
String name = String.format(format, "Harry");
System.out.print(name);// I want to print "Your name is Harry, 100%point"
I want to print "Your name is Harry, 100%point",
but it come into runtime error
java.util.UnknownFormatConversionException: Conversion = 'p'
at java.util.Formatter$FormatSpecifier.conversion(Formatter.java:2781)
at java.util.Formatter$FormatSpecifier.<init>(Formatter.java:2811)
at java.util.Formatter$FormatSpecifierParser.<init>(Formatter.java:2624)
at java.util.Formatter.parse(Formatter.java:2557)
at java.util.Formatter.format(Formatter.java:2504)
at java.util.Formatter.format(Formatter.java:2458)
at java.lang.String.format(String.java:2842)
How to solve it.
Any help will be appreciate.
You need to escape % with another %, i.e. %%.
String format = "Your name is %s, 100%%point";
String name = String.format(format, "Harry");
System.out.print(name); // I want to print "Your name is Harry, 100%point"

Formatting number based on the different currency code for different locale where output to be in the currency symbol formatted based on the locale

Language: Java
Problem: I need to set the currency code manually in java. Let's say "USD" and locale can be either "fr-CA" or "en_US" based on the user logged in . I am unable to find the solution where we can do the number format by setting the manual currency and displaying the symbol with number in the output. Please note currency code will not be the same as the locale and vice versa.
For example, if my currency is USD then based on the different locale, the number should be formatted and the output should be as below.
$1,300,000.00 - english
1.300.000,00 $ - Deutch
1 300 000,00 US$ - Potuguese
1 300 000,00 $ US - France canada
Tried below but it does not give the expected output:
Currency currencyInstance1 = Currency.getInstance("USD"); // This can change based on the user input on the UI.
NumberFormat numberFormat4 = NumberFormat.getCurrencyInstance(Locale.CANADA_FRENCH);
numberFormat4.setCurrency(currencyInstance1);
System.out.println(numberFormat4.format(amount4));
Actual output : 123 456,79 USD
**Expected output:**
For french canada: 1 300 000,00 $ US
For Portuguese : 1 300 000,00 US$
For Deutch : 1.300.000,00 $
Any help is appreciated.
Cross currency for cross locale is not supported in Java.
Country codes are an important locale component because of java. text.Format objects for dates, time, numbers, and currency are particularly sensitive to this element. Country codes add precision to the language component of a locale. For example, French is used in both France and Canada. However, precise usage and idiomatic expressions vary in the two countries.
These differences can be captured with different locale designators in which only the country code is different. For example, the code fr_CA (French-speaking Canada) is different from fr_FR (French-speaking France).
So if we need to fetch the symbol then we would need to create a map with the locale and currency. Pass the currency to fetch the symbol and then use replace to add it.
public static Map<Currency, Locale> currencyLocaleMap;
static {
currencyLocaleMap = new HashMap<>();
List<Locale> availableLocales =
Arrays.asList(Locale.getAvailableLocales());
List<Locale> supportedLocales = new ArrayList<>();
supportedLocales.add(Locale.forLanguageTag("en-US"));
List<Locale> filteredLocales = supportedLocales.stream().filter
(eachLocale -> availableLocales.contains(eachLocale)).collect(Collectors.toList());
System.out.println("UtilTemp : Locales supported : " + filteredLocales);
for (Locale locale : filteredLocales) {
try {
if(!locale.getCountry().isEmpty()){
Currency currency = Currency.getInstance(locale);
currencyLocaleMap.put(currency, locale);
}
}
catch (Exception e) {
}
}
}
public static String getCurrencySymbol(String currencyCode) {
Currency currency = Currency.getInstance(currencyCode);
System.out.println("UtilTemp :" + currencyLocaleMap);
return currency.getSymbol(currencyLocaleMap.get("USD"));
}

Android format an international phone number string with its country code included

Let's say I have this String:
String phoneNumber = "+15611234567"
Using PhoneNumberUtils.formatNumber(phoneNumber); works here and formats it into +1-561-123-4567
However, when I have a non-US phone number with its country code included, such as
String phoneNumber = "+96170123456" //Lebanese phone number
It doesn't work, it just returns it as-is instead of it being formatted.
Returns: +96170123456
Expected: +961 81-932-452 or any kind of different formatting (e.g +961 70 123 456)
In case you need it, here's the code that's not working:
public void setPhone(final String phone){
TextView phoneTV = view.findViewById(R.id.phone);
String formattedNumber = PhoneNumberUtils.formatNumber(phone);
phoneTV.setText(formattedNumber);
}
The phone number's country code isn't consistent, it could be from any country.
formatNumber (String phoneNo) This method was deprecated in API level 21.
So for API level 21 or above use :
formatNumber(String phoneNumber, String defaultCountryIso)

Not able to set browser locale to java ResourceBundle

I am writing website in 3 languages i.e. English, Arabic and Persian. I am trying to show the user this website in relative language. For example if the browser language is Arabic then website will be displayed in Arabic and if its is Persian then Persian website will be display
Here what I did so far:
private Locale locale(){
Locale locale=null;
if(country == null){
locale = request.getLocale();
if(locale.equals("ar")){
locale = new Locale("ar", "AE");
}
else if(locale.equals("fa")){
locale = new Locale("fa", "IR");
}
else if(locale.equals("en")){
language = "en";
locale = new Locale("en", "US");
}
}else{
locale = new Locale(language, country);
}
System.out.println("return---locale=>"+locale);
return locale;
}
This method printing locale=>ar
ResourceBundle rb = ResourceBundle.getBundle("LabelBundles.Labels",locale());
if(country == null){} block is not working whereas else{
locale = new Locale(language, country);
}
is working fine.
2) locale = request.getLocale(); output is "ar" whilst locale = new Locale(); requires two values that is language_COUNTRY so why its giving output as "ar" only without country?
Please advise and thanks in anticipation
request.getLocale() returns a Locale, not a String. So
locale.equals("ar")
will always be false: only a String can be equal to a String. And only a Locale can be equal to a Locale.
You can however do
if (locale.getLanguage().equals("ar"))
Regarding your second question, it's of course answered by the javadoc. If you read it, you'll learn that a locale is a language, + an optional country, + an optional variant. And there is a constructor taking only a language as argument.
Read the javadoc. That's how you learn.

What is the correct way to get Currency Symbol in Android? [duplicate]

This question already has answers here:
How to get currency symbol by currency name?
(6 answers)
Closed 6 years ago.
This code is working fine but if I change my device langauge this is also show Rs so what the correct way to get currency symbol ?
public void displayTotlaPrice() {
TextView totalPriceTextView = (TextView) findViewById(R.id.total_Price);
totalPriceTextView.setText("Rs" + displayCalculatePrice());
}
Locale locale = Locale.getDefault();
Currency currency = Currency.getInstance(locale);
String symbol = currency.getSymbol().replaceAll("\\w", "");
try this, Hope it works.
yourString= NumberFormat.getCurrencyInstance().format(yourNumber);
or
NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance();
String cur = currencyFormatter.format(yourValue);
for specific
yourString= NumberFormat.getCurrencyInstance(new Locale("en", "AU")).format(yourNumber);
number will format based on device language.
Detail:
How to get currency symbol by currency name?
Currency
Store below variable in string.xml
<string name="Rs">\u20B9</string>
Now call it as below wherever you want to display Rs symbol,
textView.setText(getResources().getString(R.string.Rs) + "500");
you can use,
Locale swedishLocale = new Locale("sv", "SE");
Currency currency = Currency.getInstance(swedishLocale);
System.out.println("Symbol: " + currency.getSymbol());
for more details, visit display the currency for a locale

Categories

Resources