7

I am trying to use i18n in java for devanagari/hindi using sample ttf files from internet.

I am able to load resource bundle entries and also load the ttf and set font but it will not render jlabel as desired. It shows blocks in place of characters. If I debug in eclipse I can hover over the unicode variable and it even renders devanagari. Below is code and resource bundle for reference.

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");
    }

}

Here is resource bundle for MyNumbersrb_hi.properties I created.

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
Abhinav Sarkar
  • 23,534
  • 11
  • 81
  • 97
Miten
  • 356
  • 7
  • 23

4 Answers4

0

try with this one https://stackoverflow.com/a/6995374/466250 as the original question says property files are ISO-8859-1 by default.

Community
  • 1
  • 1
jorgeu
  • 689
  • 6
  • 14
  • I already use \u sequence in properties files for non-ascii characters so I do not need to do any conversion. The file is plain ascii file with \u sequence and unicode variable is able to render itself fine in debug mode but it just that swing JLabel is not rendering it. – Miten Sep 25 '12 at 04:33
0

Just do not set the font on label for unicode and default font is able to render it fine.

Miten
  • 356
  • 7
  • 23
0

Try running the SymbolText applet, select the 900 range, and select the font you are trying to use. Compare the results with selecting a standard font such as Devanagari MT. There may be an incompatibility between your version of the font and the TrueType implementation on your JVM.

Try calling getFontName(), getNumGlyphs(), canDisplay() and canDisplayUpTo() to verify the font you've loaded is what you expect.

Since you know Eclipse can render Devanagari, try to identify and use the font Eclipse uses, if necessary.

Steven McGrath
  • 1,717
  • 11
  • 20
0

Load Resource with utf-8

ResourceBundle messages=ResourceBundle.getBundle("resources/MenuBarResources",locale,new 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;
}
}
Dipak
  • 1