Ashik’s IT Thoughts

December 19, 2010

Computers With Attitude

Filed under: IT — ashikuzzaman @ 2:19 pm

I thought it’s funny but I believe all the major O/S vendors are already capable of creating something this now.

Small Java Program To Convert Unicode Character Filled Sentences To Native or UTF-8 Character Filled Sentences

Filed under: IT, Java, Official — ashikuzzaman @ 12:58 pm

My colleague John asked for a tool from me last week so that he can finish the translations / localization works that he was doing. To solve it in a lazy way, I did some google around a bit but couldn’t figure out any easy tool that will convert Unicode characters \uxxxx to native locales. There are plenty of tools available to convert natives to UTF-8 and even the below simple command works in java for native to utf-8 :

native2ascii.exe -encoding UTF-8 your-input-file-name.properties your-output-file-name.properties

So what I have done is just written a small java program that will help you to achieve the reverse of it i.e. converting from unicode to native / utf-8. Here are the steps you will need to perform.

package com.salesforce.test;

import java.io.*;

/**
* To compile: javac -d . SfdcUnicodeToNativeConverter.java
* To run: java com.salesforce.test.SfdcUnicodeToNativeConverter
*
* @author ashik
*/
public class SfdcUnicodeToNativeConverter {

private static String fileName = "output.properties";

private static String sentenceToConvert = "cmgt_configurator/Sfdc_common_summary_32=\u304a\u652f\u6255\u3044\u60c5\u5831\u306e\u5165\u529b";

/*
\u304a\u652f\u6255\u3044\u60c5\u5831\u306e\u5165\u529b
\u304a\u652f\u6255\u3044\u6761\u4ef6 & \u5951\u7d04\u671f\u9593\u306e\u7de8\u96c6
\u6b64\u8ba2\u5355\u7684\u603b\u4f63\u91d1+
\u82e5\u8981\u7e7c\u7e8c\u7d50\u5e33\uff0c\u60a8\u5fc5\u9808\u8a73\u95b1\u8b80\u4e26\u63a5\u53d7\u4ee5\u4e0b\u6240\u5217\u6bcf\u500b\u7522\u54c1\u7684\u689d\u6b3e\u3002
*/
public static void writeOutput(String str, String fileName) {
System.out.println("Unicode to Native Conversion Starts...");
try {
FileOutputStream fos = new FileOutputStream(fileName);
Writer out = new OutputStreamWriter(fos, "UTF8");
out.write(str);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Unicode to Native Conversion Successful!");
}

public static void main(String[] args) {
writeOutput(sentenceToConvert, fileName);
}

}

1. Make sure sure you have JDK installed in your system. Set the environment variable JAVA_HOME in to point to JDK (alternatively there is a shorter way that I can show you).
2. Copy the source java file from the following network location into your machine – \\moorea\departments\AppStore\Comergent\Programs\SfdcUnicodeToNativeConverter.java
3. Open the source file in TextPad or Crimson Editor and replace the value for the variable sentenceToConvert to the actual sentence that you are trying to translate / convert.
4. Compile the java file that I wrote using the command javac -d . SfdcUnicodeToNativeConverter.java
5. Run the program to generate the output file using the command java com.salesforce.test.SfdcUnicodeToNativeConverter
6. A new file named output.properties should be generated in the same folder from where you ran the program.
7. Open this file with Altova Xml Spy or Notepad with UTF-8 Encoding to preserve the file format correctly. You may see some characters that look like junk. Don’t worry, those are not junks.
8. Copy the content from the file in Xml Spy or Notepad and paste those into a new MS Excel Spreadsheet. Now you should see the native characters properly. You can use those for translation purposes I your models.

I have tried to write down the steps above clearly as you will need to perform those repeatedly for each line of translations for each locale. Being a little innovative, you can use the steps above for multi-line translations as well by adding proper \n” + ” to hold many properties / values in the sentenceToConvert variables.

Create a free website or blog at WordPress.com.