Freemarker的国际化
来源:互联网 发布:网络13邯大主教楼视频 编辑:程序博客网 时间:2024/05/16 06:08
FreeMarkerTest:
t.ftl
t_zh_CN.ftl
freemarker支持多语言国际化,只要把模板名称安装资源文件的写法就可以了,也就是name_语言_国家地区.ftl
如果找不到对应的语言,就会用默认语言的模板。
转帖地址:http://www.blogjava.net/vip01/archive/2007/05/08/115959.html
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
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!</< span>title>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
</< span>head>
<body>
<h1>Welcome ${user}!</< span>h1>
</< span>body>
</< span>html>
<head>
<title>Welcome!</< span>title>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
</< span>head>
<body>
<h1>Welcome ${user}!</< span>h1>
</< span>body>
</< span>html>
t_zh_CN.ftl
<html>
<head>
<title>欢迎!</< span>title>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
</< span>head>
<body>
<h1>你好 ${user}!</< span>h1>
</< span>body>
</< span>html>
<head>
<title>欢迎!</< span>title>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
</< span>head>
<body>
<h1>你好 ${user}!</< span>h1>
</< span>body>
</< span>html>
freemarker支持多语言国际化,只要把模板名称安装资源文件的写法就可以了,也就是name_语言_国家地区.ftl
如果找不到对应的语言,就会用默认语言的模板。
转帖地址:http://www.blogjava.net/vip01/archive/2007/05/08/115959.html
- Freemarker的国际化
- Spring MVC国际化和整合Freemarker的国际化
- Spring+Freemarker(国际化)
- Freemarker+Struts2 国际化应用
- spring+freemarker 国际化i18n
- SpringMVC + FreeMarker 国际化使用方法
- SpringMVC + Freemarker 国际化
- springboot+freemarker国际化
- 基于struts2与FreeMarker的动态切换国际化技术方案
- Freemarker自定义标签+spring国际化所碰到的坑
- Freemarker日期显示取消国际化
- 基于浏览器首选语言的springmvc和freemarker国际化配置的实现
- 基于浏览器首选语言的springmvc和freemarker国际化配置的实现
- Spring MVC+Freemarker+Javascript的多语言(国际化i18n/本地化)和主题(Theme)实现
- SpringMVC+FreeMarker+MyBatis框架搭建之国际化
- springmvc国际化+freemarker整合 一步步走来
- 国际化与Struts2的国际化
- 【国际化】Android包的国际化
- Windows 2008安装后配置技巧
- sqlserver2000及sp4补丁安装时的挂起问题
- http各个状态码含义
- membership 结合单点登录
- 一个分治算法的小程序
- Freemarker的国际化
- 体验好管家 Mybase
- 中文真伟大!竟然有只能看,不能读的文章
- WinForms UI Thread Invokes: An In-Depth Review of Invoke/BeginInvoke/InvokeRequred
- 快速找出DB2分区键设置不正确的表
- 在ORACLE中如何删除表中的重复数据的补充
- 击破struts 步步为营
- 到底去不去上海......
- 最佳PHP解析RSS类lastRSS