java i18n_Java i18n – Java的国际化

本文介绍了Java的国际化(i18n)功能,通过资源包实现不同语言环境的支持。讲解了如何创建特定语言环境的属性文件,并使用`ResourceBundle`加载这些文件,动态获取和更新页面文本。同时提供了一个Java i18n的示例,展示如何在项目中应用这些概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

java i18n

Internationalization in Java or Java i18n is a very important feature. Java provides internationalization (i18n) support through resource bundles.

Java或Java i18n中的国际化是非常重要的功能。 Java通过资源包提供国际化(i18n)支持。

Java国际化 (Internationalization in Java)

For making your application support different locales, we need to create locale specific properties file. The file names follow the pattern of bundle name with language code and country code, for example ApplicationMessages_en_US.properties.

为了使您的应用程序支持不同的语言环境,我们需要创建特定于语言环境的属性文件。 文件名遵循具有语言代码和国家/地区代码的捆绑包名称的模式,例如ApplicationMessages_en_US.properties

Once the property files for specific locales are ready, all you need to do is initialize the resource bundle with correct Locale. Java provides two classes java.util.ResourceBundle and java.util.Locale that are used for this purpose. ResourceBundle reads the locale specific property file and you can get the locale specific value for any key.

准备好特定语言环境的属性文件后,您要做的就是使用正确的语言环境初始化资源包。 Java提供了两个用于此目的的类java.util.ResourceBundlejava.util.Locale 。 ResourceBundle读取特定于语言环境的属性文件,您可以获取任何键的特定于语言环境的值。

This is very helpful in making your web application texts locale specific, you can get the locale information from the HTTP request and generate the dynamic page with that locale resource bundle files. You can also provide option to user to chose the locale and update the labels dynamically.

这对于使Web应用程序文本特定于区域设置非常有帮助,您可以从HTTP请求中获取区域设置信息,并使用该区域设置资源包文件生成动态页面。 您还可以为用户提供选择语言环境并动态更新标签的选项。

Java i18n示例 (Java i18n Example)

For java i18n example, I have created the project whose structure is like below image.

对于java i18n示例,我创建了一个项目,其结构如下图所示。

Here is the java code for JavaInternationalizationExample class.

这是JavaInternationalizationExample类的Java代码。

package com.journaldev.i18n;

import java.util.Locale;
import java.util.ResourceBundle;

public class JavaInternationalizationExample {

    public static void main(String[] args) {
        //default locale
        ResourceBundle bundle = ResourceBundle.getBundle("ApplicationMessages");
        //Get ResourceBundle with Locale that are already defined
        ResourceBundle bundleFR = ResourceBundle.getBundle("ApplicationMessages", Locale.FRANCE);
        //Get resource bundle when Locale needs to be created
        ResourceBundle bundleSWE = ResourceBundle.getBundle("ApplicationMessages", new Locale("sv", "SE"));
        
        //lets print some messages
        printMessages(bundle);
        printMessages(bundleFR);
        printMessages(bundleSWE);
        
    }

    private static void printMessages(ResourceBundle bundle) {
        System.out.println(bundle.getString("CountryName"));
        System.out.println(bundle.getString("CurrencyCode"));
    }

}

Here bundle name is ApplicationMessages and I have 2 locale specific resource bundles and one default resource bundle.

这里的束名称是ApplicationMessages ,我有2个特定于语言环境的资源束和一个默认资源束。

ApplicationMessages.properties

ApplicationMessages.properties

CountryName=USA
CurrencyCode=USD

ApplicationMessages_fr_FR.properties

ApplicationMessages_fr_FR.properties

CountryName=France
CurrencyCode=Euro

ApplicationMessages_sv_SE.properties

ApplicationMessages_sv_SE.properties

CountryName=Sweden
CurrencyCode=Kr

Notice the use of Locale class, there are some locales already defined but we can always create new locale by passing language code and country code to it’s constructor.

注意使用Locale类,已经定义了一些语言环境,但是我们总是可以通过将语言代码和国家/地区代码传递给其构造函数来创建新的语言环境。

When I run the above program, here is the output.

当我运行上面的程序时,这是输出。

USA
USD
France
Euro
Sweden
Kr

That’s all for quick java i18n example. Internationalization in java is very useful in web application to serve pages in locale specific language.

这就是快速的Java i18n示例的全部内容。 Java的国际化在Web应用程序中以区域设置特定语言提供页面非常有用。

翻译自: https://www.journaldev.com/1370/java-i18n-internationalization-in-java

java i18n

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值