Java中的devanagari i18n [英] devanagari i18n in java

查看:72
本文介绍了Java中的devanagari i18n的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Internet上的示例ttf文件在Java中将i18n用于devanagari/hindi.

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

我能够加载资源束条目,还可以加载ttf并设置字体,但是它不会根据需要呈现jlabel.它显示了代替字符的块.如果我在eclipse中调试,则可以将鼠标悬停在unicode变量上,它甚至会呈现devanagari.下面是代码和资源包供参考.

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

}

这是我创建的MyNumbersrb_hi.properties的资源包.

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

推荐答案

请不要在标签上为unicode设置字体,并且默认字体可以很好地呈现该字体.

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

这篇关于Java中的devanagari i18n的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