目录
Java 国际化语法
1.语言环境
2.数字格式
3.日期格式
Examples of Internationalization in Java
Example #3
Conclusion
首页 Java java教程 Java 的国际化

Java 的国际化

Aug 30, 2024 pm 03:41 PM
java

以下文章提供了 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

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

突破或从Java 8流返回? 突破或从Java 8流返回? Feb 07, 2025 pm 12:09 PM

Java 8引入了Stream API,提供了一种强大且表达力丰富的处理数据集合的方式。然而,使用Stream时,一个常见问题是:如何从forEach操作中中断或返回? 传统循环允许提前中断或返回,但Stream的forEach方法并不直接支持这种方式。本文将解释原因,并探讨在Stream处理系统中实现提前终止的替代方法。 延伸阅读: Java Stream API改进 理解Stream forEach forEach方法是一个终端操作,它对Stream中的每个元素执行一个操作。它的设计意图是处

PHP:网络开发的关键语言 PHP:网络开发的关键语言 Apr 13, 2025 am 12:08 AM

PHP是一种广泛应用于服务器端的脚本语言,特别适合web开发。1.PHP可以嵌入HTML,处理HTTP请求和响应,支持多种数据库。2.PHP用于生成动态网页内容,处理表单数据,访问数据库等,具有强大的社区支持和开源资源。3.PHP是解释型语言,执行过程包括词法分析、语法分析、编译和执行。4.PHP可以与MySQL结合用于用户注册系统等高级应用。5.调试PHP时,可使用error_reporting()和var_dump()等函数。6.优化PHP代码可通过缓存机制、优化数据库查询和使用内置函数。7

PHP与Python:了解差异 PHP与Python:了解差异 Apr 11, 2025 am 12:15 AM

PHP和Python各有优势,选择应基于项目需求。1.PHP适合web开发,语法简单,执行效率高。2.Python适用于数据科学和机器学习,语法简洁,库丰富。

PHP与其他语言:比较 PHP与其他语言:比较 Apr 13, 2025 am 12:19 AM

PHP适合web开发,特别是在快速开发和处理动态内容方面表现出色,但不擅长数据科学和企业级应用。与Python相比,PHP在web开发中更具优势,但在数据科学领域不如Python;与Java相比,PHP在企业级应用中表现较差,但在web开发中更灵活;与JavaScript相比,PHP在后端开发中更简洁,但在前端开发中不如JavaScript。

PHP与Python:核心功能 PHP与Python:核心功能 Apr 13, 2025 am 12:16 AM

PHP和Python各有优势,适合不同场景。1.PHP适用于web开发,提供内置web服务器和丰富函数库。2.Python适合数据科学和机器学习,语法简洁且有强大标准库。选择时应根据项目需求决定。

Java程序查找胶囊的体积 Java程序查找胶囊的体积 Feb 07, 2025 am 11:37 AM

胶囊是一种三维几何图形,由一个圆柱体和两端各一个半球体组成。胶囊的体积可以通过将圆柱体的体积和两端半球体的体积相加来计算。本教程将讨论如何使用不同的方法在Java中计算给定胶囊的体积。 胶囊体积公式 胶囊体积的公式如下: 胶囊体积 = 圆柱体体积 两个半球体体积 其中, r: 半球体的半径。 h: 圆柱体的高度(不包括半球体)。 例子 1 输入 半径 = 5 单位 高度 = 10 单位 输出 体积 = 1570.8 立方单位 解释 使用公式计算体积: 体积 = π × r2 × h (4

PHP:许多网站的基础 PHP:许多网站的基础 Apr 13, 2025 am 12:07 AM

PHP成为许多网站首选技术栈的原因包括其易用性、强大社区支持和广泛应用。1)易于学习和使用,适合初学者。2)拥有庞大的开发者社区,资源丰富。3)广泛应用于WordPress、Drupal等平台。4)与Web服务器紧密集成,简化开发部署。

创造未来:面向零基础的 Java 编程 创造未来:面向零基础的 Java 编程 Oct 13, 2024 pm 01:32 PM

Java是热门编程语言,适合初学者和经验丰富的开发者学习。本教程从基础概念出发,逐步深入讲解高级主题。安装Java开发工具包后,可通过创建简单的“Hello,World!”程序实践编程。理解代码后,使用命令提示符编译并运行程序,控制台上将输出“Hello,World!”。学习Java开启了编程之旅,随着掌握程度加深,可创建更复杂的应用程序。

See all articles