Both sides previous revisionPrevious revisionNext revision | Previous revision |
student:java:localization [2018/08/16 10:46] – bernstdh | student:java:localization [2022/05/03 14:18] (current) – bernstdh |
---|
| |
| |
Java has a [[ http://docs.oracle.com/javase/9/docs/api/java/util/Locale.html | java.util.Locale | ]] class that encapsulates the identifying attributes of a locale. These attributes include a //language// (defined in [[ http://www.loc.gov/standards/iso639-2/php/code_list.php | ISO 639 ]]), a //script// or writing system (defined in [[ http://unicode.org/iso15924/iso15924-codes.html | ISO 15924 ]]), a //country// or region (defined in [[ https://www.iso.org/iso-3166-country-codes.html | ISO 3166 ]]), and a //variant// (defined in [[ https://tools.ietf.org/html/bcp47 | IETF BCP47 ]]) like Canadian French or Swiss German. | Java has a [[ http://docs.oracle.com/javase/9/docs/api/java/util/Locale.html | java.util.Locale ]] class that encapsulates the identifying attributes of a locale. These attributes include a //language// (defined in [[ http://www.loc.gov/standards/iso639-2/php/code_list.php | ISO 639 ]]), a //script// or writing system (defined in [[ http://unicode.org/iso15924/iso15924-codes.html | ISO 15924 ]]), a //country// or region (defined in [[ https://www.iso.org/iso-3166-country-codes.html | ISO 3166 ]]), and a //variant// (defined in [[ https://tools.ietf.org/html/bcp47 | IETF BCP47 ]]) like Canadian French or Swiss German. |
| |
| |
When the Java Virtual Machine is started it determines the ''%%Locale%%'' of the processor it is running on. This information can be obtained from within a program using the [[ http://docs.oracle.com/javase/9/docs/api/java/util/Locale.html#getDefault() | java.util.Locale#getDefault() | ]] method. | When the Java Virtual Machine is started it determines the ''%%Locale%%'' of the processor it is running on. This information can be obtained from within a program using the [[ http://docs.oracle.com/javase/9/docs/api/java/util/Locale.html#getDefault() | java.util.Locale#getDefault() ]] method. |
| |
| |
| |
| |
The Java Virtual Machine can be provided with the information is should use to identify the ''%%Locale%%'' when it is started using the ''%%-Duser.country%%'' and ''%%-Duser.language%%'' options. For example, when executing an application named ''%%Test%%'' in the United States one could tell the JVM to use the ''%%Locale%%'' for Canadian French as follows: | The Java Virtual Machine can be provided with the information is should use to identify the ''%%Locale%%'' when it is started using the ''%%-Duser.country%%'' and ''%%-Duser.language%%'' options. For example, when executing an application named ''%%Test%%'' in the United States one could tell the JVM to use the ''%%Locale%%'' for Canadian French using the following virtual machine (VM) options: |
| |
<code> | <code> |
java -Duser.country=CA -Duser.language=fr Test | -Duser.country=CA -Duser.language=fr |
</code> | </code> |
| |
It is also possible to change the ''%%Locale%%'' inside of a program using the [[ http://docs.oracle.com/javase/9/docs/api/java/util/Locale.html#setDefault(java.util.Locale) | java.util.Locale#setDefault(java.util.Locale) | ]] method. | They can be passed to the VM from the command line when executing the ''%%java%%'' command. (Note: When using PowerShell, the arguments must be enclosed in double-quotes.) All IDEs also have a way of passing options to the VM (e.g., in Eclipse as part of the "VM arguments" in the"Arguments" tab of a "Run Configuration"; in jGRASP as "RUN Arguments"). |
| |
| |
| It is also possible to change the ''%%Locale%%'' inside of a program using the [[ http://docs.oracle.com/javase/9/docs/api/java/util/Locale.html#setDefault(java.util.Locale) | java.util.Locale#setDefault(java.util.Locale) ]] method. |
| |
| |
| |
| |
Some people/companies approach the localization of terminology as a problem in machine translation but most use a simple mapping (and rely on human translators to do the hard work of translation). Java provides a [[ http://docs.oracle.com/javase/9/docs/api/java/util/ResourceBundle.html | java.util.ResourceBundle | ]] class for working which such mappings. To use it, one creates a file containing a mapping between keys (in the programmer’s language) and values (in the target language). Each key is, essentially, a ''%%String%%'' literal that appears in the user interface (e.g., "File", "Open", etc…). One mapping is created for each language, **including the programmer’s language**. The appropriate mapping is then loaded (based on the ''%%Locale%%'') and is used everywhere a ''%%String%%'' literal would otherwise be used. | Some people/companies approach the localization of terminology as a problem in machine translation but most use a simple mapping (and rely on human translators to do the hard work of translation). Java provides a [[ http://docs.oracle.com/javase/9/docs/api/java/util/ResourceBundle.html | java.util.ResourceBundle ]] class for working which such mappings. To use it, one creates a file containing a mapping between keys (in the programmer’s language) and values (in the target language). Each key is, essentially, a ''%%String%%'' literal that appears in the user interface (e.g., "File", "Open", etc…). One mapping is created for each language, **including the programmer’s language**. The appropriate mapping is then loaded (based on the ''%%Locale%%'') and is used everywhere a ''%%String%%'' literal would otherwise be used. |
| |
| |
| |
| |
There are versions of the ''%%printf()%%'' and ''%%format()%%'' methods in the [[ http://docs.oracle.com/javase/9/docs/api/java/io/PrintStream.html | java.io.PrintStream | ]] class that are a ''%%Locale%%'' object and use it to determine the appropriate formatting for numbers. The versions that are not passed a ''%%Locale%%'' simply call the versions that are, passing the default ''%%Locale%%''. Hence, if one uses these methods one need not make any special accommodations to handle locales. | There are versions of the ''%%printf()%%'' method in the [[ http://docs.oracle.com/javase/9/docs/api/java/io/PrintStream.html | java.io.PrintStream ]] class and and ''%%format()%%'' in the [[ http://docs.oracle.com/javase/9/docs/api/java/lang/String.html | java.lang.String ]] class that are a ''%%Locale%%'' object and use it to determine the appropriate formatting for numbers. The versions that are not passed a ''%%Locale%%'' simply call the versions that are, passing the default ''%%Locale%%''. Hence, if one uses these methods one need not make any special accommodations to handle locales. |
| |
| |
There is also a [[ http://docs.oracle.com/javase/9/docs/api/java/text/NumberFormat.html | java.text.NumberFormat | ]] class that provides even more flexibility, though it is less convenient. | There is also a [[ http://docs.oracle.com/javase/9/docs/api/java/text/NumberFormat.html | java.text.NumberFormat ]] class that provides even more flexibility, though it is less convenient. |
| |
| |
| |
| |
The [[ http://docs.oracle.com/javase/9/docs/api/java/text/DateFormat.html | java.text.DateFormat | ]] class has a ''%%format()%%'' method that can be used to format dates in a variety of ways. It can be used to create ''%%SHORT%%'', ''%%MEDIUM%%'', ''%%LONG%%'' and ''%%FULL%%'' representations. | The [[ http://docs.oracle.com/javase/9/docs/api/java/text/DateFormat.html | java.text.DateFormat ]] class has a ''%%format()%%'' method that can be used to format dates in a variety of ways. It can be used to create ''%%SHORT%%'', ''%%MEDIUM%%'', ''%%LONG%%'' and ''%%FULL%%'' representations. |
| |
| |
* [[ https://docs.oracle.com/javase/tutorial/i18n/ | Localization/Internationalization in Java ]] | * [[ https://docs.oracle.com/javase/tutorial/i18n/ | Localization/Internationalization in Java ]] |
* [[ http://www.oracle.com/technetwork/articles/javase/locale-140624.html | | * [[ http://www.oracle.com/technetwork/articles/javase/locale-140624.html | |
Understanding ''%%Locale%%'' ]] | Understanding Locale ]] |
* [[ https://docs.oracle.com/javase/tutorial/i18n/text/shapedDigits.html | | * [[ https://docs.oracle.com/javase/tutorial/i18n/text/shapedDigits.html | |
Using Different Number-Writing Systems ]] | Using Different Number-Writing Systems ]] |