Thursday, March 17, 2011

Java and Locale based formatting

Folks working on localization, often ends up in situations they need to see a list of what the Locale specific information looks like in Java. They need a sample of each locale and not just a simple list of supported locales.

Here's a simple implementation (+/-) thrown together after googling the basics of the Java API, that renders a table which has samples of localized in each and every locale supported by Java.

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Currency;
import java.util.Date;
import java.util.Locale;

public class CurrencyFormatterExample {
  
    private static PrintWriter pw;

    public static void format(Locale currentLocale) {
    Double price = new Double(1234.56);
    NumberFormat currencyFormatter;
    DateFormat df = DateFormat.getDateTimeInstance(DateFormat.LONG,
        DateFormat.MEDIUM, currentLocale);
    String currencyOut;
    currencyFormatter = NumberFormat.getCurrencyInstance(currentLocale);
    currencyOut = currencyFormatter.format(price);
    Currency currency = currencyFormatter.getCurrency();
    String dateFormat = df.format(new Date());
        pw.print("<tr><td>" + currentLocale.toString() + "</td>" +
                 "<td>" + currencyOut + "</td>" +
                 "<td>" + currency.getCurrencyCode() + "</td>" +
                 "<td>" + currency.getSymbol(currentLocale) + "</td>" +
                 "<td>" + unicodeEscape(currency.getSymbol(currentLocale)) + "</td>" +
                 "<td>" + dateFormat + "</td></tr>");
        System.out.println(currentLocale.toString() + "\t\t" + currencyOut + "\t\t" + dateFormat);
    }

    public static void main(String args[]) throws UnsupportedEncodingException {
    try {
        pw = new PrintWriter("format.html", "UTF-8");
        pw.print("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" /></head>");
        pw.print("<body><table>");
        pw.print("<tr><th>LOCALE</th>" +
                     "<th>FORMATTED PRICE</th>" +
                     "<th>CURRENCY CODE</th>" +
                     "<th>CURRENCY SYMBOL</th>" +
                     "<th>UNICODE ESCAPED CURRENCY SYMBOL</th>" +
                     "<th>FORMATTED DATE</th></tr>");
        Locale[] locales = Locale.getAvailableLocales();
        for (int i = 0; i < locales.length; i++) {
        CurrencyFormatterExample.format(locales[i]);
        }
        pw.print("</table></body></html?");
        pw.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    }

    private static final char[] hexChar = {
        '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
    };

    private static String unicodeEscape(String s) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < s.length(); i++) {
        char c = s.charAt(i);
        if ((c >> 7) > 0) {
        sb.append("\\u");
        sb.append(hexChar[(c >> 12) & 0xF]);
        sb.append(hexChar[(c >> 8) & 0xF]);
        sb.append(hexChar[(c >> 4) & 0xF]);
        sb.append(hexChar[c & 0xF]);
        }
        else {
        sb.append(c);
        }
    }
    return sb.toString();
    }

}

Often with such code, the printout on the java console in eclipse works fine but the populated dates, numbers and currencies in different languages, cannot be seen in the browser. I ripped of the Unicode escaping code after some googling to fix this issue. I don’t know how great the algorithm is but it does the job as far as I can tell.

Another unique feature of the generated table is that XXX is thrown in as a placeholder where ever the row represents a language and not a region so it does not have a currency associated with it.

