Not able to set browser locale to java ResourceBundle - java

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.

Related

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

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

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

different formatting for EURO in JODA and JAVA

The below code (which is in JODA) prints: €12,23
String formatAmount = new MoneyFormatterBuilder().
appendCurrencySymbolLocalized().
appendAmountLocalized().
toFormatter().
withLocale(new Locale("es", "ES")).
print(Money.of(CurrencyUnit.EUR, 12.23));
System.out.print(formatAmount);
The below code prints: 12,23 €
String formatAmount = NumberFormat.
getCurrencyInstance(new Locale("es", "ES")).
format(amount);
System.out.print(formatAmount);
Can someone tell me which one is correct and why both the libraries print differently?

What standard is the Currency formatting of NumberFormat.getCurrencyInstance() based on?

I need to format currency correctly based on locale, but our customer disagrees with the default formatting that Java does based on some locales.
What standard is the formatting based on that Java uses for the NumberFormat returned by NumberFormat.getCurrencyInstance(), if any, and is this documented anywhere?
Below is some example code that shows how Java formats currency based on Locale.
public static void main(String[] args)
{
displayCurrency(Locale.ENGLISH);
displayCurrency(Locale.FRENCH);
displayCurrency(Locale.ITALIAN);
}
static public void displayCurrency( Locale currentLocale) {
Double currencyAmount = new Double(9876543.21);
DecimalFormatSymbols symbol = new DecimalFormatSymbols(currentLocale);
symbol.setCurrencySymbol("$");
DecimalFormat currencyFormatter = (DecimalFormat)
NumberFormat.getCurrencyInstance(currentLocale);
currencyFormatter.setDecimalFormatSymbols(symbol);
System.out.println(
currentLocale.getDisplayName() + ": " +
currencyFormatter.format(currencyAmount));
}
Output:
English: $9,876,543.21
Italian: $ 9.876.543,21
French: 9 876 543,21 $
I18n in Java is defined by the UNICODE standard, so you can find the information about how to format currencies at http://www.unicode.org/cldr/charts/latest/supplemental/detailed_territory_currency_information.html
In the source code, the same information is stored in the JDK rt.jar and localedata.jar files, in packages sun.util.resources and sun.text.resources (files are called CurrencyNames_xx_XX.class)
Probably it takes de default curreyncy from the JVM you are using. there is a file currency.data in the lib folder of the jre

How to get Chinese locale to display in chinese as specifically simplified or traditional

I'm generating a list of locales and printing out their local display languages (i.e. printing out ja_JP as 日本語) using java.util.Locale. I noticed that both zh_CN (chinese simplified) and zh_TW (chinese traditional) localize as 中文 rather than 简体中文 and 繁体中文. Is there a way to get these locales to include the prefix characters for simplified and traditional without hard-coding that zh_CN should be 简体中文 and zh_TW should be 繁体中文? I know I could print out language + country (i.e. 中文 (中国), but that's not quite the same.
Here's a java snippet demonstrating that they're the same:
import java.util.Locale;
public final class test {
public static void main(String[] args) {
Locale locale1 = new Locale("zh", "cn");
System.out.println( locale1.getDisplayLanguage(locale1));
System.out.println( locale1.getDisplayLanguage(Locale.TRADITIONAL_CHINESE));
System.out.println( locale1.getDisplayLanguage(Locale.SIMPLIFIED_CHINESE));
System.out.println( locale1.getDisplayCountry(locale1));
System.out.println( "");
Locale locale2 = new Locale("zh", "tw");
System.out.println( locale2.getDisplayLanguage(locale2));
System.out.println( locale2.getDisplayLanguage(Locale.TRADITIONAL_CHINESE));
System.out.println( locale2.getDisplayLanguage(Locale.SIMPLIFIED_CHINESE));
System.out.println( locale2.getDisplayCountry(locale2));
}
}
Instantiating the Locale Objects in the following way should resolve your issue:
Locale locale1 = new Locale("zh", "CN");
Locale locale2 = new Locale("zh", "TW");

Categories

Resources