简体   繁体   English

Tomcat国际化维护

[英]Tomcat internationalization maintenance

I am trying to implement internationalization in Tomcat. 我正在尝试在Tomcat中实现国际化。 There are going to be different resource text files. 将有不同的资源文本文件。 My idea is to load all the resources in to the memory while tomcat loads. 我的想法是在tomcat加载时将所有资源加载到内存中。 Below is the sample code to load multiple resource in to the memory. 下面是将多个资源加载到内存中的示例代码。

public class ResourceBundleLoader {


    private static ResourceBundle enResourceBundle;
    private static ResourceBundle frResourceBundle;

    public static void loadBundle(){
        Locale enLocale = new Locale("en", "US");
        enResourceBundle = ResourceBundle.getBundle("MessagesBundle",enLocale);
        enLocale = new Locale("fr", "FR");
        frResourceBundle = ResourceBundle.getBundle("MessagesBundle",enLocale);
    }

    public static ResourceBundle getEnResourceBundle(){
        return enResourceBundle;
    }

    public static ResourceBundle getFrResourceBundle(){
        return frResourceBundle;
    }
}

The method loadBundle is called once thru startup servlet. 通过启动servlet一次调用loadBundle方法。 And getEnResourceBundle() and getFrResourceBundle() is called accordingly. 并相应地调用getEnResourceBundle()和getFrResourceBundle()。 Is this right way to implement/maintain internationalization in tomcat? 在雄猫中实现/保持国际化的正确方法吗? or is there any better way? 还是有更好的方法?

Thanks in advance. 提前致谢。

You dont need to make this helper class, as per the java documentation the bundles are already cached for you in memory. 您不需要制作此帮助程序类,因为根据Java文档 ,捆绑包已经为您缓存在内存中。 This will just make your code more complicated to maintain. 这只会使您的代码维护更加复杂。 ie You would have to alter your code every time you add a new "bundle". 即,每次添加新的“捆绑包”时,您都必须更改代码。

Just add code like this to your servlets and/or JSP's: 只需将这样的代码添加到您的servlet和/或JSP:

//request.getLocale() returns the web browsers locale
bundle = ResourceBundle.getBundle("MessagesBundle",request.getLocale())

Just make sure you have a default message bundle file with all your text. 只要确保您有一个包含所有文本的默认消息捆绑文件即可。 Then you can just add extra locales at will as things get translated. 然后,您可以随翻译随便添加其他语言环境。

UTF-8 support UTF-8支持

I also strongly suggest you create a servlet filter that applies to all requests that ensures that UTF-8 is turned on for both the html that is output, and the parsing of the form responses that are posted back to your application: 我也强烈建议您创建一个适用于所有请求的Servlet过滤器,以确保为输出的html和发回应用​​程序的表单响应的解析都打开了UTF-8:

request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");

I wouldn't optimize until I knew the i18n was too slow. 在知道i18n速度太慢之前,我不会进行优化。

But if I proceeded down your path, instead of using scalar ResourceBundles, I'd put the ResouceBundles into a Map. 但是,如果我沿着您的道路前进,而不是使用标量ResourceBundles,我会将ResouceBundles放入地图中。 Now your code can use any bundle knowing the locale - which you have to select the appropriate ResourceBundle anyway. 现在,您的代码可以使用任何知道语言环境的捆绑软件-无论如何您都必须选择适当的ResourceBundle。

Your code won't have any if locale is this, use English . 如果您的代码没有if locale is this, use English Instead, it will be myResourceBundle = bundleMap.get(myLocale); 相反,它将是myResourceBundle = bundleMap.get(myLocale);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM