Java: How to write “Arabic” in properties file?

前端 未结 7 1973
自闭症患者
自闭症患者 2021-01-05 09:32

I want to write \"Arabic\" in the message resource bundle (properties) file but when I try to save it I get this error:

\"Save couldn\'t be completed Some characters

相关标签:
7条回答
  • 2021-01-05 09:57

    Properties-based resource bundles must be encoded in ISO-8859-1 to use the default loading mechanism, but I have successfully used this code to allow the properties files to be encoded in UTF-8:

    private static class ResourceControl extends ResourceBundle.Control {
        @Override
        public ResourceBundle newBundle(String baseName, Locale locale,
                String format, ClassLoader loader, boolean reload)
                throws IllegalAccessException, InstantiationException,
                IOException {
            String bundlename = toBundleName(baseName, locale);
            String resName = toResourceName(bundlename, "properties");
            InputStream stream = loader.getResourceAsStream(resName);
            return new PropertyResourceBundle(new InputStreamReader(stream,
                    "UTF-8"));
        }
    
    }
    

    Then of course you have to change the encoding of the file itself to UTF-8 in your IDE, and can use it like this:

    ResourceBundle bundle = ResourceBundle.getBundle(
        "package.Bundle", new ResourceControl());
    
    0 讨论(0)
  • 2021-01-05 10:06

    If you are using Eclipse, you can choose "Window-->Preferences" and then filter on "content types". Then you should be able to set the default encoding. There's a screen shot showing this at the top of this post.

    0 讨论(0)
  • 2021-01-05 10:07

    As described in the class reference for "Properties"

    The load(Reader) / store(Writer, String) methods load and store properties from and to a character based stream in a simple line-oriented format specified below. The load(InputStream) / store(OutputStream, String) methods work the same way as the load(Reader)/store(Writer, String) pair, except the input/output stream is encoded in ISO 8859-1 character encoding. Characters that cannot be directly represented in this encoding can be written using Unicode escapes ; only a single 'u' character is allowed in an escape sequence. The native2ascii tool can be used to convert property files to and from other character encodings.

    0 讨论(0)
  • 2021-01-05 10:09

    http://sourceforge.net/projects/eclipse-rbe/

    You can use the above plugin for eclipse IDE to make the Unicode conversion for you.

    0 讨论(0)
  • 2021-01-05 10:16

    This is mainly an editor configuration issue. If you're working in Windows, you can edit the text in an editor that supports UTF-8. Notepad or Eclipse built-in editor should be more than enough, provided you've saved file as UTF-8. In Linux, I've used gedit and emacs successfully. In Notepad, you can do this by clicking 'Save As' button and choosing 'UTF-8' encoding. Other editors should have similar feature. Some editors might require font change in order to display letters correctly, but it seems that you don't have this issue.

    Having said that, there are other steps to consider when performing i18n for arabic. You can find some useful links below. Make sure to use native2ascii on properties file before using it otherwise it might not work. I spent a lot of time until I figured this one out.

    Tomcat webapps

    Using nativ2ascii with properties files

    0 讨论(0)
  • 2021-01-05 10:18

    Besides native2ascii tool mentioned in other answers there is a java Open Source library that can provide conversion functionality to be used in code

    Library MgntUtils has a Utility that converts Strings in any language (including special characters and emojis to unicode sequence and vise versa:

    result = "Hello World";
    result = StringUnicodeEncoderDecoder.encodeStringToUnicodeSequence(result);
    System.out.println(result);
    result = StringUnicodeEncoderDecoder.decodeUnicodeSequenceToString(result);
    System.out.println(result);
    

    The output of this code is:

    \u0048\u0065\u006c\u006c\u006f\u0020\u0057\u006f\u0072\u006c\u0064
    Hello World
    

    The library can be found at Maven Central or at Github It comes as maven artifact and with sources and javadoc

    Here is javadoc for the class StringUnicodeEncoderDecoder

    0 讨论(0)
提交回复
热议问题