---
title: "Facelets如何进行i18n"
author: "aaronluo"
date: "2009-06-04T09:56:45Z"
tags: []
slug: "facelets-i18n"
draft: false
lastmod: "2009-06-04T09:56:45Z"
id: 401588
url: "https://aaronluo.iteye.com/blog/401588"
---

如果使用JSP作为JSF的视图，那么在每个也没可以用&lt;f:loadBundle&gt;加载国际化文件，但是使用Faclets如何进行国际化文件加载呢？我的做法如下

1\. 在faces-config.xml中增加如下配置：

```
<application>
	<view-handler>com.sun.facelets.FaceletViewHandler</view-handler>
	<locale-config>
		<default-locale>zh_CN</default-locale>
	</locale-config>
	<message-bundle>message</message-bundle>

</application>
```

 2. 在/WEB-INF/classes/下增加message.properties和message\_zh\_CN.properties两个文件，一个是默认资源文件，一个是中文资源文件。可以根据自己应用的需要添加其他语言支持。

3\. 在模版文件，比如/WEB-INF/facelets/template.xhtml中加载这个资源

```
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:h="http://java.sun.com/jsf/html">

<f:loadBundle basename="message" var="msg"/>

......

</html>
```

 4. 在使用这个模版的客户端文件中不必再进行加载，直接使用即可，例如welcome.xhtml

```
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:h="http://java.sun.com/jsf/html"
	template="/WEB-INF/facelets/template.xhtml">
	
<ui:define name="title">
${msg.app_title}

</ui:define>
....

</ui:composition>
```

 因为我使用的是MyFaces JSF 1.1的实现，据说JSF1.2可以完全在faces-config.xml中进行配置，有机会试试~~