13.1 I18N Basics Explained
Internationalization (I18N) is the process of designing and developing applications that can be adapted to various languages and regions without engineering changes. This is crucial for creating software that can be used globally, ensuring that users from different cultures and languages can interact with the application effectively.
Key Concepts
1. Locale
A Locale
represents a specific geographical, political, or cultural region. It is used to determine the language, country, and any special variant (such as a dialect) for formatting dates, numbers, and currencies. In Java, the Locale
class is used to encapsulate this information.
Example
Locale usLocale = new Locale("en", "US"); Locale frLocale = new Locale("fr", "FR");
2. Resource Bundles
Resource bundles are files that contain locale-specific objects. They are used to store messages, labels, and other text that need to be displayed to the user. Java provides the ResourceBundle
class to manage these bundles, allowing you to load the appropriate bundle based on the user's locale.
Example
ResourceBundle bundle = ResourceBundle.getBundle("Messages", usLocale); String greeting = bundle.getString("greeting");
3. Formatting
Formatting is the process of converting data into a string representation that is appropriate for a given locale. Java provides various classes for formatting, such as DateFormat
for dates, NumberFormat
for numbers, and MessageFormat
for complex messages.
Example
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG, usLocale); String formattedDate = dateFormat.format(new Date());
4. Character Encoding
Character encoding is the process of converting characters into bytes and vice versa. Different locales may use different character sets, so it's important to handle encoding correctly to ensure that text is displayed accurately. Java uses the Charset
class to manage character encoding.
Example
Charset utf8 = Charset.forName("UTF-8"); String text = new String("Hello, World!".getBytes(), utf8);
5. Locale-Sensitive Classes
Java provides several classes that are locale-sensitive, meaning they adapt their behavior based on the current locale. Examples include Collator
for sorting strings, BreakIterator
for text boundaries, and DateFormatSymbols
for date symbols.
Example
Collator collator = Collator.getInstance(frLocale); collator.setStrength(Collator.PRIMARY); List<String> words = Arrays.asList("école", "ecole", "éclair"); words.sort(collator);
Examples and Analogies
Think of I18N as preparing a universal recipe that can be adapted to different tastes and dietary requirements. For example, a recipe for pizza can be adjusted to include different toppings based on the region (locale). The base recipe (application) remains the same, but the toppings (localized content) change.
For instance, if you are developing a global e-commerce platform, I18N allows you to display product descriptions, prices, and dates in the user's preferred language and format. This ensures that users from different regions can easily understand and use the platform.
By mastering I18N basics, you can create more inclusive and user-friendly Java SE 11 applications, capable of reaching a global audience and providing a seamless user experience across different locales.