简体   繁体   English

使用Java中的资源包以国际化方式加载多个特定于语言环境的属性文件

[英]Load multiple locale specific properties files in internationalization with resource bundle in java

I have four properties files 我有四个属性文件

  1. Application.properties Application.properties
  2. Application_fr_FR.properties Application_fr_FR.properties
  3. Database.properties Database.properties
  4. Database_fr_FR.properties Database_fr_FR.properties

So now I need internationalization in multiple programs, so now I need to load multiple properties files and get the key-value pair of values from properties files specific to a locale. 所以现在我需要在多个程序中进行国际化,所以现在我需要加载多个属性文件并从特定于语言环境的属性文件中获取键值对。 For that I have a ResourceBundleService.java 为此,我有一个ResourceBundleService.java

public class ResourceBundleService {
    private static String language;
    private static String country;
    private static Locale currentLocale;
    static ResourceBundle labels;
    static {
        labels = ResourceBundle
                .getBundle("uday.properties.Application");
        labels = append(Database.properties");
        //** how to append existing resource bundle with new properties file?
    }

    public static String getLabel(String resourceIndex, Locale locale) {
        return labels.getString(resourceIndex);
        //How to get locale specific messages??
    }
}

Hope the question is clear. 希望问题清楚。

You need to call ResourceBundle.getBundle(baseName, locale) each time in getLabel . 您需要每次在getLabel调用ResourceBundle.getBundle(baseName, locale) ResourceBundle maintains an internal cache so it wont load all props files each time: ResourceBundle维护内部缓存,因此不会每次都加载所有props文件:

public static String getLabel(String resourceIndex, Locale locale) {
    ResourceBundle b1 = ResourceBundle.getBundle("uday.properties.Application", locale);
    if (b1.contains(resourceIndex)) {
       return b1.getString(resourceIndex);
    }
    ResourceBundle b2 = ResourceBundle.getBundle("uday.properties.Database", locale);
    return b2.getString(resourceIndex);
}

For the time being use Application_fr.properties; 暂时使用Application_fr.properties; les Canadiens will be thankful. les Canadiens非常感谢。 With Locale.setDefault(availableLocale) elect an available locale. 使用Locale.setDefault(availableLocale)选择一个可用的语言环境。 The root locale properties, Application.properties, should also contain the language keys. 根区域设置属性Application.properties也应包含语言键。 You could copy the French ones. 您可以复制法语的。 In that case you need not set the default locale. 在这种情况下,您无需设置默认语言环境。

Lets check this implementation on github it works really nice. 让我们在github上检查此实现,它确实很好用。 It requires the following function naming convention: 它需要以下函数命名约定:

MultiplePropertiesResourceBundle is an abstract base implementation to allow to combine a ResourceBundle from multiple properties files whereas these properties files must end with the same name - the base-name for these combined ResourceBundle. MultiplePropertiesResourceBundle是一个抽象的基本实现,允许组合来自多个属性文件的ResourceBundle,而这些属性文件必须以相同的名称结尾-这些组合的ResourceBundle的基本名称。

If you'll use it at the first you need to implement abstract class MultiplePropertiesResourceBundle as below: 如果首先使用它,则需要实现抽象类MultiplePropertiesResourceBundle ,如下所示:

import ch.dueni.util.MultiplePropertiesResourceBundle;

public class CombinedResources extends MultiplePropertiesResourceBundle {

    public  CombinedResources() {
        super("package_with_bundles");
    }

}

then you should implement empty class which extends out CombinedResources : 那么您应该实现扩展了CombinedResources空类:

public class CombinedResources_en extends CombinedResources {}

and so on for other languages. 其他语言也是如此。 After that you can use your bundle as below: 之后,您可以按以下方式使用捆绑包:

ResourceBundle bundle = ResourceBundle.getBundle("CombinedResources");

This bundle will be use all properties files inside package_with_bundles . 该捆绑软件将使用package_with_bundles所有属性文件。 For more information just look inside github repo. 有关更多信息,请查看github repo。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java国际化 - 我是否必须为每个类加载资源包? - Java internationalization - Do I have to load a resource bundle for every class? 如何使用/导入要在 HTML 文件(非 JSP)中使用的 Java 区域设置类和资源包属性? - How Do I Use/Import my Java Locale class and Resource Bundle properties to be used in my HTML files (not JSP)? 在Java Spring中使用资源包中的语言环境 - Using Locale from Resource Bundle in Java Spring Java:将多个属性文件作为一个加载 - Java: Load multiple properties files as one 使用JSP中的资源包属性进行国际化,非拉丁文本成为Mojibake - Internationalization using resource bundle properties in JSP, non-Latin text becomes Mojibake 使用Java国际化(i18n)修改Wicket的XML资源包 - Modify Wicket's XML Resource Bundle with Java internationalization (i18n) 如何在Java代码中使用资源包文件? - How to use resource bundle files in Java code? JavaFX 2和国际化加载属性文件 - JavaFX 2 and Internationalization loading properties files 国际化(语言环境)在 java spring boot 中不使用重音 - Internationalization (locale) not working with accents in java spring boot 如何从 Java 中的文件资源加载资源包? - How to load a resource bundle from a file resource in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM