随笔 - 119  文章 - 3173  trackbacks - 0
<2007年5月>
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

交友莫独酒,茅台西凤游。
口干古井贡,心徜洋河流。
称多情杜康,趟无量双沟。
赞中华巍巍,无此不销愁。

常用链接

留言簿(68)

随笔分类(136)

随笔档案(122)

最新随笔

搜索

  •  

积分与排名

  • 积分 - 520562
  • 排名 - 93

最新评论

FreeMarkerTest:
 1 import java.io.BufferedWriter;
 2 import java.io.File;
 3 import java.io.FileOutputStream;
 4 import java.io.OutputStreamWriter;
 5 import java.io.Writer;
 6 import java.util.HashMap;
 7 import java.util.Locale;
 8 
 9 import freemarker.template.Configuration;
10 import freemarker.template.Template;
11 
12 public class FreeMarkerTest {
13 
14     public static void main(String[] args) {
15         FreeMarkerTest test = new FreeMarkerTest();
16         test.getFile();
17         test.getFile(Locale.JAPAN);
18     }
19     
20     public void getFile() {
21         Configuration freemarkerCfg = new Configuration();
22         freemarkerCfg.setClassForTemplateLoading(this.getClass(),"/");
23         freemarkerCfg.setEncoding(Locale.getDefault(),"UTF-8");
24         Template template;
25         try {
26             template = freemarkerCfg.getTemplate("t.ftl");
27             template.setEncoding("UTF-8");
28             File htmlFile = new File("t.html");
29             Writer out = new BufferedWriter(new OutputStreamWriter(
30                     new FileOutputStream(htmlFile), "UTF-8"));
31             HashMap propMap = new HashMap();
32             propMap.put("user""hermit");
33             template.process(propMap, out);
34             out.flush();
35         } catch (Exception e) {
36             e.printStackTrace();
37         }
38     }
39     
40     public void getFile(Locale loc) {
41         Configuration freemarkerCfg = new Configuration();
42         freemarkerCfg.setClassForTemplateLoading(this.getClass(),"/");
43         freemarkerCfg.setEncoding(Locale.getDefault(),"UTF-8");
44         Template template;
45         try {
46             template = freemarkerCfg.getTemplate("t.ftl",loc);
47             template.setEncoding("UTF-8");
48             File htmlFile = new File("t_"+loc.getLanguage()+"_"+loc.getCountry()+".html");
49             Writer out = new BufferedWriter(new OutputStreamWriter(
50                     new FileOutputStream(htmlFile), "UTF-8"));
51             HashMap propMap = new HashMap();
52             propMap.put("user""hermit");
53             template.process(propMap, out);
54             out.flush();
55         } catch (Exception e) {
56             e.printStackTrace();
57         }
58     }
59 
60 }
61 


t.ftl
<html>
<head>
  
<title>Welcome!</title>
  
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
</head>
<body>
  
<h1>Welcome ${user}!</h1>
</body>
</html>  


t_zh_CN.ftl
<html>
<head>
  
<title>欢迎!</title>
  
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
</head>
<body>
  
<h1>你好 ${user}!</h1>
</body>
</html>  


freemarker支持多语言国际化,只要把模板名称安装资源文件的写法就可以了,也就是name_语言_国家地区.ftl

如果找不到对应的语言,就会用默认语言的模板。

顺便抱怨一下,freemarker对于空值的处理太霸道了,没有值你就写个空或者写KEY也可以啊,弄一堆出错信息在那。。。。。。。。。。。。。。
posted on 2007-05-08 15:23 交口称赞 阅读(3664) 评论(5)  编辑  收藏 所属分类: freemarker

FeedBack:
# re: 用freemarker生产静态页面文件,支持多语言 2007-05-08 15:35 killer->
顺便抱怨一下,freemarker对于空值的处理太霸道了,没有值你就写个空或者写KEY也可以啊,弄一堆出错信息在那。。。。。。。。。。。。。。

这正是我喜欢freemarker的地方不像velocity那样, 什么反应也没有,错了都不知道.  回复  更多评论
  
# re: 用freemarker生产静态页面文件,支持多语言 2007-05-08 15:37 交口称赞
呵呵,我倒是希望他把KEY值留在那,就像做RCP开发时候那样。
这样比较好。。。。。。

留空确实不好。。。。  回复  更多评论
  
# re: 用freemarker生产静态页面文件,支持多语言 2007-05-09 00:05 yadan
加上个! 后面还可以带任何默认值 很喜欢这种方式。   回复  更多评论
  
# re: 用freemarker生产静态页面文件,支持多语言 2009-03-09 18:34 097
你好,请问用FREEMARKER在WEB程序中生产静态页面模板文件怎么获取?  回复  更多评论
  
# re: 用freemarker生产静态页面文件,支持多语言 2011-11-22 09:16 q
谢谢楼主,正好解决了我的问题。  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: