Tomcat的信息国际化之路
来源:互联网 发布:mac子弹头专柜价格 编辑:程序博客网 时间:2024/05/17 09:14
我们都知道Tomcat是一个很流行的轻量级应用容器,正因为他的流行,他有各种各样的说各种语言的用户群体,那么他是怎么解决信息国际化呢?
先看一下JDK是怎么处理信息国际化:
创建几个资源文件:
msg.properties
name=dusk对应的中文文件message_zh.properties
name=dushangkui
以及中国地区的中文文件message_zh_cn.properties
name=\u675c\u5c1a\u594e
创建场景类:
package dusk.file.local;import org.junit.Test;import java.util.Locale;import java.util.ResourceBundle;/** * Created by dushangkui on 2017/5/10. */public class TestLocal { @Test public void test(){ Locale locale1 = new Locale(""); Locale locale2 = new Locale("zh"); Locale locale3 = new Locale("zh","cn"); ResourceBundle bnd1 = ResourceBundle.getBundle(TestLocal.class.getPackage().getName()+".message", locale1); ResourceBundle bnd2 = ResourceBundle.getBundle(TestLocal.class.getPackage().getName()+".message", locale2); ResourceBundle bnd3 = ResourceBundle.getBundle(TestLocal.class.getPackage().getName()+".message", locale3); System.out.println(bnd1.getString("name")); System.out.println(bnd2.getString("name")); System.out.println(bnd3.getString("name")); }}
运行结果:
我们可以看到根据不同的Locale信息,我们获取到了不同的适合结果。
注: java.util.Locale是本地化辅助类,用于描述特定的地理、政治和文化地区。
我们的项目结构如下:
现在回到正题,Tomcat是怎么做信息国际化呢?
是通过类org.apache.naming.StringManager完成的,它里面封装了对ResourceBundle的处理,因为ResourceBundle需要两个参数,文件类路径以及Locale信息,所以Tomcat的StringManager也是基于包完成的。我们可以看一个实际使用例子:
查看源码,我们很容易知道他是一个单例类。其中一个很重要的方法是
public String getString(String key)根据一个key获取对应的信息。当然了还有一个可以带参数格式化信息的重载方法:
public String getString(final String key, final Object... args)
/** * Creates a new StringManager for a given package. This is a * private method and all access to it is arbitrated by the * static getManager method call so that only one StringManager * per package will be created. * * @param packageName Name of package to create StringManager for. */ private StringManager(String packageName, Locale locale) { String bundleName = packageName + ".LocalStrings"; ResourceBundle bnd = null; try { bnd = ResourceBundle.getBundle(bundleName, locale); } catch( MissingResourceException ex ) { // Try from the current loader (that's the case for trusted apps) // Should only be required if using a TC5 style classloader structure // where common != shared != server ClassLoader cl = Thread.currentThread().getContextClassLoader(); if( cl != null ) { try { bnd = ResourceBundle.getBundle(bundleName, locale, cl); } catch(MissingResourceException ex2) { // Ignore } } } bundle = bnd; // Get the actual locale, which may be different from the requested one if (bundle != null) { Locale bundleLocale = bundle.getLocale(); if (bundleLocale.equals(Locale.ROOT)) { this.locale = Locale.ENGLISH; } else { this.locale = bundleLocale; } } else { this.locale = null; } }
getString的关键代码
/** Get a string from the underlying resource bundle or return null if the String is not found. @param key to desired resource String @return resource String matching <i>key</i> from underlying bundle or null if not found. @throws IllegalArgumentException if <i>key</i> is null. */ public String getString(String key) { if(key == null){ String msg = "key may not have a null value"; throw new IllegalArgumentException(msg); } String str = null; try { // Avoid NPE if bundle is null and treat it like an MRE if (bundle != null) { str = bundle.getString(key); } } catch(MissingResourceException mre) { //bad: shouldn't mask an exception the following way: // str = "[cannot find message associated with key '" + key + // "' due to " + mre + "]"; // because it hides the fact that the String was missing // from the calling code. //good: could just throw the exception (or wrap it in another) // but that would probably cause much havoc on existing // code. //better: consistent with container pattern to // simply return null. Calling code can then do // a null check. str = null; } return str; }
可以看出获取国际化信息的时候透传给了ResourceBundle。
1 0
- Tomcat的信息国际化之路
- 信息的国际化
- 复习struts2之输出带有占位符的国际化信息
- 《研磨struts2》第九章 国际化 之 9.5 Struts2访问国际化信息的不同方式
- Spring之国际化信息MessageSource源码阅读
- 国际化软件工程师的成才之路
- Spring-国际化信息03-容器级的国际化信息资源
- tomcat国际化
- 《研磨struts2》第九章 国际化 之 9.6 指定语言信息的不同方式
- extremecomponents使用struts2国际化信息的方法
- extremecomponents使用struts2国际化信息的方法
- 输出带有占位符的国际化信息
- struts2--输入校验的国际化信息
- Android 之 尺寸的国际化
- Tomcat的基础信息
- 复习struts2之配置国际化全局资源文件、输出国际化信息
- 传奇之路——国际化的中国人
- swift国际化之路(拼写错误惹的祸)
- 0-1背包优化动态规划算法之跳跃点法
- Android 根据包名,获取应用程序的签名
- 算法导论复习(5) 线性时间排序
- 自定义滚动轴样式
- sql中的数据表三种连接方式
- Tomcat的信息国际化之路
- 前端项目实战1:酒仙网首页
- 给自己的mongodb设置密码吧
- angularJS基本表格
- OC学习笔记4
- Leetcode学习(41)—— Find All Anagrams in a String
- 拥抱JavaScript闭包
- 解决 上传的excel2007文件,在用poi处理时通过new XSSFWorkbook(inputStream)出错
- vim