Extract name of the current day and translate it

Post your questions and help other users.

Moderator: Martin

Post Reply
User avatar
tsolignani
Posts: 187
Joined: 12 Jan 2019 11:53
Location: Vignola, Mo, Italy
Contact:

Extract name of the current day and translate it

Post by tsolignani » 17 Feb 2020 18:05

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.

Micky Micky
Posts: 179
Joined: 16 Oct 2019 17:38

Re: Extract name of the current day and translate it

Post by Micky Micky » 17 Feb 2020 20:11

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
Crude but it works.

anuraag
Posts: 371
Joined: 24 Jan 2015 02:06

Re: Extract name of the current day and translate it

Post by anuraag » 18 Feb 2020 00:41

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);

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: Extract name of the current day and translate it

Post by Desmanto » 18 Feb 2020 06:15

@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.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

Micky Micky
Posts: 179
Joined: 16 Oct 2019 17:38

Re: Extract name of the current day and translate it

Post by Micky Micky » 18 Feb 2020 07:09

EEEE

Thanks!
Crude but it works.

User avatar
tsolignani
Posts: 187
Joined: 12 Jan 2019 11:53
Location: Vignola, Mo, Italy
Contact:

Re: Extract name of the current day and translate it

Post by tsolignani » 21 Feb 2020 07:39

Thank you.

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

Thanks again!

Post Reply