「Spring MVC 各国語対応」の版間の差分
ナビゲーションに移動
検索に移動
(ページの作成:「==Spring MVC 各国語対応== [Spring][Spring MVC] *http://d.hatena.ne.jp/GreenTea2010/20110529/1306692169 *https://jira.springsource.org/browse/SPR-6555 *http://vir…」) |
|||
| 1行目: | 1行目: | ||
==Spring MVC 各国語対応== | ==Spring MVC 各国語対応== | ||
| − | [Spring][Spring MVC] | + | [[Spring][Spring MVC]] |
*http://d.hatena.ne.jp/GreenTea2010/20110529/1306692169 | *http://d.hatena.ne.jp/GreenTea2010/20110529/1306692169 | ||
*https://jira.springsource.org/browse/SPR-6555 | *https://jira.springsource.org/browse/SPR-6555 | ||
| 10行目: | 10行目: | ||
*messages_en.properties | *messages_en.properties | ||
*messages_ja.properties | *messages_ja.properties | ||
| − | + | <blockquote>messages_ja.properties は native2ascii をかける</blockquote> | |
=====WEB-INF/i18n/messages_en.properties===== | =====WEB-INF/i18n/messages_en.properties===== | ||
label.firstname=First Name | label.firstname=First Name | ||
| 16行目: | 16行目: | ||
label.firstname=\u540d\u524d | label.firstname=\u540d\u524d | ||
====Spring設定ファイルにBeanの設定==== | ====Spring設定ファイルにBeanの設定==== | ||
| − | + | <?xml version="1.0" encoding="UTF-8"?> | |
| − | + | <beans xmlns="http://www.springframework.org/schema/beans" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns:mvc="http://www.springframework.org/schema/mvc" | xmlns:mvc="http://www.springframework.org/schema/mvc" | ||
| 23行目: | 23行目: | ||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd | xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd | ||
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd | http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd | ||
| − | http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd" | + | http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> |
| − | + | <mvc:annotation-driven/> | |
| − | + | <mvc:interceptors> | |
| − | + | <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"> | |
| − | + | <property name="paramName" value="lang" /> | |
| − | + | </bean> | |
| − | + | </mvc:interceptors> | |
| − | + | <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> | |
| − | + | <property name="basename" value="WEB-INF/i18n/messages"/> | |
| − | + | <property name="defaultEncoding" value="UTF-8" /> | |
| − | + | </bean> | |
| − | + | <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver"> | |
| − | + | <property name="cookieName" value="locale" /> | |
| − | + | </bean> | |
| − | + | </beans> | |
====JSPでtaglibの宣言==== | ====JSPでtaglibの宣言==== | ||
| − | + | <%@taglib uri="http://www.springframework.org/tags" prefix="spring"%> | |
====JSPで利用==== | ====JSPで利用==== | ||
=====出力===== | =====出力===== | ||
| − | + | <spring:message code="label.firstname"/> | |
=====結果を変数に入れる===== | =====結果を変数に入れる===== | ||
| − | + | <spring:message code="label.title.index" var="title" scope="page"/> | |
====タグライブラリが見つからないエラーの場合==== | ====タグライブラリが見つからないエラーの場合==== | ||
*org.springframework.web.servlet の /META-INF/spring.tld を /WEB-INF へコピー | *org.springframework.web.servlet の /META-INF/spring.tld を /WEB-INF へコピー | ||
*web.xml に taglib 情報を与える | *web.xml に taglib 情報を与える | ||
| − | + | <web-app> | |
: | : | ||
| − | + | <jsp-config> | |
| − | + | <taglib> | |
| − | + | <taglib-uri>http://www.springframework.org/tags</taglib-uri> | |
| − | + | <taglib-location>/WEB-INF/spring.tld</taglib-location> | |
| − | + | </taglib> | |
| − | + | </jsp-config> | |
2020年2月15日 (土) 08:05時点における版
目次
Spring MVC 各国語対応
[[Spring][Spring MVC]]
- http://d.hatena.ne.jp/GreenTea2010/20110529/1306692169
- https://jira.springsource.org/browse/SPR-6555
- http://viralpatel.net/blogs/2010/07/spring-3-mvc-internationalization-i18n-localization-tutorial-example.html
- http://static.springsource.org/spring/docs/1.1.5/taglib/tag/MessageTag.html
Eclipse での手順
プロパティファイルを作成
- messages_en.properties
- messages_ja.properties
<blockquote>messages_ja.properties は native2ascii をかける</blockquote>
WEB-INF/i18n/messages_en.properties
label.firstname=First Name
WEB-INF/i18n/messages_ja.properties
label.firstname=\u540d\u524d
Spring設定ファイルにBeanの設定
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <mvc:annotation-driven/> <mvc:interceptors> <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"> <property name="paramName" value="lang" /> </bean> </mvc:interceptors> <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> <property name="basename" value="WEB-INF/i18n/messages"/> <property name="defaultEncoding" value="UTF-8" /> </bean> <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver"> <property name="cookieName" value="locale" /> </bean> </beans>
JSPでtaglibの宣言
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
JSPで利用
出力
<spring:message code="label.firstname"/>
結果を変数に入れる
<spring:message code="label.title.index" var="title" scope="page"/>
タグライブラリが見つからないエラーの場合
- org.springframework.web.servlet の /META-INF/spring.tld を /WEB-INF へコピー
- web.xml に taglib 情報を与える
<web-app>
:
<jsp-config>
<taglib>
<taglib-uri>http://www.springframework.org/tags</taglib-uri>
<taglib-location>/WEB-INF/spring.tld</taglib-location>
</taglib>
</jsp-config>
© 2006 矢木浩人