Java Forum / General / December 2005
can struts read hebrew from property files?
Elhanan - 10 Dec 2005 21:58 GMT 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.
Thomas Fritsch - 10 Dec 2005 23:19 GMT > my jsp and property files are encoded in UTF-8:
> if i use this:
> <%@ taglib uri="/tags/struts-bean" prefix="bean" %> > <html> [quoted text clipped - 6 lines] > </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/>)
 Signature "TFritsch$t-online:de".replace(':','.').replace('$','@')
Elhanan - 11 Dec 2005 07:02 GMT 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.
Roedy Green - 11 Dec 2005 08:36 GMT >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'};
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Elhanan - 11 Dec 2005 14:22 GMT i think this program does what native2ascii program does, would like me to send you the result from native2ascii?
Roedy Green - 11 Dec 2005 20:53 GMT >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.
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Elhanan - 12 Dec 2005 04:54 GMT 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'
Roedy Green - 12 Dec 2005 06:47 GMT >here it is with the \u and all > [quoted text clipped - 8 lines] > >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'}; }
 Signature Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching.
Elhanan - 12 Dec 2005 22:12 GMT 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.
Richard Wheeldon - 15 Dec 2005 20:01 GMT > 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
Elhanan - 15 Dec 2005 22:01 GMT you mean extend the MessageResource class? i tried doing that , but struts ignore me.
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|