2010-06-23 73 views
1

目標國際化:我想用jsf`s國際化問題與JSF 2.0(烏克蘭和俄羅斯)

場景:

創建資源包(UTF-8)

文件的信息:

使用它
file -I ./messages.properties 
./messages.properties: text/plain; charset=utf-8 

通過

面-配置:

<application> 
     <locale-config> 
      <default-locale>uk_UA</default-locale> 
      <supported-locale>en_US</supported-locale> 
      <supported-locale>ru_RU</supported-locale> 
     </locale-config>   
     <resource-bundle> 
      <base-name>ua.eset.oasys.hydra.i18n.messages</base-name> 
      <var>i18n</var> 
     </resource-bundle> 
    </application> 
一些的index.xhtml

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
     xmlns:h="http://java.sun.com/jsf/html" 
     xmlns:f="http://java.sun.com/jsf/core" 
     xmlns:ui="http://java.sun.com/jsf/facelets" 
     xmlns:vt="http://java.sun.com/jsf/composite/security"> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 
</head> 
<body> 

<ui:composition template="layout/template.xhtml"> 
    <ui:define name="top"> 
     <h:form> 
      <h:panelGrid border="4"> 

... 
       <f:view>      
        <h:commandButton value="#{i18n['logout']}" action="#{securityBacking.logout}"/> 
       </f:view> 
      </h:panelGrid> 
     </h:form> 
    </ui:define> 

    ... 

</ui:composition> 

</body> 
</html> 

問題: 在結果我得到錯誤編碼的那些按鈕的文本。

我試圖用native2asciiin行家

的pom.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns="http://maven.apache.org/POM/4.0.0" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <parent> 
     <artifactId>oasys</artifactId> 
     <groupId>ua.co.oasys</groupId> 
     <version>1.0</version> 
    </parent> 

    <groupId>ua.co.oasys</groupId> 
    <artifactId>hydra</artifactId> 
    <packaging>war</packaging> 
    <name>Hydra</name> 


    <properties> 
     <project.build.sourceEncoding> 
      UTF-8 
     </project.build.sourceEncoding> 

     <project.reporting.outputEncoding> 
      UTF-8 
     </project.reporting.outputEncoding> 
    </properties> 


    <dependencies> 

     <!-- SL4J API --> 
     <dependency> 
      <groupId>org.slf4j</groupId> 
      <artifactId>slf4j-api</artifactId> 
      <version>1.6.0</version> 
     </dependency> 

     <!-- SLF4J JDK14 Binding --> 
     <dependency> 
      <groupId>org.slf4j</groupId> 
      <artifactId>slf4j-jdk14</artifactId> 
      <version>1.6.0</version> 
     </dependency> 

     <!-- Injectable Weld-Logger --> 
     <dependency> 
      <groupId>org.jboss.weld</groupId> 
      <artifactId>weld-logger</artifactId> 
      <version>1.0.0-CR2</version> 
     </dependency> 

     <!--<dependency>--> 
     <!--<groupId>org.jboss.weld</groupId>--> 
     <!--<artifactId>weld-extensions</artifactId>--> 
     <!--<version>1.0.0.Alpha2</version>--> 
     <!--</dependency>--> 
     <!--<dependency>--> 
     <!--<groupId>org.jboss.weld</groupId>--> 
     <!--<artifactId>weld-api</artifactId>--> 
     <!--<version>1.0-CR4</version>--> 
     <!--</dependency>--> 


     <dependency> 
      <groupId>javax.enterprise</groupId> 
      <artifactId>cdi-api</artifactId> 
      <scope>provided</scope> 
      <version>1.0-CR1</version> 
     </dependency> 
     <dependency> 
      <groupId>javax.faces</groupId> 
      <artifactId>jsf-api</artifactId> 
      <version>2.0</version> 
      <scope>provided</scope> 
     </dependency> 
     <dependency> 
      <groupId>javax</groupId> 
      <artifactId>javaee-api</artifactId> 
      <version>6.0</version> 
      <scope>provided</scope> 
     </dependency> 

     <dependency> 
      <groupId>org.eclipse.persistence</groupId> 
      <artifactId>javax.persistence</artifactId> 
      <version>2.0.0</version> 
      <scope>provided</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.glassfish</groupId> 
      <artifactId>javax.ejb</artifactId> 
      <version>3.0</version> 
      <scope>provided</scope> 
     </dependency> 

     <dependency> 
      <groupId>org.glassfish</groupId> 
      <artifactId>javax.servlet</artifactId> 
      <version>3.0-b70</version> 
      <type>jar</type> 
      <scope>provided</scope> 
     </dependency> 
    </dependencies> 

    <build> 
     <finalName>hydra</finalName> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>2.0.2</version> 
       <configuration> 
        <encoding>utf8</encoding> 
        <source>1.6</source> 
        <target>1.6</target> 
       </configuration> 
      </plugin> 
      <plugin> 
       <groupId>org.codehaus.mojo</groupId> 
       <artifactId>native2ascii-maven-plugin</artifactId> 
       <version>1.0-alpha-1</version> 
       <configuration> 
        <dest>target/classes</dest> 
        <src>src/main/resources</src> 
       </configuration> 
       <executions> 
        <execution> 
         <id>native2ascii-utf8</id> 
         <goals> 
          <goal>native2ascii</goal> 
         </goals> 
         <!-- specific configurations --> 
         <configuration> 
          <!--<encoding>UTF8</encoding>--> 
          <tasks> 
           <native2ascii encoding="UTF-8" 
               src="." 
               dest="src/main/resources" includes="**/*.properties"> 
            <mapper type="glob" from="*.properties.utf8" 
              to="*.properties"/> 
           </native2ascii> 
          </tasks> 
         </configuration> 
        </execution> 
       </executions> 
      </plugin> 
     </plugins> 
    </build> 

</project> 

我有不同的符號,但仍然是錯誤的。

String (value.getBytes("ISO-8859-1"),"UTF-8") ; 

Messages.java:

我通過傳遞消息用由我招

public class Messages { 
    private static final String BUNDLE_NAME = "ua.eset.oasys.hydra.i18n.messages"; 
    private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle(BUNDLE_NAME); 

    private Messages() { 

    } 

    public static String getString(String key) { 
     try { 
      String value = (String) RESOURCE_BUNDLE.getString(key); 
      try { 
        return new String (value.getBytes("ISO-8859-1"),"UTF-8") ; 
      } catch (UnsupportedEncodingException e) { 
        e.printStackTrace(); 
        return null; 
      } 
     } catch (MissingResourceException e) { 
       return '!' + key + '!'; 
     } 
    } 
} 

它是成功的,我得到了一個有效的文本,但它是醜陋的JSF使用。 。

信息: 我使用的GlassFish v3的Mac OSX版(所以defaulst編碼LATIN1或ISO-8859-1, - 沒有舒爾)

Q1:什麼可能是問題的原因(jsf i18n的編碼不好)? Q2:是否有可能通過maven或某些jsf期貨爲jsf做類似String(value.getBytes(「ISO-8859-1」),「UTF-8」)的技巧?

問題3:maven配置有什麼問題?

謝謝!

回答

1

Q1:病因文件系統上的文件的編碼。 Q2:我沒有找到用maven編碼這些文件的正確方法,但jetBrains Idea爲我做了一些技巧 - 在屬性中它有一個編碼選項(文件編碼) - 並且有配置「屬性的默認編碼文件「 - 將其設置爲UTF-8並設置爲真複選框」透明的本地到ASCII轉換「 使用此選項,所有工作都很完美,但它不是我想的正確方式,所以我將對正確的方式進行研究maven的native2ascii插件配置。

如果有人知道我的配置有什麼問題,請在Q3上回答。

謝謝!

1

我不確定Maven-2中的native2ascii應該如何工作,但java.util.ResourceBundle確實默認使用ISO-8859-1讀取資源。 native2ascii工具應該創建一組基於UTF-8編碼的ISO-8859-1編碼資源文件。我自己名都UTF-8性能的文件message_xx_XX.utf8.properties,然後使用命令native2ascii工具將其編碼爲message_xx_XX.properties如下:

native2ascii.exe -encoding UTF-8 text_xx_XX.utf8.properties text_xx_XX.properties 

這對我的作品。

參見:

+0

感謝您的回覆。看起來我有maven牀配置..需要做一些研究。我目前發現了一些不同的方式,但它不適合我。我的更新和答案。 – 2010-06-24 09:59:20