Skip to content

Commit 0563d12

Browse files
committed
Localized name of the day in the week
1 parent 5e6bee7 commit 0563d12

File tree

4 files changed

+55
-4
lines changed

4 files changed

+55
-4
lines changed

Community/OpenWeatherMap/automation/jsr223/python/community/openweathermap/owm_daily_forecast.py

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
Change Log
8080
==========
8181
82+
* 23/11/20: Added support for day of the week in the current local language
8283
* 01/11/19: Corrected an issue where the Items were not linking.
8384
* 01/12/19: Removed Forecast_Temperature_X, and added
8485
Forecast_Temperature_High_X and Forecast_Temperature_Low_X
@@ -105,7 +106,9 @@
105106
* -6/14/20: Only check for SCALE transformation service if using Linux OS
106107
* 06/14/20: Using System properties for HTTP/HTTPS ports, rather than hard coded 8080/8443
107108
"""
109+
from __future__ import unicode_literals
108110
from core.log import logging, LOG_PREFIX, log_traceback
111+
from core.actions import Transformation
109112

110113

111114
@log_traceback
@@ -128,6 +131,32 @@ def remove_owm_items():
128131

129132
#remove_owm_items()
130133

134+
@log_traceback
135+
def current_lang():
136+
"""
137+
Determines the current language of the openhab.
138+
You need to assure you have setup JAVA_OPTS in the start.sh to your preferred language and country, e.g.
139+
EXTRA_JAVA_OPTS="-Duser.language=pl -Duser.country=PL"
140+
"""
141+
default_locale = locale.getdefaultlocale()[0]
142+
if default_locale:
143+
return default_locale if "_" not in default_locale else default_locale.split("_")[0]
144+
return "en"
145+
146+
@log_traceback
147+
def map_day_of_week(index, last_reading):
148+
"""
149+
Method calculates the day index and maps into the day of the week for the current language
150+
151+
Args:
152+
index: Forecast group id
153+
last_reading: Forecast group id of the last reading
154+
155+
Returns:
156+
str: A name of the day in the week for current language
157+
"""
158+
day_week_idx = "TODAY" if index == 1 else str((last_reading + index - 1) % 7)
159+
return Transformation.transform("MAP", "weather-{}.map".format(current_lang()), day_week_idx) or u"UNKNOWN"
131160

132161
def add_owm_items():
133162
add_owm_items.log = logging.getLogger("{}.add_owm_items".format(LOG_PREFIX))
@@ -266,11 +295,10 @@ def add_owm_items():
266295
add_link("Current_SnowVolume", ChannelUID(owm_thing_uid + ":current#snow"))
267296

268297
# create Forecast groups
269-
import calendar
270298
from org.joda.time import DateTime
271299
last_reading = DateTime(str(items["Current_Timestamp"])).getDayOfWeek() - 1
272300
for index in range(1, 6):
273-
day_of_the_week = "Today" if index == 1 else calendar.day_name[(last_reading + index - 1) % 7]
301+
day_of_the_week = map_day_of_week(index, last_reading)
274302
if itemRegistry.getItems("gForecast_" + str(index)) == []:
275303
add_item("gForecast_" + str(index), item_type="Group", groups=["gOpenWeatherMap"], label=day_of_the_week, tags=["OpenWeatherMap"])
276304
if itemRegistry.getItems("gForecast_Timestamp_" + str(index)) == []:
@@ -392,10 +420,9 @@ def add_owm_items_to_groups(event):
392420

393421
# update group labels to reflect week day
394422
from org.joda.time import DateTime
395-
import calendar
396423
last_reading = DateTime(str(items["Current_Timestamp"])).getDayOfWeek() - 1
397424
for index in range(1, 6):
398-
day_of_the_week = "Today" if index == 1 else calendar.day_name[(last_reading + index - 1) % 7]
425+
day_of_the_week = map_day_of_week(index, last_reading)
399426
itemRegistry.getItem("gForecast_" + str(index)).setLabel(day_of_the_week)
400427
itemRegistry.getItem("gForecast_Timestamp_" + str(index)).setLabel(day_of_the_week + ": Timestamp")
401428
itemRegistry.getItem("gForecast_Condition_" + str(index)).setLabel(day_of_the_week + ": Condition [%s]")
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
0=Montag
2+
1=Dienstag
3+
2=Dienstag
4+
3=Donnerstag
5+
4=Freitag
6+
5=Samstag
7+
6=Sonntag
8+
TODAY=Heute
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
0=Poniedziałek
2+
1=Wtorek
3+
2=Środa
4+
3=Czwartek
5+
4=Piątek
6+
5=Sobota
7+
6=Niedziela
8+
TODAY=Dzisiaj
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
0=Poniedziałek
2+
1=Wtorek
3+
2=Śaroda
4+
3=Czwartek
5+
4=Piątek
6+
5=Sobota
7+
6=Niedziela
8+
TODAY=Dzisiaj

0 commit comments

Comments
 (0)