LOCALEFORMATTED PRICECURRENCY CODECURRENCY SYMBOLUNICODE ESCAPED CURRENCY SYMBOLFORMATTED DATE
ja_JP¥1,235JPY\uFFE52010/10/22 10:53:17
es_PES/1.234,56PENS/S/22 de octubre de 2010 10:53:17 AM
en¤1,234.56XXXXXXXXXOctober 22, 2010 10:53:17 AM
ja_JP_JP¥1,235JPY\uFFE5H22.10.22 10:53:17
es_PAB1,234.56PABBB22 de octubre de 2010 10:53:17 AM
sr_BAКМ. 1.234,56BAMКМ.\u041A\u041C.22. октобар 2010. 10:53:17
mk¤ 1.234,56XXXXXXXXX22, октомври 2010 10:53:
es_GTQ1,234.56GTQQQ22 de octubre de 2010 10:53:17 AM
ar_AEد.إ.‏ 1,234.56AEDد.إ.‏\u062F.\u0625.\u200F22 أكتوبر, 2010 10:53:17 ص
no_NOkr 1 234,56NOKkrkr22. oktober 2010 10:53:17
sq_ALLek1.234,56ALLLekLek2010-10-22 10:53:17.PD
bg¤ 1 234,56XXXXXXXXXПетък, 2010, Октомври 22 10:53:17
ar_IQد.ع.‏ 1,234.56IQDد.ع.‏\u062F.\u0639.\u200F22 أكتوبر, 2010 10:53:17 ص
ar_YEر.ي.‏ 1,234.56YERر.ي.‏\u0631.\u064A.\u200F22 أكتوبر, 2010 10:53:17 ص
hu¤ 1 234,56XXXXXXXXX2010. október 22. 10:53:17
pt_PT1.234,56 €EUR\u20AC22 de Outubro de 2010 10:53:17
el_CY€1.234,56EUR\u20AC22 Οκτώβριος 2010 10:53:17 ΠΜ
ar_QAر.ق.‏ 1,234.56QARر.ق.‏\u0631.\u0642.\u200F22 أكتوبر, 2010 10:53:17 ص
mk_MKDen 1.234,56MKDDenDen22, октомври 2010 10:53:
sv¤ 1 234,56XXXXXXXXXden 22 oktober 2010 10:53:17
de_CHSFr. 1'234.56CHFSFr.SFr.22. Oktober 2010 10:53:17
en_US$1,234.56USD$$October 22, 2010 10:53:17 AM
fi_FI1 234,56 €EUR\u20AC22. lokakuuta 2010 10:53:17
is¤ 1.234,56XXXXXXXXX22. október 2010 10:53:17
cs¤ 1 234,56XXXXXXXXX22. říjen 2010 10:53:17
en_MT€1,234.56EUR\u20AC22 October 2010 10:53:17
sl_SI€ 1.234,56EUR\u20ACPetek, 22 oktober 2010 10:53:17
sk_SK1 234,56 SkSKKSkSkPiatok, 2010, október 22 10:53:17
it¤ 1.234,56XXXXXXXXX22 ottobre 2010 10.53.17
tr_TR1.234,56 YTLTRYYTLYTL22 Ekim 2010 Cuma 10:53:17
zh¤ 1,234.56XXXXXXXXX2010年10月22日 10:53:17
th¤ 1,234.56XXXXXXXXX22 ตุลาคม 2010, 10:53:17
ar_SAر.س.‏ 1,234.56SARر.س.‏\u0631.\u0633.\u200F22 أكتوبر, 2010 10:53:17 ص
no¤ 1 234,56XXXXXXXXX22. oktober 2010 10:53:17
en_GB£1,234.56GBP£\u00A322 October 2010 10:53:17
sr_CSCSD 1.234,56CSDCSDCSD22.10.2010. 10.53.17
lt¤ 1 234,56XXXXXXXXXPenktadienis, 2010, Spalio 22 10.53.17
ro¤ 1.234,56XXXXXXXXX22 octombrie 2010 10:53:17
en_NZ$1,234.56NZD$$22 October 2010 10:53:17 AM
no_NO_NYkr 1 234,56NOKkrkr22. oktober 2010 10:53:17
lt_LT1 234,56 LtLTLLtLtPenktadienis, 2010, Spalio 22 10.53.17
es_NI$C1,234.56NIO$C$C22 de octubre de 2010 10:53:17 AM
nl¤ 1.234,56XXXXXXXXX22 oktober 2010 10:53:17
ga_IE€1,234.56EUR\u20AC22 Deireadh Fómhair 2010 10:53:17
fr_BE1.234,56 €EUR\u20AC22 octobre 2010 10:53:17
es_ES1.234,56 €EUR\u20AC22 de octubre de 2010 10:53:17
ar_LBل.ل.‏ 1,234.56LBPل.ل.‏\u0644.\u0644.\u200F22 تشرين الأول, 2010 10:53:17 ص
ko¤ 1,234.56XXXXXXXXX2010년 10월 22일 (금) 오전 10:53:17
fr_CA1 234,56 $CAD$$22 octobre 2010 10:53:17
et_EE1 234,56 krEEKkrkrreede, 22. Oktoober 2010. a 10:53:17
ar_KWد.ك.‏ 1,234.56KWDد.ك.‏\u062F.\u0643.\u200F22 أكتوبر, 2010 10:53:17 ص
sr_RSRSD 1.234,56RSDRSDRSD22.10.2010. 10.53.17
es_USUS$1,234.56USDUS$US$22 de octubre de 2010 10:53:17 a.m.
es_MX$1,234.56MXN$$22 de octubre de 2010 10:53:17 AM
ar_SDج.س.‏ 1,234.56SDGج.س.‏\u062C.\u0633.\u200F22 أكتوبر, 2010 10:53:17 ص
in_IDRp1.234,56IDRRpRp22 Oktober 2010 10:53:17
ru¤ 1 234,56XXXXXXXXX22 Октябрь 2010 г. 10:53:17
lv¤ 1 234,56XXXXXXXXXpiektdiena, 2010, 22 oktobris 10:53:17
es_UYNU$ 1.234,56UYUNU$NU$22 de octubre de 2010 10:53:17 AM
lv_LV1 234,56 LsLVLLsLspiektdiena, 2010, 22 oktobris 10:53:17
iw¤ 1,234.56XXXXXXXXX10:53:17 22 אוקטובר 2010
pt_BRR$ 1.234,56BRLR$R$22 de Outubro de 2010 10:53:17
ar_SYل.س.‏ 1,234.56SYPل.س.‏\u0644.\u0633.\u200F22 تشرين الأول, 2010 10:53:17 ص
hr¤ 1.234,56XXXXXXXXX2010. listopad 22 10:53:17
et¤ 1 234,56XXXXXXXXXreede, 22. Oktoober 2010. a 10:53:17
es_DORD$1,234.56DOPRD$RD$22 de octubre de 2010 10:53:17 AM
fr_CHSFr. 1'234.56CHFSFr.SFr.22. octobre 2010 10:53:17
hi_INरू १,२३४.५६INRरू\u0930\u0942२२ अक्‍तूबर, २०१० १०:५३:१७ पूर्वाह्न
es_VEBsF.1.234,56VEFBsF.BsF.22 de octubre de 2010 10:53:17 AM
ar_BHد.ب.‏ 1,234.56BHDد.ب.‏\u062F.\u0628.\u200F22 أكتوبر, 2010 10:53:17 ص
en_PHPhp1,234.56PHPPhpPhpOctober 22, 2010 10:53:17 AM
ar_TNد.ت.‏ 1,234.56TNDد.ت.‏\u062F.\u062A.\u200F22 أكتوبر, 2010 10:53:17 ص
fi¤ 1 234,56XXXXXXXXX22. lokakuuta 2010 10:53:17
de_AT€ 1.234,56EUR\u20AC22. Oktober 2010 10:53:17
es¤1.234,56XXXXXXXXX22 de octubre de 2010 10:53:17
nl_NL€ 1.234,56EUR\u20AC22 oktober 2010 10:53:17
es_EC$1.234,56USD$$22 de octubre de 2010 10:53:17 AM
zh_TWNT$1,234.56TWDNT$NT$2010年10月22日 上午 10:53:17
ar_JOد.أ.‏ 1,234.56JODد.أ.‏\u062F.\u0623.\u200F22 تشرين الأول, 2010 10:53:17 ص
be¤ 1 234,56XXXXXXXXXпятніца, 22, кастрычніка 2010 10.53.17
is_IS1.235, kr.ISKkr.kr.22. október 2010 10:53:17
es_CO$1.234,56COP$$22 de octubre de 2010 10:53:17 AM
es_CRC1,234.56CRCCC22 de octubre de 2010 10:53:17 AM
es_CLCh$1.235CLPCh$Ch$22 de octubre de 2010 10:53:17 AM
ar_EGج.م.‏ 1,234.56EGPج.م.‏\u062C.\u0645.\u200F22 أكتوبر, 2010 10:53:17 ص
en_ZAR 1,234.56ZARRR22 October 2010 10:53:17 AM
th_TH฿1,234.56THB฿\u0E3F22 ตุลาคม 2553, 10:53:17
el_GR1.234,56 €EUR\u20AC22 Οκτώβριος 2010 10:53:17 πμ
it_IT€ 1.234,56EUR\u20AC22 ottobre 2010 10.53.17
ca¤ 1.234,56XXXXXXXXX22 / octubre / 2010 10:53:17
hu_HU1 234,56 FtHUFFtFt2010. október 22. 10:53:17
fr1 234,56 ¤XXXXXXXXX22 octobre 2010 10:53:17
en_IE€1,234.56EUR\u20AC22 October 2010 10:53:17
uk_UA1.234,56 грв.UAHгрв.\u0433\u0440\u0432.22 жовтня 2010 10:53:17
pl_PL1 234,56 złPLNz\u014222 październik 2010 10:53:17
fr_LU1 234,56 €EUR\u20AC22 octobre 2010 10:53:17
nl_BE1.234,56 €EUR\u20AC22 oktober 2010 10:53:17
en_INRs.1,234.56INRRs.Rs.22 October, 2010 10:53:17 AM
ca_ES€ 1.234,56EUR\u20AC22 / octubre / 2010 10:53:17
ar_MAد.م.‏ 1,234.56MADد.م.‏\u062F.\u0645.\u200F22 أكتوبر, 2010 10:53:17 ص
es_BOB$1.234,56BOBB$B$22 de octubre de 2010 10:53:17 AM
en_AU$1,234.56AUD$$22 October 2010 10:53:17 AM
sr¤ 1.234,56XXXXXXXXX22.10.2010. 10.53.17
zh_SGS$1,234.56SGDS$S$22 十月 2010 上午 10:53
pt¤ 1.234,56XXXXXXXXX22 de Outubro de 2010 10:53:17
uk¤ 1.234,56XXXXXXXXX22 жовтня 2010 10:53:17
es_SVC1,234.56SVCCC22 de octubre de 2010 10:53:17 AM
ru_RU1 234,56 руб.RUBруб.\u0440\u0443\u0431.22 Октябрь 2010 г. 10:53:17
ko_KR₩1,235KRW\uFFE62010년 10월 22일 (금) 오전 10:53:17
vi¤ 1.234,56XXXXXXXXX10:53:17 Ngày 22 tháng 10 năm 2010
ar_DZد.ج.‏ 1,234.56DZDد.ج.‏\u062F.\u062C.\u200F22 أكتوبر, 2010 10:53:17 ص
vi_VN1.234,56 đVNDđ\u011110:53:17 Ngày 22 tháng 10 năm 2010
sr_ME€ 1.234,56EUR\u20AC22.10.2010. 10.53.17
sq¤ 1.234,56XXXXXXXXX2010-10-22 10:53:17.PD
ar_LYد.ل.‏ 1,234.56LYDد.ل.‏\u062F.\u0644.\u200F22 أكتوبر, 2010 10:53:17 ص
ar¤ 1,234.56XXXXXXXXX22 أكتوبر, 2010 10:53:17 ص
zh_CN¥1,234.56CNY\uFFE52010年10月22日 10:53:17
be_BYРуб1 235BYRРуб\u0420\u0443\u0431пятніца, 22, кастрычніка 2010 10.53.17
zh_HKHK$1,234.56HKDHK$HK$2010年10月22日 星期五 上午10:53:17
ja¤ 1,234.56XXXXXXXXX2010/10/22 10:53:17
iw_IL1,234.56 ש"חILSש"ח\u05E9"\u05D710:53:17 22 אוקטובר 2010
bg_BGлв.1 234,56BGNлв.\u043B\u0432.Петък, 2010, Октомври 22 10:53:17
in¤1.234,56XXXXXXXXX2010 Oktober 22 10:53:17
mt_MT€1,234.56EUR\u20AC22 ta’ Ottubru 2010 10:53:17
es_PYG1.235PYGGG22 de octubre de 2010 10:53:17 AM
sl¤ 1.234,56XXXXXXXXXPetek, 22 oktober 2010 10:53:17
fr_FR1 234,56 €EUR\u20AC22 octobre 2010 10:53:17
cs_CZ1 234,56 KčCZKK\u010D22. říjen 2010 10:53:17
it_CHSFr. 1'234.56CHFSFr.SFr.22. ottobre 2010 10:53:17
ro_RO1.234,56 LEIRONLEILEI22 octombrie 2010 10:53:17
es_PR$1,234.56USD$$22 de octubre de 2010 10:53:17 AM
en_CA$1,234.56CAD$$October 22, 2010 10:53:17 AM
de_DE1.234,56 €EUR\u20AC22. Oktober 2010 10:53:17
ga¤ 1,234.56XXXXXXXXX2010 Deireadh Fómhair 22 10:53:17
de_LU1.234,56 €EUR\u20AC22. Oktober 2010 10:53:17
de¤ 1.234,56XXXXXXXXX22. Oktober 2010 10:53:17
es_AR$1.234,56ARS$$22 de octubre de 2010 10:53:17
sk¤ 1 234,56XXXXXXXXXPiatok, 2010, október 22 10:53:17
ms_MYRM1,234.56MYRRMRM22 Oktober 2010 10:53:17 AM
hr_HRKn 1.234,56HRKKnKn2010. listopad 22 10:53:17
en_SG$1,234.56SGD$$October 22, 2010 10:53:17 AM
da¤ 1.234,56XXXXXXXXX22. oktober 2010 10:53:17
mt¤ 1,234.56XXXXXXXXX22 ta’ Ottubru 2010 10:53:17
pl¤ 1 234,56XXXXXXXXX22 październik 2010 10:53:17
ar_OMر.ع.‏ 1,234.56OMRر.ع.‏\u0631.\u0639.\u200F22 أكتوبر, 2010 10:53:17 ص
tr1.234,56 ¤XXXXXXXXX22 Ekim 2010 Cuma 10:53:17
th_TH_TH฿๑,๒๓๔.๕๖THB฿\u0E3F๒๒ ตุลาคม ๒๕๕๓, ๑๐:๕๓:๑๗
el¤ 1.234,56XXXXXXXXX22 Οκτώβριος 2010 10:53:17 πμ
ms¤ 1,234.56XXXXXXXXX2010 Oktober 22 10:53:17
sv_SE1 234,56 krSEKkrkrden 22 oktober 2010 10:53:17
da_DKkr 1.234,56DKKkrkr22. oktober 2010 10:53:17
es_HNL1,234.56HNLLL22 de octubre de 2010 10:53:17 AM


0 comments:

Post a Comment