Java 9 - Internationalization

Gowgi
866 views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

Welcome to Java 9 Internationalization - By Gowtham Girithar Srirangasamy

Java 9 has an improvements in Internationalization.The improvements are described below,

Unicode 8

Java 8 supported till unicode 6.2. The Unicode 6.3, 7.0, and 8.0 standards introduced 10,555 characters, 29 scripts, and 42 blocks, all of which are supported in Java SE 9.

UTF -8 Properties Files

In previous releases of Java, ISO-8859-1 encoding was used to load the property resource bundles, (i.e.,PropertyResourceBundle – constructing its instance from an InputStream requires that the input stream be encoded in ISO-8859-1). But ISO-8859-1 is not a convenient way to represent the non-Latin characters. In Java SE 9 ,property files are loaded using UTF -8 encoding.

CLDR Locale Data Enabled by Default

In JDK 8 and previous releases, JRE is the default locale data. JDK 9 sets CLDR as highest priority by default.

The Unicode 8 and UTF -8 Properties Files are explained below with an example. In the below example , we have used the property files also to print.Reason for using the output property files is to see the output ,if the console is not supporting UTF-8

Hands-on Demo

Internationalization Enhancements in JDK 9 By Gowtham Girithar Srirangasamy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.demo.unicode;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Properties;
import java.util.PropertyResourceBundle;
/**
* Class to describe the java 9 features of Internationalization in JDK 9
* Java 9 Features Demo with modular development.
* @author Gowtham Girithar Srirangasamy
*
*
*/
public class DemoUnicode {
/**
* For input.properties the content is
* key =€ / \u20AC
* mood = 😎 / \uD83D\uDE0E
* chineese =
*
*/
public static void main(String[] args) throws FileNotFoundException, IOException {
/** demo for PropertyResourceBundle and Properties */
System.out.println("Internationalization Enhancements in JDK 9 ");
System.out.println("Inputs are");
System.out.println("key =€ / \u20AC");
System.out.println("mood = 😎 / \uD83D\uDE0E");
System.out.println("chineese =");
System.out.println("**********Outputs*************");
System.out.println("****** With Property Resource Bundle************ ");
withPropertyResourceBundle(new FileInputStream("input.properties"));
System.out.println("****** With Properties************ ");
withProperties(new FileInputStream("input.properties"));
}
/**
* This method will read the value from the property file and write into the another one.
* This method will print the actual characters.
* @param propertyFile
* @throws IOException
*/
private static void withPropertyResourceBundle(InputStream propertyFile) throws IOException {
/** the text file is created becuasue the console might not have UTF-8 support for many*/
PrintWriter writer = new PrintWriter("output_1.txt", "UTF-8");
PropertyResourceBundle properties = new PropertyResourceBundle(propertyFile);
/** iterate and print the values*/
properties.getKeys().asIterator().forEachRemaining(key -> {
String value = properties.getString(key);
/** Printing the value will help for whom UTF-8 will be supported*/
System.out.println(key + " = " + value);
/**printing in file*/
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

#Explanation on Output

In the above program, we have given the input in property files and then output is printed in console and also in the text files.UTF-8 is supported in PropertyResourceBundle.

Open Source Your Knowledge: become a Contributor and help others learn. Create New Content