首页  >  文章  >  Java  >  Java 的国际化

Java 的国际化

WBOY
WBOY原创
2024-08-30 15:41:371072浏览

以下文章提供了 Java 国际化的概述。国际化是创建 Web 应用程序的过程,使其自动支持多个国家/地区、语言和货币,而不需要对应用程序进行任何更改。它也被称为 I18N,因为字母 I 和 N 之间有 18 个字符。全球市场是当今设计软件或网站时的一个重要因素。随着全球市场软件应用程序的不断扩展,公司必须创建能够与当地区域和语言的用户互动的产品。

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

为国际市场开发软件的开发人员应该了解每种文化的习俗和差异。语言、标点符号、货币、日期、时间、数字和时区都是差异的例子。本地化也缩写为 I10N,因为首字母“L”和最后一个字母“N,”之间有十个字符。本地化是向应用程序添加特定于区域设置的文本和组件的过程,以便可以根据给定的语言和地点对其进行定制。

Java 国际化语法

以下类可用于实现国际化:

  • 语言环境
  • 数字格式
  • 日期格式

1.语言环境

Locale 对象可用于表示地理位置或语言。 java.util 包包含一个 Locale 类。

Locale 类的构造函数:

Locale l = new Locale(String language);
Locale l = new Locale(String language, String country);

Locale 类的常量:

一些 Locale 常量已在 Locale 类中声明。

这些常量可以直接使用,下面列出几个常量:

  • 区域设置。英国
  • 语言环境.意大利
  • Locale.US

Locale 类的功能:

  • pubic static Locale getDefault():该方法用于获取当前语言环境的实例。
  • public static Locale[] getAvailableLocales(): 此方法用于获取当前语言环境的数组。
  • public String getDisplayLanguage(Locale l):该方法用于获取传入的 locale 对象的语言名称。
  • public String getDisplayVariant(Locale l): 该方法用于获取传入的 locale 对象的变体代码。
  • public String getDisplayCountry(Locale l):该方法用于获取传入的 locale 对象的国家名称。
  • public static getISO3Language(): 该方法用于获取当前语言环境的语言代码的三字母缩写。
  • public static getISO3Country(): 此方法用于获取当前语言环境国家/地区的三个字母缩写。

2.数字格式

我们可以使用 NumberFormat 类根据特定区域设置格式化数字。 NumberFormat 类存在于 java.Text 包中,是一个抽象类,因此我们无法使用其构造函数创建对象。

Locale 类的功能:

  • public static NumberFormat getInstance():该方法用于获取默认Locale的对象。
  • public static NumberFormat getInstance(Locale l): 该方法用于获取传入的 Locale 对象的对象。
  • public static NumberFormat getCurrencyInstance(): 该方法用于获取默认 Locale 的对象以特定货币显示。
  • 公共静态格式(long l):此方法用于将传递的数字转换为区域设置对象。

3.日期格式

我们将日期格式国际化,因为日期格式因位置而异。我们可以使用 DateFromat 类根据给定的区域设置格式化日期。 DateFormat是java.text包中的一个抽象类。

Locale 类的常量:

一些 DateFormat 常量已在 DateFormat 类中声明。

这些常量可以直接使用,下面列出了一些常量:

  • 日期格式.LONG
  • 日期格式.MINUTE_FIELD
  • 日期格式.MINUTE_FIELD

DateFormat 类的函数:

  • final String format(Date date): This method converts and returns the specified Date object into a string.
  • static final DateFormat getInstance(): This method is used to gets a date-time formatter with a short formatting style for date and time.
  • static Locale[] getAvailableLocales(): This method is used to gets an array of present locales.
  • static final DateFormat getTimeInstance(): This method is used to gets time formatter with default formatting style for the default locale.
  • static final DateFormat getTimeInstance(int style): This method is used to gets time formatter with the specified formatting style for the default locale.
  • static final DateFormat getTimeInstance(int style, Locale locale): This method is used to gets time formatter with the specified formatting style for the given locale.
  • TimeZone getTimeZone(): This method is used to gets an object of TimeZone for this DateFormat instance.
  • static final DateFormat getDateInstance(): This method is used to gets date formatter with default formatting style for the default locale.
  • static final DateFormat getDateInstance(int style): This method is used to gets date formatter with the specified formatting style for the default locale.
  • static final DateFormat getDateInstance(int style, Locale locale): This method is used to gets date formatter with the specified formatting style for the given locale.
  • static final DateFormat getDateTimeInstance(): This method is used to gets date or time formatter with default formatting style for the default locale.
  • NumberFormat getNumberFormat(): This method is used to gets an object of NumberFormat for this DateFormat instance.
  • Calendar getCalendar(): This method is used to gets an object of the Calendar for this DateFormat instance.

Examples of Internationalization in Java

Given below are the examples mentioned:

Example #1

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:

Java 的国际化

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 #2

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:

Java 的国际化

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 #3

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:

Java 的国际化

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.

Conclusion

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中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn