Home > Blockchain >  How I can convert message bundle properties files to READABLE text in UTF-8 format?
How I can convert message bundle properties files to READABLE text in UTF-8 format?

Time:07-22

https://github.com/essiembre/eclipse-rbe/issues/83

eclipse-rbe

In Eclipse exist file: messagesBundle_ru_RU.properties

#Generated by ResourceBundle Editor (http://essiembre.github.io/eclipse-rbe/)
#Generated by ResourceBundle Editor (http://eclipse-rbe.sourceforge.net)
#Created by JInto - www.guh-software.de
#Sun Nov 18 17:19:12 EET 2012

ABS = \u0410\u0411\u0421

About = \u041E \u043F\u0440\u043E\u0433\u0440\u0430\u043C\u043C\u0435

Add = \u0414\u043E\u0431\u0430\u0432\u043B\u0435\u043D\u0438\u0435 \u0437\u0430\u043F\u0438\u0441\u0438

Add_Condition = \u0414\u043E\u0431\u0430\u0432\u0438\u0442\u044C \u0443\u0441\u043B\u043E\u0432\u0438\u0435

Additional = \u0414\u043E\u043F\u043E\u043B\u043D\u0438\u0442\u0435\u043B\u044C\u043D\u043E

How I can convert this to READABLE text in UTF-8 format?

CodePudding user response:

This is a properties file, and when it was saved to an OutputStream, any character outside of the ISO-8859-1 character set was replaced with a Unicode escape. The Properties.load(InputStream) method will decode this for you. You can then save the properties to a new file, specifying UTF-8 encoding.

static void transcodeProperties(Path src, Path dst) throws IOException {
    Properties properties = new Properties();
    try (InputStream fis = Files.newInputStream(src);
        BufferedInputStream is = new BufferedInputStream(fis)) {
        properties.load(is);
    }
    try (Writer w = Files.newBufferedWriter(dst, StandardCharsets.UTF_8, StandardOpenOption.CREATE_NEW)) {
        properties.store(w, null);
    }
}

CodePudding user response:

I created a method of doing what you asked. Keep in mind, I used methods from Apache. I extracted the required methods that way you will not be forced to use that library if you do not want to.

    public static void translateFileUnicode(File input, File output) {
        LinkedList<String> result = new LinkedList<>();
        try {
            BufferedReader reader = new BufferedReader(new FileReader(input));
            String temp = reader.readLine();
            while (temp != null) {
                result.add(translate(temp));
                temp = reader.readLine();
            }
            reader.close();

            BufferedWriter writer = new BufferedWriter(new FileWriter(output));

            for (String str : result) {
                writer.write(str   '\n');
            }
            writer.flush();
            writer.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static String translate(final CharSequence input) {
        if (input == null) {
            return null;
        }
        try {
            final StringWriter writer = new StringWriter(input.length() * 2);
            translate(input, writer);
            return writer.toString();
        } catch (final IOException ioe) {
            throw new UncheckedIOException(ioe);
        }
    }

    public static void translate(final CharSequence input, final Writer writer) throws IOException {
        Objects.requireNonNull(writer, "writer");
        if (input == null) {
            return;
        }
        int pos = 0;
        final int len = input.length();
        while (pos < len) {
            final int consumed = translateUnicode(input, pos, writer);
            if (consumed == 0) {
                final char c1 = input.charAt(pos);
                writer.write(c1);
                pos  ;
                if (Character.isHighSurrogate(c1) && pos < len) {
                    final char c2 = input.charAt(pos);
                    if (Character.isLowSurrogate(c2)) {
                        writer.write(c2);
                        pos  ;
                    }
                }
                continue;
            }
            for (int pt = 0; pt < consumed; pt  ) {
                pos  = Character.charCount(Character.codePointAt(input, pos));
            }
        }
    }

    public static int translateUnicode(final CharSequence input, final int index, final Writer out) throws IOException {
        if (input.charAt(index) == '\\' && index   1 < input.length() && input.charAt(index   1) == 'u') {
            int i = 2;
            while (index   i < input.length() && input.charAt(index   i) == 'u') {
                i  ;
            }

            if (index   i < input.length() && input.charAt(index   i) == ' ') {
                i  ;
            }

            if (index   i   4 <= input.length()) {
                final CharSequence unicode = input.subSequence(index   i, index   i   4);

                try {
                    final int value = Integer.parseInt(unicode.toString(), 16);
                    out.write((char) value);
                } catch (final NumberFormatException nfe) {
                    throw new IllegalArgumentException("Unable to parse unicode value: "   unicode, nfe);
                }
                return i   4;
            }
            throw new IllegalArgumentException("Less than 4 hex digits in unicode value: '"   input.subSequence(index, input.length())
                      "' due to end of CharSequence");
        }
        return 0;
    }

If you wish to translate the unicode characters from one file into another all at once you can use the translateFileUnicode(File, File) method. If you wish to translate a single String you can use the translate(CharSequence) method. I hope this is what you were looking for.

  • Related