Page 1 of 1

Extract name of the current day and translate it

Posted: 17 Feb 2020 18:05
by tsolignani
Hallo everyone.

I found a way to extract the name of the current day.

Now I need to translate it into Italian, say Monday to Lunedì.

Should I use a seven if statement group or is there a more elegant way to do that?

Thank you.

Re: Extract name of the current day and translate it

Posted: 17 Feb 2020 20:11
by Micky Micky
Hello

For a flow I wanted the day in full, ie Mon = Monday.
This is the seven if statements way you mentioned.

Code: Select all

day = "{getDate(),dateformat,E}";
if (day == "Mon") {
day = "Monday"}
else if  (day == "Tue") {
day = "Tuesday"}
else if  (day == "Wed") {
day = "Wednesday"}
else if  (day == "Thu") {
day = "Thursday"}
else if  (day == "Fri") {
day = "Friday"}
else if (day == "Sat") {
day = "Saturday"}
else if  (day == "Sun") {
day = "Sunday"};
I think there's better ways of doing it. And I would like them too. Probably a map list. But wondering if Android can translate for you. It has the languages.

Hopefully others can provide the solution.

Good luck

Re: Extract name of the current day and translate it

Posted: 18 Feb 2020 00:41
by anuraag
To Italian

Code: Select all

local = callJavaConstructor("java.util.Locale", "Locale(java.lang.String)", "it");  //change language code here
now = callJavaStaticMethod("java.time.LocalDate", "now()");
dow = callJavaMethod(now, "java.time.LocalDate", "getDayOfWeek()");
output = callJavaMethod(dow, "java.time.DayOfWeek", "getDisplayName(java.time.format.TextStyle, java.util.Locale)", getJavaStaticField("java.time.format.TextStyle", "FULL"), local)
Edit: same way with Automagic's dateformat

Code: Select all

local = callJavaConstructor("java.util.Locale", "Locale(java.lang.String)", "it");
date = getDate();
day = "{date,dateformat,EEEE}";
dow = callJavaStaticMethod("java.time.DayOfWeek", "valueOf(java.lang.String)", toUpperCase(day));
output = callJavaMethod(dow, "java.time.DayOfWeek", "getDisplayName(java.time.format.TextStyle, java.util.Locale)", getJavaStaticField("java.time.format.TextStyle", "FULL"), local);

Re: Extract name of the current day and translate it

Posted: 18 Feb 2020 06:15
by Desmanto
@tsolignani : I also face the same problem, especially when dealing with speech output. I have to get the day name in local language and speak it out. I create a list that contain the day name and then use index formatted from day to retrieve the list. The same can be done with month name.

Code: Select all

dayname = newList("Lunedi", "Martedi", "Mercoledi", "Giovedi", "Venerdi", "Sabato", "Domenica");

numday = "{triggertime,dateformat,u}"; //return 1 = Monday, to 7 = Sunday
dayitalian = dayname[numday-1]; //index start at 0, so need to minus 1
Pattern 'u' in dateformat only available from Nougat above. (For user below Nougat, can divide and get modulus of it.)

Other method is to use map for translation. This can be used for basically translation. You can create a full database to translate from one to another language or pattern/symbol.

Code: Select all

daydb = newMapFromValues(
"Monday", "Lunedi",
"Tuesday", "Martedi",
"Wednesday", "Mercoledi",
"Thursday", "Giovedi",
"Friday", "Venerdi",
"Saturday", "Sabato",
"Sunday", "Domenica");

dayenglish = "{triggertime,dateformat,EEEE}";
dayitalian = daydb[dayenglish];
if you use 'E' datepattern, then you should use "Mon" instead of "Monday". Change other day accordingly.

@anuraag : Java is always full of wonder, I don't know there is built-in function for that.

Re: Extract name of the current day and translate it

Posted: 18 Feb 2020 07:09
by Micky Micky
EEEE

Thanks!

Re: Extract name of the current day and translate it

Posted: 21 Feb 2020 07:39
by tsolignani
Thank you.

The Java way was the most elegant, to me, and I can confirm it works.

Thanks again!