惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Security Archives - TechRepublic
MongoDB | Blog
MongoDB | Blog
量子位
博客园 - 叶小钗
罗磊的独立博客
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Hacker News: Ask HN
Hacker News: Ask HN
MyScale Blog
MyScale Blog
GbyAI
GbyAI
Help Net Security
Help Net Security
Y
Y Combinator Blog
Engineering at Meta
Engineering at Meta
Hacker News - Newest:
Hacker News - Newest: "LLM"
Latest news
Latest news
H
Hacker News: Front Page
Blog — PlanetScale
Blog — PlanetScale
雷峰网
雷峰网
Microsoft Azure Blog
Microsoft Azure Blog
P
Proofpoint News Feed
C
CXSECURITY Database RSS Feed - CXSecurity.com
Scott Helme
Scott Helme
S
Schneier on Security
博客园 - 司徒正美
Hugging Face - Blog
Hugging Face - Blog
S
Security @ Cisco Blogs
Recorded Future
Recorded Future
S
Securelist
博客园 - Franky
Application and Cybersecurity Blog
Application and Cybersecurity Blog
A
About on SuperTechFans
N
News and Events Feed by Topic
AI
AI
T
Tenable Blog
N
News | PayPal Newsroom
C
Cybersecurity and Infrastructure Security Agency CISA
V
V2EX - 技术
T
Threat Research - Cisco Blogs
Cisco Talos Blog
Cisco Talos Blog
L
LINUX DO - 热门话题
N
Netflix TechBlog - Medium
S
SegmentFault 最新的问题
T
The Blog of Author Tim Ferriss
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Google Online Security Blog
Google Online Security Blog
S
Security Affairs
Webroot Blog
Webroot Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
博客园 - 三生石上(FineUI控件)
C
Comments on: Blog
G
GRAHAM CLULEY

博客园 - daniel-shen

精通EJB-概述 硬件基础知识大全 教程:关于XMLBeans的初步 在客户端进行xslt对xml的xuan渲染 jaxp笔记2007-4-17 6.ZK用户接口标记语言 5.事件的监听和处理 - daniel-shen - 博客园 4.组件的生命周期 3.zk体系结构 "tomcat启动分析"文章读后笔记 jdk5.0 范型说明 java 数组反射例子 HTML小技巧 MessageFormat初步 jni中关于dll的装载问题 java线程-消费者vs生产者 HttpSessionBindingListener实现与应用 用Java Servlets 2.4实现过滤 加和不加java:comp/env/前缀有什么区别?
ResourceBundle 与locale的使用
daniel-shen · 2006-11-29 · via 博客园 - daniel-shen

ResourceBundle
使用ResourceBundle访问本地资源
        在设计时,我们往往需要访问一些适合本地修改的配置信息,如果作为静态变量,那么每次修改都需要重新编译一个class,.config保存此类信息并不适合,这时我们需要ResourceBundle。
   通过ResourceBundle,我们需要访问位于/WEB-INF/classes目录下的一个后缀名为properties的文本类型文件,从里面读取我们需要的值。

    Locale locale = Locale.getDefault();
    ResourceBundle localResource = ResourceBundle.getBundle("ConnResource", locale);
 
    String value = localResource.getString("test");
    System.out.println("ResourceBundle: " + value);

    这里对应了/WEB-INF/class/ConnResource.properties文件内容为:

    test=hello world

    打印出来的结果就是hello world
   
    请注意,这里我们可以利用Locale和ResourceBundle的这个组合创建国际化的java程序。我们可以把locale实例化为new Locale("zh","CN");

    通过ResourceBundle.getBundle("MessagesBundle",  locale);

    系统将自动寻找MessagesBundle_zh_CN,即定义为中国大陆地区简体中文。如果没有该文件,则会依次寻找MessagesBundle_zh,MessagesBundle,直到找到为止。

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
代码:
import java.util.Locale;
import java.util.ResourceBundle;
public class LocaleTest {
 public static void main(String[] args)
 {
  Locale localeCN = new Locale("zh","CN");
  Locale localeJP = new Locale("co","JP");
   ResourceBundle localResourceCN = ResourceBundle.getBundle("ConnResource", localeCN);
   String valueCN = localResourceCN.getString("test");
   System.out.println("ResourceBundle: " + valueCN);
  
   ResourceBundle localResourceJP = ResourceBundle.getBundle("ConnResource", localeJP);
   String valueJP = localResourceJP.getString("test");
   System.out.println("ResourceBundle: " + valueJP);
 }
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
文件内容:
ConnResource_co_JP.properties:
                        test=hello japan
ConnResource_zh_CN.properties:
                        test=hello china