Reputation: 799
I have text representing date and time in English.
For example: 12 May, 2021 11:50 PM
I want to translate this into various languages that are not natively supported by java.util.Locale
API.
Examples of such languages are Bangla, Marathi, Punjabi, Telugu, Tamil, Kannada, etc.
In Bengali, for example, the above text would translate to: ১২ই মে, ২০২১ এর ১১:৫০ PM
Is there an easy way to do this?
Many thanks.
Upvotes: 0
Views: 605
Reputation: 86148
For Bangla AKA Bengali:
Locale loc = Locale.forLanguageTag("bn"); // Bengali/Bangla
DateTimeFormatter formatter
= DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM)
.localizedBy(loc)
.withDecimalStyle(DecimalStyle.of(loc));
LocalDateTime dateTime = LocalDateTime.of(2021, Month.MAY, 12, 23, 50);
System.out.println(dateTime.format(formatter));
Output is:
১২ মে, ২০২১ ১১:৫০:০০ PM
Java does support the languages you mention.
The localizedBy
method that I use was introduced in Java 10. It promises to set the decimal style too, so the call to withDecimalStyle()
ought not be necessary, but in my Java 11 it is. I can’t tell whether it’s a bug or what.
You also mentioned Marathi:
Locale loc = Locale.forLanguageTag("mr"); // Marathi
१२ मे, २०२१, ११:५०:०० म.उ.
For the remaining languages mentioned it seems that my Java 11 doesn’t know the digits to use. So we have to specify those ourselves. Only we only have to specify the digit for 0 (zero). Then Java figures the rest out.
Locale loc = Locale.forLanguageTag("pa"); // Punjabi
DateTimeFormatter formatter
= DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM)
.localizedBy(loc)
.withDecimalStyle(DecimalStyle.of(loc)
.withZeroDigit('\u0a66'));
੧੨ ਮਈ ੨੦੨੧, ੧੧:੫੦:੦੦ ਬਾ.ਦੁ.
The specifications for the remaining languages mentioned are:
Language | Langauge tag | Zero digit | Output |
---|---|---|---|
Telugu | te |
'\u0c66' |
౧౨ మే, ౨౦౨౧ ౧౧:౫౦:౦౦ PM |
Tamil | ta |
'\u0be6' |
௧௨ மே, ௨௦௨௧, பிற்பகல் ௧௧:௫௦:௦௦ |
Kannada | kn |
'\u0ce6' |
ಮೇ ೧೨, ೨೦೨೧ ೧೧:೫೦:೦೦ ಅಪರಾಹ್ನ |
I don’t understand why you have got your date and time as text in English. You should keep your date and time in proper date-time objects like the LocalDateTime
object I use above. If you cannot avoid getting a String
, parse it. Use this formatter for parsing:
private static final DateTimeFormatter ENGLISH_PARSER
= DateTimeFormatter.ofPattern("d MMM, uuuu h:mm a", Locale.ENGLISH);
Except I can’t tell from your example whether the string contains a full month name like August
or an abbreviation like Aug
(since the month of May only has three letters in its full name). If you get full month name, you will need MMMM
instead of MMM
. In any case parse like this:
String dateTimeText = "12 May, 2021 11:50 PM";
LocalDateTime dateTime = LocalDateTime.parse(dateTimeText, ENGLISH_PARSER);
System.out.println(dateTime);
Output:
2021-05-12T23:50
Upvotes: 3
Reputation: 141
Coding yourself
Take a look at UNICODE tables. Link Use it for translating characters.
Combine it with internationalization/localization. These example will help:
Personally, I would create a properties files(as in the examples above) with key value pairs for each character/digit, month, and so on. Then, just convert the time as needed.
Using Google Translate
This answer explains how to use Google translate API for the same purpose. Depending on your tech stack, you should be able to integrate this API. It's free for 300 requests a month.
Upvotes: 1