如何使用 Java 读取希腊语属性文件

发布于 2024-09-04 03:43:36 字数 1698 浏览 1 评论 0原文

我正在尝试从具有英语和英文键的属性文件中读取内容。希腊语值。我的代码如下:

public class I18NSample {

   static public void main(String[] args) {

      String language;
      String country;

      if (args.length != 2) {
          language = new String("el");
          country = new String("GR");
      } else {
          language = new String(args[0]);
          country = new String(args[1]);
      }

      Locale currentLocale;
      ResourceBundle messages;

      currentLocale = new Locale(language, country);

      messages =
        ResourceBundle.getBundle("MessagesBundle",currentLocale, new CustomClassLoader("E:\\properties"));

      System.out.println(messages.getString("greetings"));
      System.out.println(messages.getString("inquiry"));
      System.out.println(messages.getString("farewell"));
   }
}

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;


public class CustomClassLoader extends ClassLoader {

    private String path;



    public CustomClassLoader(String path) {
        super();
        this.path = path;
    }



    @Override
    protected URL findResource(String name) {
        File f = new File(path + File.separator + name);
        try {
            return f.toURL();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        return super.findResource(name);
    }

}

MessagesBundle_el_GR.properties 问候=ρήμ。 χαιρετώ 告别=επιφ。 αντίο

询问=τικάνεισ,τικάνετε

我像这样编译javac -encoding UTF8 CustomClassLoader.java javac -encoding UTF8 I18Sample.java

当我运行此程序时,我得到乱码输出。如果属性文件是英语、法语或德语,则它可以正常工作。 请帮忙。 问候, 苏本杜

I am trying to read from a properties file which have keys in English & values in greek.My code is like this:

public class I18NSample {

   static public void main(String[] args) {

      String language;
      String country;

      if (args.length != 2) {
          language = new String("el");
          country = new String("GR");
      } else {
          language = new String(args[0]);
          country = new String(args[1]);
      }

      Locale currentLocale;
      ResourceBundle messages;

      currentLocale = new Locale(language, country);

      messages =
        ResourceBundle.getBundle("MessagesBundle",currentLocale, new CustomClassLoader("E:\\properties"));

      System.out.println(messages.getString("greetings"));
      System.out.println(messages.getString("inquiry"));
      System.out.println(messages.getString("farewell"));
   }
}

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;


public class CustomClassLoader extends ClassLoader {

    private String path;



    public CustomClassLoader(String path) {
        super();
        this.path = path;
    }



    @Override
    protected URL findResource(String name) {
        File f = new File(path + File.separator + name);
        try {
            return f.toURL();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        return super.findResource(name);
    }

}

MessagesBundle_el_GR.properties
greetings=ρήμ. χαιρετώ
farewell=επιφ. αντίο

inquiry=τι κάνεισ, τι κάνετε

I am compiling like this javac -encoding UTF8 CustomClassLoader.java
javac -encoding UTF8 I18Sample.java

When I run this I get garbled output.If the properies file is in English,French or German it works fine.
Please help.
Regards,
Subhendu

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫九 2024-09-11 03:43:36

根据文档,出现属性文件仅限于 ISO 8859-1。然而,它们确实支持 Unicode 转义,并且链接的文档有一个工具可以自动生成所述转义。

XML 加载器允许使用 UTF-8 编码的 XML。

javac 选项仅适用于 .java 文件中的字符串文字,对程序的运行时行为不执行任何操作。

As per the documentation, properties files appear to be restricted to ISO 8859-1. They do however support Unicode escapes, and the linked documentation has a tool to produce said escapes automatically.

The XML loader allows for UTF-8 encoded XML.

The javac options only apply to string literals in the .java file, and do nothing for the runtime behavior of the program.

著墨染雨君画夕 2024-09-11 03:43:36

感谢剧院。

我尝试直接从属性文件中读取,如下所示:

FileInputStream fis = new FileInputStream("E:\\properties\\MessagesBundle_el_GR.properties");
        DataInputStream dis = new DataInputStream(fis);
        BufferedReader br = new BufferedReader(new InputStreamReader(dis, "ISO-8859-7"));
        String strLine;
        while((strLine = br.readLine()) != null){
            System.out.println(strLine);
        }

        dis.close();

它没有帮助。

我们可以将相同的信息放入数据库中从那里读取。虽然在安装 mysql 数据库的机器的终端中,当我们尝试从 java 代码中读取相同的信息时,我们会正确地以希腊语显示消息,但它会变成乱码。javac 编码 UTF8 对阅读有帮助吗?来自数据库的消息?

native2ascii 工具不会有太大帮助,因为我们系统的用户不会同意承担所有这些痛苦。用户已准备好使用数据库(因为我们将提供用于进入数据库的 GUI)或属性文件。

问候,


苏本杜

Thanks theatrus.

I tried to directly read from properties file like this :

FileInputStream fis = new FileInputStream("E:\\properties\\MessagesBundle_el_GR.properties");
        DataInputStream dis = new DataInputStream(fis);
        BufferedReader br = new BufferedReader(new InputStreamReader(dis, "ISO-8859-7"));
        String strLine;
        while((strLine = br.readLine()) != null){
            System.out.println(strLine);
        }

        dis.close();

It did not help.

We can put the same information in database & read from there.Though in the terminal of the machine where mysql database is installed we get the message displayed in Greek properly when we try to read the same from java code it becomes garbled.Will the javac -encoding UTF8 be of help for reading the message from database?

The native2ascii tool will not be of much help as user of our system will not agree to take all that pain.The user is ready to use database( as we will be providing a GUI for entry into database) or properties file.

Regards,


Subhendu

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文