2

I use JSTL <fmt:message> tag with properties file to localize text on the JSP. The texts for different languages are kept in their respective properties file associated with a key.

Then that key is used in JSP like, <fmt:message key="somekey"/>

How do I localize number system this way? E.g. when Marathi locale is used, the Latin number 456 should print as ४५६.

ajm
  • 12,863
  • 58
  • 163
  • 234
  • There are two separate issues involved: 1) number formatting in JSTL and 2) Marathi support in Java. If you more interesting in the second issue, please mention it in the question title. – kgeorgiy Nov 08 '16 at 09:41

3 Answers3

3

Marathi

Unfortunately there is no standard implementation for Marathi. But you can use ICU's RuleBasedNumberFormat to create your own translations.

There is also a GitHub project, which use it to format Marathi numbers, so that you don't need to write the rules yourself. You can easily extend it to handle mutliple languages and create a custom tag for it.

Hindi

Hindi is supported by Java's NumberFormat, but fmt:numberFormat doesn't use it directly, therefore you still have to create a custom tag.

Christian Strempfer
  • 7,291
  • 6
  • 50
  • 75
0

If you just want to convert your Numbers to Marathi equivalent numbers. Then create class like below. This class helps you to convert your numbers to not only in marathi but punjabi, gujarati, hundi but urdu also. you can add many more likewise.

HashMap<String, String> unicodeConversionList = new HashMap<>();

public void prepareList(String lang)
{
    unicodeConversionList.clear();
    if("marathi".equalsIgnoreCase(lang))
    {
        unicodeConversionList.put("0", "\u0966");
        unicodeConversionList.put("1", "\u0967");
        unicodeConversionList.put("2", "\u0968");
        unicodeConversionList.put("3", "\u0969");
        unicodeConversionList.put("4", "\u096A");
        unicodeConversionList.put("5", "\u096B");
        unicodeConversionList.put("6", "\u096C");
        unicodeConversionList.put("7", "\u096D");
        unicodeConversionList.put("8", "\u096E");
        unicodeConversionList.put("9", "\u096F");
    }
    else if("hindi".equalsIgnoreCase(lang))
    {
        unicodeConversionList.put("0", "\u0966");
        unicodeConversionList.put("1", "\u0967");
        unicodeConversionList.put("2", "\u0968");
        unicodeConversionList.put("3", "\u0969");
        unicodeConversionList.put("4", "\u096A");
        unicodeConversionList.put("5", "\u096B");
        unicodeConversionList.put("6", "\u096C");
        unicodeConversionList.put("7", "\u096D");
        unicodeConversionList.put("8", "\u096E");
        unicodeConversionList.put("9", "\u096F");
    }else if("gujarati".equalsIgnoreCase(lang))
    {
        unicodeConversionList.put("0", "\u0AE6");
        unicodeConversionList.put("1", "\u0AE7");
        unicodeConversionList.put("2", "\u0AE8");
        unicodeConversionList.put("3", "\u0AE9");
        unicodeConversionList.put("4", "\u0AEA");
        unicodeConversionList.put("5", "\u0AEB");
        unicodeConversionList.put("6", "\u0AEC");
        unicodeConversionList.put("7", "\u0AED");
        unicodeConversionList.put("8", "\u0AEE");
        unicodeConversionList.put("9", "\u0AEF");
    }else if("punjabi".equalsIgnoreCase(lang))
    {
        unicodeConversionList.put("0", "\u0AE6");
        unicodeConversionList.put("1", "\u0AE7");
        unicodeConversionList.put("2", "\u0AE8");
        unicodeConversionList.put("3", "\u0AE9");
        unicodeConversionList.put("4", "\u0AEA");
        unicodeConversionList.put("5", "\u0AEB");
        unicodeConversionList.put("6", "\u0AEC");
        unicodeConversionList.put("7", "\u0AED");
        unicodeConversionList.put("8", "\u0AEE");
        unicodeConversionList.put("9", "\u0AEF");
    }else if("urdu".equalsIgnoreCase(lang))
    {
        unicodeConversionList.put("0", "\u06F0");
        unicodeConversionList.put("1", "\u06F1");
        unicodeConversionList.put("2", "\u0682");
        unicodeConversionList.put("3", "\u0693");
        unicodeConversionList.put("4", "\u06A4");
        unicodeConversionList.put("5", "\u06B5");
        unicodeConversionList.put("6", "\u06C6");
        unicodeConversionList.put("7", "\u06D7");
        unicodeConversionList.put("8", "\u06E8");
        unicodeConversionList.put("9", "\u06F9");
    }else{
        unicodeConversionList.put("0", "0");
        unicodeConversionList.put("1", "1");
        unicodeConversionList.put("2", "2");
        unicodeConversionList.put("3", "3");
        unicodeConversionList.put("4", "4");
        unicodeConversionList.put("5", "5");
        unicodeConversionList.put("6", "6");
        unicodeConversionList.put("7", "7");
        unicodeConversionList.put("8", "8");
        unicodeConversionList.put("9", "9");
    }
}

public String convertToMarathi(String lang,String str)
{
    prepareList(lang);

    StringBuilder response = new StringBuilder("");
    if(str!=null && str.trim().length()>0 && unicodeConversionList.size()==10)
        for (int i = 0; i < str.length(); i++)
        {
            String temp = "" + str.charAt(i);
            if (unicodeConversionList.containsKey(temp))
                response.append(unicodeConversionList.get(temp));
            else
                response.append(temp);
        }
    else
        response.append(str);

    return response.toString();
}

Its very easy to use these code with EL.

If you are using filters then

//Add This code to your filters
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
        ServletException
{
    HttpServletRequest req = (HttpServletRequest) request;
    if (req.getSession().isNew())
    {
        HttpSession session=req.getSession();
        session.setAttribute("converter", new ConverterUtil());
    }
    // pass the request along the filter chain
    chain.doFilter(request, response);
}

And then all you need to do in JSP is

${converter.convertToMarathi("Gujarati","1234 u 567 z 890") }

If you want to write manually in every page then Follow Code Below

<jsp:useBean id="converter2" class="digimation.bestowBucket.util.ConverterUtil"></jsp:useBean>
${converter2.convertToMarathi("maRatHi","1234 u 567 z 890") }
Shalin Patel
  • 1,079
  • 1
  • 10
  • 15
-1

Numbers should be formatted using <fmt:numberFormat>. See example in the J2EE tutorial.

kgeorgiy
  • 1,477
  • 7
  • 9
  • I don't want to format numbers. Different language will have different characters for numbers e..g in English = 456, In Marathi = ४५६ – ajm Nov 05 '16 at 11:44
  • And what is the expected result and why? – kgeorgiy Nov 05 '16 at 11:45
  • If the locale is English, it should display 456 and if the locale is Marathi it should display ४५६. Application end user may or may not know English, in that case they will not recognise the English numbers. End user will have the option to select their preferred language. Text is converted using properties file. – ajm Nov 05 '16 at 11:47
  • Hindi (Devanagari numbering system) is supported by Java out-of-the-box and this solution works for Hindi. Adding a Marathi support to Java is completely different question. – kgeorgiy Nov 08 '16 at 09:59
  • I need for both hindi and marathi – ajm Nov 08 '16 at 10:00
  • The numbers in hindi and marathi are similar. So, hindi will do. – ajm Nov 08 '16 at 10:08