【问题标题】:devanagari i18n in javajava中的梵文i18n
【发布时间】:2012-09-21 14:00:11
【问题描述】:

我正在尝试使用来自互联网的示例 ttf 文件在 java 中将 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

【问题讨论】:

    标签: java internationalization indic


    【解决方案1】:

    试试这个https://stackoverflow.com/a/6995374/466250 正如原始问题所说,属性文件默认为 ISO-8859-1。

    【讨论】:

    • 我已经在非 ascii 字符的属性文件中使用了 \u 序列,所以我不需要进行任何转换。该文件是带有 \u 序列的普通 ascii 文件,并且 unicode 变量能够在调试模式下很好地呈现自己,但只是 swing JLabel 没有呈现它。
    【解决方案2】:

    只是不要为unicode设置标签上的字体,默认字体可以很好地呈现它。

    【讨论】:

      【解决方案3】:

      尝试运行 SymbolText 小程序,选择 900 范围,然后选择您尝试使用的字体。将结果与选择标准字体(如 Devanagari MT)进行比较。您的字体版本与 JVM 上的 TrueType 实现可能不兼容。

      尝试调用 getFontName()、getNumGlyphs()、canDisplay() 和 canDisplayUpTo() 来验证您加载的字体是否符合预期。

      既然您知道 Eclipse 可以呈现梵文,请尝试识别和使用 Eclipse 使用的字体(如有必要)。

      【讨论】:

        【解决方案4】:

        使用 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;
        }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-04-10
          • 2011-01-05
          • 2018-02-11
          • 1970-01-01
          • 2017-10-30
          • 2020-02-02
          • 1970-01-01
          相关资源
          最近更新 更多