can struts read hebrew from property files?

Discussion in 'Java' started by Elhanan, Dec 10, 2005.

  1. Elhanan

    Elhanan Guest

    hi..

    my jsp and property files are encoded in UTF-8:

    if i use this:

    <%@ taglib uri="/tags/struts-bean" prefix="bean" %>
    <html>
    <head>

    </head>

    <body>נסיון
    <h3><bean:message key="welcome.heading"/></h3>
    </body>
    </html>

    i get my hebrew in gibbirsh, but changing the encoding setting in
    explorer corrects this: (using meta didn't help).
    however if i add
    <%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"
    %>

    to the jsp , i get gibbrish value from the bean tag, no matter what i
    do.
    Elhanan, Dec 10, 2005
    #1
    1. Advertising

  2. "Elhanan" <> wrote:

    > my jsp and property files are encoded in UTF-8:


    > if i use this:


    > <%@ taglib uri="/tags/struts-bean" prefix="bean" %>
    > <html>
    > <head>
    >
    > </head>
    >
    > <body>?????
    > <h3><bean:message key="welcome.heading"/></h3>
    > </body>
    > </html>


    > i get my hebrew in gibbirsh, but changing the encoding setting in
    > explorer corrects this: (using meta didn't help).
    > however if i add
    > <%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"
    > %>


    > to the jsp , i get gibbrish value from the bean tag, no matter what i
    > do.



    I would recommend to avoid the problematic non-ASCII characters altogether
    (both in HTML and in properties files) and instead use the HTML or Java
    escape syntax to write the hebrew characters as hexadecimal codes.
    In HTML files like this:
    <body>ןויםנ
    In properties files (and in Java source code) like this:
    key=value\u05df\u05d5\u05d9\u05dd\u05e0
    This strategy makes your HTML and properties totally independent of
    encoding, thus taking you to the safe side.

    (You can get the hebrew unicode chart from <http://www.unicode.org/charts/>)
    --
    "TFritsch$t-online:de".replace(':','.').replace('$','@')
    Thomas Fritsch, Dec 10, 2005
    #2
    1. Advertising

  3. Elhanan

    Elhanan Guest

    actually i've just read about a utility in java called native2ascii
    which converts files to such charachters, i tried, and did a simple
    java application of a property class reading such a file, and altough
    the get property returned a readible string, i still got gibbrish in
    jsp version.

    remember that if i try my page with out the page directive it defaults
    to western encoding, but i can still turn it manually, if i add the
    page directive, it confuses it.
    Elhanan, Dec 11, 2005
    #3
  4. Elhanan

    Roedy Green Guest

    On 10 Dec 2005 23:02:58 -0800, "Elhanan" <> wrote,
    quoted or indirectly quoted someone who said :

    >actually i've just read about a utility in java called native2ascii


    Let's look at your *.properties file first.

    quoting from http://mindprod.com/jgloss/properties.html

    In contrast to the natively generated system properties, ordinary user
    Properties are usually loaded into RAM in their entirety and indexed
    for rapid Hashtable access and are later saved to flat files on disk
    with the *.properties extension. Properties files on disk are similar
    to the old Windows 3.1 INI files, except they have no […] sections.
    They are text files encoded with ISO 8859-1 keyword=value pairs. You
    can get additional characters with \uxxxx escapes or with \t, \r, \n,
    or \f. Comments begin with #. Lines may be continued by ending them in
    a trailing \.

    NOTE: you must use 8859-1 encoding for your *.properties file. I
    don't know if you encode your strings left to right or right to left.

    Dump your strings out on the console in hex so you can tell precisely
    what you have in each slot.

    /** convert a String to a hex representation of the String,
    * with 4 hex chars per char of the original String.
    * e.g. "1abc \uabcd" gives "0031_0061_0062_0063_0020_abcd"
    * @param s String to convert to hex equivalent
    */
    public static String displayHexString ( String s )
    {
    StringBuilder sb = new StringBuilder( s.length() * 5 - 1 );
    for ( int i=0; i<s.length(); i++ )
    {
    char c = s.charAt(i);
    if ( i != 0 )
    {
    sb.append( '_' );
    }
    // encode 16 bits as four nibbles

    sb.append( hexChar [ c >>> 12 & 0xf ] );
    sb.append( hexChar [ c >>> 8 & 0xf ] );
    sb.append( hexChar [ c >>> 4 & 0xf ] );
    sb.append( hexChar [ c & 0xf ] );
    }
    return sb.toString();
    }

    // table to convert a nibble to a hex char.
    static final char[] hexChar = {
    '0' , '1' , '2' , '3' ,
    '4' , '5' , '6' , '7' ,
    '8' , '9' , 'a' , 'b' ,
    'c' , 'd' , 'e' , 'f'};

    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Dec 11, 2005
    #4
  5. Elhanan

    Elhanan Guest

    i think this program does what native2ascii program does, would like me
    to send you the result from native2ascii?
    Elhanan, Dec 11, 2005
    #5
  6. Elhanan

    Roedy Green Guest

    On 11 Dec 2005 06:22:21 -0800, "Elhanan" <> wrote,
    quoted or indirectly quoted someone who said :

    >i think this program does what native2ascii program does, would like me
    >to send you the result from native2ascii?


    Post your properties file. If all is well it will be expressed in
    7-bit ASCII so your newsreader encoding won't matter.

    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Dec 11, 2005
    #6
  7. Elhanan

    Elhanan Guest

    here it is with the \u and all

    # -- validator --
    bean.title=\u05f3\u00a0\u05f3\u00a1\u05f3\u2122\u05f3\u2022\u05f3\ufffd

    bean.body= \u05f3\u2019\u05f3\u2022\u05f3\u00a3
    errors.required={0} \u05f3\u201c\u05f3\u00a8\u05f3\u2022\u05f3\u00a9
    # -- welcome --
    welcome.title=Struts Blank Application
    welcome.heading=\u05e9\u05dc\u05d5\u05dd

    the one i'm aiming for is 'welcome.heading'
    Elhanan, Dec 12, 2005
    #7
  8. Elhanan

    Roedy Green Guest

    On 11 Dec 2005 20:54:19 -0800, "Elhanan" <> wrote,
    quoted or indirectly quoted someone who said :

    >here it is with the \u and all
    >
    ># -- validator --
    >bean.title=\u05f3\u00a0\u05f3\u00a1\u05f3\u2122\u05f3\u2022\u05f3\ufffd
    >
    >bean.body= \u05f3\u2019\u05f3\u2022\u05f3\u00a3
    >errors.required={0} \u05f3\u201c\u05f3\u00a8\u05f3\u2022\u05f3\u00a9
    ># -- welcome --
    >welcome.title=Struts Blank Application
    >welcome.heading=\u05e9\u05dc\u05d5\u05dd
    >
    >the one i'm aiming for is 'welcome.heading'


    I wrote this little SSCCE to read your file. It seems to be reading it
    correctly under Win2K with JDK 1.5.0_06.

    import java.io.FileInputStream;
    import java.io.IOException;
    import java.util.Properties;

    public class Hebrew
    {
    /**
    * test harness
    *
    * @param args not used
    */
    public static void main ( String[] args )
    {

    // get an ordinary property
    Properties props = new Properties();
    try
    {
    FileInputStream fis = new FileInputStream(
    "hebrew.properties" );
    props.load( fis );
    fis.close();
    }
    catch ( IOException e )
    {
    return;
    }
    String desc = props.getProperty( "welcome.heading" );
    System.out.println( desc );
    System.out.println( displayHexString( desc ) );

    }

    /** convert a String to a hex representation of the String,
    * with 4 hex chars per char of the original String.
    * e.g. "1abc \uabcd" gives "0031_0061_0062_0063_0020_abcd"
    * @param s String to convert to hex equivalent
    */
    public static String displayHexString ( String s )
    {
    StringBuilder sb = new StringBuilder( s.length() * 5 - 1 );
    for ( int i=0; i<s.length(); i++ )
    {
    char c = s.charAt(i);
    if ( i != 0 )
    {
    sb.append( '_' );
    }
    // encode 16 bits as four nibbles

    sb.append( hexChar [ c >>> 12 & 0xf ] );
    sb.append( hexChar [ c >>> 8 & 0xf ] );
    sb.append( hexChar [ c >>> 4 & 0xf ] );
    sb.append( hexChar [ c & 0xf ] );
    }
    return sb.toString();
    }

    // table to convert a nibble to a hex char.
    static final char[] hexChar = {
    '0' , '1' , '2' , '3' ,
    '4' , '5' , '6' , '7' ,
    '8' , '9' , 'a' , 'b' ,
    'c' , 'd' , 'e' , 'f'};
    }
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Dec 12, 2005
    #8
  9. Elhanan

    Elhanan Guest

    i know it reads it correctly i also did a small application only i
    simply displayed the results from the properties class on the eclipse
    console, and it was correct, in the struts it doesn't work.
    Elhanan, Dec 12, 2005
    #9
  10. Elhanan wrote:
    > my jsp and property files are encoded in UTF-8:


    Properties files cannot be in UTF8. However, there is
    a quick and dirty hack to allow this: Create a wrapper
    which reads the String, gets the byte array of the
    string, then re-parses the byte array as a string of
    UTF8 characters,

    Regards,

    Richard
    Richard Wheeldon, Dec 15, 2005
    #10
  11. Elhanan

    Elhanan Guest

    you mean extend the MessageResource class? i tried doing that , but
    struts ignore me.
    Elhanan, Dec 15, 2005
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Efy.
    Replies:
    2
    Views:
    1,080
  2. Mr. x

    sample of configuration for Hebrew

    Mr. x, Nov 4, 2003, in forum: ASP .Net
    Replies:
    5
    Views:
    2,587
    Mr. x
    Nov 5, 2003
  3. Sharon

    hebrew output

    Sharon, Jan 22, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    378
    Sharon
    Jan 22, 2004
  4. Katie Wright
    Replies:
    8
    Views:
    4,200
    Andrea Desole
    Jan 7, 2005
  5. John M
    Replies:
    1
    Views:
    733
Loading...

Share This Page