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.
Extract name of the current day and translate it
Moderator: Martin
- tsolignani
- Posts: 187
- Joined: 12 Jan 2019 11:53
- Location: Vignola, Mo, Italy
- Contact:
-
- Posts: 179
- Joined: 16 Oct 2019 17:38
Re: Extract name of the current day and translate it
Hello
For a flow I wanted the day in full, ie Mon = Monday.
This is the seven if statements way you mentioned.
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
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"};
Hopefully others can provide the solution.
Good luck
Crude but it works.
Re: Extract name of the current day and translate it
To Italian
Edit: same way with Automagic's dateformat
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)
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
@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.
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.
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.
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
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];
@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.
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.
-
- Posts: 179
- Joined: 16 Oct 2019 17:38
- tsolignani
- Posts: 187
- Joined: 12 Jan 2019 11:53
- Location: Vignola, Mo, Italy
- Contact:
Re: Extract name of the current day and translate it
Thank you.
The Java way was the most elegant, to me, and I can confirm it works.
Thanks again!
The Java way was the most elegant, to me, and I can confirm it works.
Thanks again!