I am coding a simple java chat app. Is there any possibility of knowing the language user types in order to choose appropriate font?
Something like
Locale locale = InputContext.getLocale();
String language = locale.getLanguage();
But you must rely on the locale setting. I'm here on a German locale, but I'm happily typing English.
Related
When debugging my application, if found that DecimalFormat and DateFormat are set to use nl_NL by default, but the system locale seems to be en_US.
I found the first by evaluating:
new DecimalFormat().symbols.locale
new SimpleDateFormat().locale
And the second:
Locale.getDefault()
System.getProperty("user.country")
System.getProperty("user.language")
What is happening there?
How can I set those formatters to use a certain locale by default? (I'm writing unit tests and would like to set a specific locale without touching the application code)
As of Java 7, the default locale comes in two categories: "DISPLAY" and "FORMAT".
Both are initially set according to the environment. They can be found by calling Locale.getDefault(Category).
Besides the system properties mentioned in the question:
"user.country"
"user.language"
The following can also be present when running the JVM:
"user.country.format"
"user.language.format"
"user.country.display"
"user.language.display"
The reason for that seems to be that some OS's allow independent localization settings for displaying text and for formatting dates, numbers and currency.
Answering my last question, by calling Locale.setDefault(Locale) the default locale will be set for all categories.
I'm developing a project where I need to automatically determinate which is the user language used to created the content.
We're actually using Locale.getDefault().getLanguage() but this can not be the right choice because, for istance, I live in Italy, I write content in Italian but my default Locale on my phone is en_US.
So I tought that maybe I could determinate it from the SIM Country (and fallback to user Locale when the device does not have one) but there's no one-to-one relation from a Country to a Language. For istance, US has en_US and es_US.
Do you have any suggestion? Should I ask the user for their current content's Locale?
What's the reason you need the user language?
I too have my locale set to en_US, and I have that because I prefer to have menus etc in English. (The translation of menus and settings etc feels strained in my native language.)
Chances are that your users will be annoyed (I know I would) if you use anything other than Locale.getDefault().getLanguage() for whatever you're using the language for.
That being said, if you really want to use a local language, I suggest you figure out the country code by some heuristics based on
Default locale
SIM country
Current network country
and use a hard coded lookup table from country code to (one of) the official language code(s). There are open databases with this information, see for example these resources:
List of language codes by country
Country codes / language codes on the OpenStreetMap wiki
I've created an app in two languages. The second one (english), is used when user's default system language is english. If it's not, then the first one is used.
I want to set the second language (that's english) as a DEFAULT language,
which means that when user opens my app and his system language is not the first one, nor English, the English language will appear as a default one.
I tried:
Locale locale = new Locale("en_US");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
context.getApplicationContext.getResources().updateConfiguration(config, null);
But got "context cannot be resolved" error everytime.. Is this piece of code right or..?
Okay,
to make everything clear,
I realized res/values is a DEFAULT directory and the others are just "in case of language". So everything I had to do was to switch the english to /res/values and the other language goes to res/values-es
You should define all languages you support using res folders, i.e res/values, res/values-en, res/values-fr. The system will take care of everything else, you don't need any code.
If you are in an activity you can do:
this.getApplicationContext().getResources().updateConfiguration(config, null);
...to fix your error.
Otherwise you need to pass in the context.
Make sure you add the parenthesis at the end of getApplicationContext(). You didn't do so in your code.
I've been in the same situation, my app was first created in portuguese (BR) so we went global and I had as second language En-Us, so my solution was creating a new language ( clicking in translation Editor + Brazil )... so I have my default language (Portuguese) second (English) third (Portuguese)
then I replaced the resource/ values to the english strings setting as default...
I got one application that can switch language between English and Germany. When in Germany language i want the currency display will auto convert into German format. Therefore in my program i have to do checking for the locale then convert the currency based on the language selected. I choose to use locale.setDefault() but i not sure whether this will has any risk or not based on below statement which i found. Can somebody advise for this?
Statement:
"Since changing the default locale may affect many different areas of functionality, this method should only be used if the caller is prepared to reinitialize locale-sensitive code running within the same Java Virtual Machine."
Thanks.
That warning means that if you've already had code that initialized based on a different locale, then it won't magically hear about the locale change and update. For example, if you already set up your title bar and menus and button labels in English and then call setDefault(Locale.GERMANY), all of the text will still be in English. Your example sounds like you won't be changing the locale after startup, so just make sure that you call setDefault early, before you do anything that depends on the locale.
I have a java application with multi-language support. When I change the language (in a preferences dialog), the language of the entire application changes, including the language of Swing components like JFileChooser. That is working perfect for English, Spanish and French. But when I choose Dutch, the language of Swing components (JFileChooser, confirm dialogs, etc.) changes to English.
Below is the code that changes the language to Dutch. Remark: for the other languages I use the same code (except for the "NL" string, of course) and it works fine.
Locale locale = new Locale("nl");
Locale.setDefault(locale);
JComponent.setDefaultLocale(locale);
I also tried creating the locale using new Locale("nl", "BE"); and new Locale("nl", "NL"); but none of them worked. Is there a problem with the Dutch locale? Or am I doing something wrong here?
As stated here Dutch is not supported for User Interface Translations:
User Interface Translation
Java SE Runtime Environment
The user interface elements provided by the Java SE Runtime Environment 6, include Swing dialogs, messages written by the runtime environment to the standard output and standard error streams, as well as messages produced by the tools provided with the JRE. These user interface elements are localized into the following languages:
Language Locale ID
Chinese (Simplified) zh_CN
Chinese (Traditional) zh_TW
English en
French fr
German de
Italian it
Japanese ja
Korean ko
Portuguese (Brazilian) pt_BR
Spanish es
Swedish sv
Some years to late... But you can also create a wrapper class like this
public class DutchLocale {
static public final Locale NL = new Locale("nl", "NL");
}