以下文章提供了 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 <p><strong>Output:</strong></p> <p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/172500369926024.jpg?x-oss-process=image/resize,p_40" class="lazy" alt="Java 的国际化" ></p> <p>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.</p> <h4 id="Example">Example #2</h4> <p>Example for the internationalization in Java to show the number in different formats for the different countries.</p> <p><strong>Code:</strong></p> <pre class="brush:php;toolbar:false">// 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 #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:
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中文网其他相关文章!

本文讨论了使用Maven和Gradle进行Java项目管理,构建自动化和依赖性解决方案,以比较其方法和优化策略。

本文使用Maven和Gradle之类的工具讨论了具有适当的版本控制和依赖关系管理的自定义Java库(JAR文件)的创建和使用。

本文讨论了使用咖啡因和Guava缓存在Java中实施多层缓存以提高应用程序性能。它涵盖设置,集成和绩效优势,以及配置和驱逐政策管理最佳PRA

本文讨论了使用JPA进行对象相关映射,并具有高级功能,例如缓存和懒惰加载。它涵盖了设置,实体映射和优化性能的最佳实践,同时突出潜在的陷阱。[159个字符]

Java的类上载涉及使用带有引导,扩展程序和应用程序类负载器的分层系统加载,链接和初始化类。父代授权模型确保首先加载核心类别,从而影响自定义类LOA


热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

禅工作室 13.0.1
功能强大的PHP集成开发环境

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。