【问题标题】:Issue with i18n in JSF 2.0 (ukrainian & russian)JSF 2.0 中的 i18n 问题(乌克兰语和俄语)
【发布时间】:2011-03-07 09:22:03
【问题描述】:

目标:我想用jsf`s i18n

场景:

创建资源包 (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 maven

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(所以默认编码 latin1 或 ISO-8859-1,- 不舒尔。)

Q1:问题的原因可能是什么(jsf i18n 编码错误)? [关闭]

Q2:是否可以在 maven 或一些 jsf 期货的帮助下为 jsf 做类似 String (value.getBytes("ISO-8859-1"),"UTF-8") 的技巧?

Q3:maven 配置有什么问题?

谢谢!

【问题讨论】:

    标签: maven-2 encoding internationalization jsf-2


    【解决方案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
    

    这对我有用。

    另见

    【讨论】:

    • 感谢您的回复。看起来我有 maven 的床配置..需要对此进行一些研究。我目前找到了一些不同的方式,但它不够适合我。查看我的更新和答案。
    【解决方案2】:

    Q1:原因是文件系统上的文件编码。

    Q2:我没有找到用 maven 对这些文件进行编码的正确方法,但是 jetBrains Idea 为我做了一些技巧 - 在属性中它有一个编码选项(文件编码) - 并且有配置“属性文件的默认编码" - 将其设置为 UTF-8 并设置 ti true 复选框“透明的 native-to-ascii 转换” 有了这个选项,一切都很完美,但这不是我认为的正确方法,所以我将研究正确的 maven 的 native2ascii 插件配置。

    如果有人知道我的配置有什么问题,请在 Q3 回答。

    谢谢!

    【讨论】:

      猜你喜欢
      • 2021-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多