以下文章提供了 Java 国际化的概述。国际化是创建 Web 应用程序的过程,使其自动支持多个国家/地区、语言和货币,而不需要对应用程序进行任何更改。它也被称为 I18N,因为字母 I 和 N 之间有 18 个字符。全球市场是当今设计软件或网站时的一个重要因素。随着全球市场软件应用程序的不断扩展,公司必须创建能够与当地区域和语言的用户互动的产品。
开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
为国际市场开发软件的开发人员应该了解每种文化的习俗和差异。语言、标点符号、货币、日期、时间、数字和时区都是差异的例子。本地化也缩写为 I10N,因为首字母“L”和最后一个字母“N,”之间有十个字符。本地化是向应用程序添加特定于区域设置的文本和组件的过程,以便可以根据给定的语言和地点对其进行定制。
以下类可用于实现国际化:
Locale 对象可用于表示地理位置或语言。 java.util 包包含一个 Locale 类。
Locale 类的构造函数:
Locale l = new Locale(String language);
Locale l = new Locale(String language, String country);
Locale 类的常量:
一些 Locale 常量已在 Locale 类中声明。
这些常量可以直接使用,下面列出几个常量:
Locale 类的功能:
我们可以使用 NumberFormat 类根据特定区域设置格式化数字。 NumberFormat 类存在于 java.Text 包中,是一个抽象类,因此我们无法使用其构造函数创建对象。
Locale 类的功能:
我们将日期格式国际化,因为日期格式因位置而异。我们可以使用 DateFromat 类根据给定的区域设置格式化日期。 DateFormat是java.text包中的一个抽象类。
Locale 类的常量:
一些 DateFormat 常量已在 DateFormat 类中声明。
这些常量可以直接使用,下面列出了一些常量:
DateFormat 类的函数:
Given below are the examples mentioned:
Example for the internationalization in Java to create different country locale.
Code:
// The program can be tested in Eclipse IDE, JAVA 11 package jex; import java.util.Locale; public class ex { public static void main(String[] args) { Locale[] locales = { new Locale("en", "US"), new Locale("it", "IT"), new Locale("es", "ES") }; for (int l=0; l< locales.length; l++) { String Language = locales[l].getDisplayLanguage(locales[l]); System.out.println(locales[l].toString() + ": " + Language); } } }
Output:
As in the above program, the Locale class objects are created and store in the array. Next, used the for loop to iterate each locale object and display its name and its language, as we can see in the above output.
Example for the internationalization in Java to show the number in different formats for the different countries.
Code:
// The program can be tested in Eclipse IDE, JAVA 11 package jex; import java.util.*; import java.text.*; public class ex { public static void main (String[]args) { double n = 45273.8956; NumberFormat f1 = NumberFormat.getInstance (Locale.US); NumberFormat f2 = NumberFormat.getInstance (Locale.ITALY); NumberFormat f3 = NumberFormat.getInstance (Locale.CHINA); System.out.println ("The number format in US is :" + f1.format (n)); System.out.println ("The number format in ITALY is:" + f2.format (n)); System.out.println ("The number format in CHINA is :" + f3.format (n)); } }
Output:
As in the above program, three different NumberFormat class objects are created using the Locale class. Next, using the format() method of the NumberFormat class, the given number is printing in the specific format of the country, as we can see in the above output.
Example for the internationalization in Java to show the date in different formats for the different countries.
Code:
// The program can be tested in Eclipse IDE, JAVA 11 package jex; import java.text.DateFormat; import java.util.Date; import java.util.Locale; public class ex { public static void main (String[]args) { DateFormat d1 = DateFormat.getDateInstance (0, Locale.US); DateFormat d2 = DateFormat.getDateInstance (0, Locale.ITALY); DateFormat d3 = DateFormat.getDateInstance (0, Locale.CHINA); System.out.println ("The date format in US is :" + d1.format (new Date ())); System.out.println ("The date format in ITALY is : " + d2.format (new Date ())); System.out.println ("The date format in CHINA is : " + d3.format (new Date ())); } }
Output:
As in the above program, three different DateFormat class objects are created using the Locale class. Next, using the format() method of the DateFormat class, the return date of the Date() method is printing in the specific format of the country, as we can see in the above output.
Internationalization is also called I18N because there are 18 characters between the letters I and N. It is the process of creating web applications in such a way that they automatically support several countries, languages, and currencies without requiring any changes to the application.
以上是Java 的国际化的详细内容。更多信息请关注PHP中文网其他相关文章!