2012-09-21 21 views
7

我正在嘗試在java中使用i18n,用於使用來自Internet的示例ttf文件的devanagari/hindi。java中的devanagari i18n

我能夠加載資源包條目,並加載ttf和設置字體,但它不會根據需要呈現jlabel。它顯示塊代替字符。如果我在eclipse中調試,我可以將鼠標懸停在unicode變量上,它甚至可以呈現梵文。以下是代碼和資源包供參考。

package i18n; 

import java.awt.Font; 
import java.awt.GridLayout; 
import java.io.InputStream; 
import java.util.Locale; 
import java.util.ResourceBundle; 

import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 

public class MyNumbers extends JFrame { 
    private ResourceBundle rb; 
    private Font devanagariFont; 

    public MyNumbers (String language, String fontFile) { 
     loadResourceBundle(language); 
     loadFont(fontFile); 
     display(); 
    } 

    private void display() { 
     String unicode = null; 

     JPanel labels = new JPanel(new GridLayout(0,2)); 
     JLabel uni = null; 
     for(int i=0; i<=10; i++) { 
      unicode = rb.getString("" +i); 
      labels.add(new JLabel("" + i)); 
      labels.add(uni = new JLabel(unicode)); 
      uni.setFont(devanagariFont); 
     } 
     getContentPane().add(labels); 
     setDefaultCloseOperation(DISPOSE_ON_CLOSE); 
     pack(); 
     setVisible(true); 
    } 

    private void loadFont(String fontFile) { 
     try { 
      InputStream input = getClass().getResourceAsStream(fontFile); 
      Font b = Font.createFont(Font.TRUETYPE_FONT, input); 
      devanagariFont = b.deriveFont(Font.PLAIN, 11); 

     } catch(Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    private void loadResourceBundle(String language) { 
     String base = getClass().getName() + "rb"; 
     rb = ResourceBundle.getBundle(base, new Locale(language)); 

    } 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
     // TODO Auto-generated method stub 
     new MyNumbers("hi", "Devnew.ttf"); 
    } 

} 

這是我創建的MyNumbersrb_hi.properties資源包。

Default properties in Devnagari 
0=\u0915\u0916\u0917: 
1=\u090f\u0915: 
2=\u0926\u094b: 
3=\u0924\u0940\u0907: 
4=\u091a\u093e\u0930: 
5=\u092a\u093e\u091a: 
6=\u091b\u0947: 
7=\u0938\u093e\u0924: 
8=\u0906\u093e\u0920: 
9=\u0928\u094c: 
10=\u0926\u0938: 
random=Random 
title=Key in numbers to match the words 

回答

0

只是不要在unicode的標籤上設置字體,並且默認字體能夠渲染它。

0

嘗試用這一個https://stackoverflow.com/a/6995374/466250 因爲原來的問題說,財產文件是ISO-8859-1默認。

+0

我已經在非ASCII字符的屬性文件中使用\ u序列,所以我不需要做任何轉換。該文件是普通的ascii文件,具有\ u序列,並且unicode變量能夠在調試模式下呈現自身,但僅僅是這種擺動JLabel不會呈現它。 – Miten

0

嘗試運行SymbolText小程序,選擇900範圍,然後選擇您嘗試使用的字體。將結果與選擇一個標準字體比如天凡山MT進行比較。在您的JVM上,您的字體版本與TrueType實施可能不兼容。嘗試調用getFontName(),getNumGlyphs(),canDisplay()和canDisplayUpTo()來驗證你所加載的字體是你期望的。

因爲您知道Eclipse可以渲染梵文,所以如有必要,請嘗試識別並使用Eclipse使用的字體。

0

使用UTF-8

的ResourceBundle消息加載資源= ResourceBundle.getBundle( 「資源/ MenuBarResources」,語言環境,新UTF8Control());

public class UTF8Control extends Control { 
public ResourceBundle newBundle 
    (String baseName, Locale locale, String format, ClassLoader loader, boolean reload) 
     throws IllegalAccessException, InstantiationException, IOException 
{ 
    // The below is a copy of the default implementation. 
    String bundleName = toBundleName(baseName, locale); 
    String resourceName = toResourceName(bundleName, "properties"); 
    ResourceBundle bundle = null; 
    InputStream stream = null; 
    if (reload) { 
     URL url = loader.getResource(resourceName); 
     if (url != null) { 
      URLConnection connection = url.openConnection(); 
      if (connection != null) { 
       connection.setUseCaches(false); 
       stream = connection.getInputStream(); 
      } 
     } 
    } else { 
     stream = loader.getResourceAsStream(resourceName); 
    } 
    if (stream != null) { 
     try { 
      // Only this line is changed to make it to read properties files as UTF-8. 
      bundle = new PropertyResourceBundle(new InputStreamReader(stream, "UTF-8")); 
     } finally { 
      stream.close(); 
     } 
    } 
    return bundle; 
} 
}