@@ -15,34 +15,44 @@ local gears = require("gears")
15
15
local beautiful = require (" beautiful" )
16
16
17
17
local HOME_DIR = os.getenv (" HOME" )
18
- local WIDGET_DIR = HOME_DIR .. ' /.config/awesome/awesome-wm-widgets/weather-widget'
18
+ local WIDGET_DIR = HOME_DIR .. ' /.config/awesome/awesome-wm-widgets/weather-api- widget'
19
19
local GET_FORECAST_CMD = [[ bash -c "curl -s --show-error -X GET '%s'"]]
20
20
21
21
local SYS_LANG = os.getenv (" LANG" ):sub (1 , 2 )
22
22
if SYS_LANG == " C" or SYS_LANG == " C." then
23
23
-- C-locale is a common fallback for simple English
24
24
SYS_LANG = " en"
25
25
end
26
- -- default language is ENglish
27
- local LANG = gears .filesystem .file_readable (WIDGET_DIR .. " /" .. " locale/" ..
28
- SYS_LANG .. " .lua" ) and SYS_LANG or " en"
29
- local LCLE = require (" awesome-wm-widgets.weather-widget.locale." .. LANG )
30
- -- WeatherAPI supports only these according to https://www.weatherapi.com/docs/
31
- -- ar, bn, bg, zh, zh_tw, cs, da, nl, fi, fr, de, el, hi, hu, it, ja, jv, ko,
32
- -- zh_cmn, mr, pl, pt, pa, ro, ru, sr, si, sk, es, sv, ta, te, tr, uk, ur, vi,
33
- -- zh_wuu, zh_hsn, zh_yue, zu
34
-
35
26
36
- local function show_warning (message )
27
+ local function show_warning (message , locale )
37
28
naughty .notify {
38
29
preset = naughty .config .presets .critical ,
39
- title = LCLE .warning_title ,
30
+ title = locale .warning_title ,
40
31
text = message
41
32
}
42
33
end
43
34
44
- if SYS_LANG ~= LANG then
45
- show_warning (" Your language is not supported yet. Language set to English" )
35
+ local function get_locale (data )
36
+ -- WeatherAPI supports only these according to https://www.weatherapi.com/docs/
37
+ -- ar, bn, bg, zh, zh_tw, cs, da, nl, fi, fr, de, el, hi, hu, it, ja, jv, ko,
38
+ -- zh_cmn, mr, pl, pt, pa, ro, ru, sr, si, sk, es, sv, ta, te, tr, uk, ur, vi,
39
+ -- zh_wuu, zh_hsn, zh_yue, zu
40
+
41
+ -- default language is ENglish
42
+ local lang = gears .filesystem .file_readable (
43
+ WIDGET_DIR .. " /" .. " locale/" .. data .. " .lua"
44
+ ) and data or " en"
45
+
46
+ local locale = require (" awesome-wm-widgets.weather-api-widget.locale." .. lang )
47
+
48
+ if data ~= lang then
49
+ show_warning (
50
+ string.format (" Your language (%s) is not supported yet. Language set to English" , data ),
51
+ locale
52
+ )
53
+ end
54
+
55
+ return locale
46
56
end
47
57
48
58
local weather_widget = {}
@@ -162,7 +172,7 @@ local function worker(user_args)
162
172
163
173
--- Validate required parameters
164
174
if args .coordinates == nil or args .api_key == nil then
165
- show_warning (LCLE .parameter_warning ..
175
+ show_warning (locale .parameter_warning ..
166
176
(args .coordinates == nil and ' <b>coordinates</b>' or ' ' ) ..
167
177
(args .api_key == nil and ' , <b>api_key</b> ' or ' ' ))
168
178
return
@@ -172,6 +182,7 @@ local function worker(user_args)
172
182
local api_key = args .api_key
173
183
local font_name = args .font_name or beautiful .font :gsub (" %s%d+$" , " " )
174
184
local units = args .units or ' metric'
185
+ local lang = args .lang or SYS_LANG
175
186
local time_format_12h = args .time_format_12h
176
187
local both_units_widget = args .both_units_widget or false
177
188
local icon_pack_name = args .icons or ' weather-underground-icons'
@@ -181,12 +192,15 @@ local function worker(user_args)
181
192
local show_hourly_forecast = args .show_hourly_forecast or false
182
193
local timeout = args .timeout or 120
183
194
local ICONS_DIR = WIDGET_DIR .. ' /icons/' .. icon_pack_name .. ' /'
195
+
196
+ local locale = get_locale (lang )
197
+
184
198
-- Forecast endpoint includes current. I could map show_daily_forecast to days here.
185
199
-- Currently overfetching but only showing when opting in.
186
200
local weather_api =
187
201
(' https://api.weatherapi.com/v1/forecast.json' ..
188
202
' ?q=' .. coordinates [1 ] .. ' ,' .. coordinates [2 ] .. ' &key=' .. api_key ..
189
- ' &units=' .. units .. ' &lang=' .. LANG .. ' &days=3' )
203
+ ' &units=' .. units .. ' &lang=' .. lang .. ' &days=3' )
190
204
191
205
weather_widget = wibox .widget {
192
206
{
@@ -303,12 +317,12 @@ local function worker(user_args)
303
317
ICONS_DIR .. icon_map [weather .condition .code ] .. day_night_extension .. icons_extension )
304
318
self :get_children_by_id (' temp' )[1 ]:set_text (gen_temperature_str (weather .temp_c , ' %.0f' , false , units ))
305
319
self :get_children_by_id (' feels_like_temp' )[1 ]:set_text (
306
- LCLE .feels_like .. gen_temperature_str (weather .feelslike_c , ' %.0f' , false , units ))
320
+ locale .feels_like .. gen_temperature_str (weather .feelslike_c , ' %.0f' , false , units ))
307
321
self :get_children_by_id (' description' )[1 ]:set_text (weather .condition .text )
308
322
self :get_children_by_id (' wind' )[1 ]:set_markup (
309
- LCLE .wind .. ' <b>' .. weather .wind_kph .. ' km/h (' .. weather .wind_dir .. ' )</b>' )
310
- self :get_children_by_id (' humidity' )[1 ]:set_markup (LCLE .humidity .. ' <b>' .. weather .humidity .. ' %</b>' )
311
- self :get_children_by_id (' uv' )[1 ]:set_markup (LCLE .uv .. uvi_index_color (weather .uv ))
323
+ locale .wind .. ' <b>' .. weather .wind_kph .. ' km/h (' .. weather .wind_dir .. ' )</b>' )
324
+ self :get_children_by_id (' humidity' )[1 ]:set_markup (locale .humidity .. ' <b>' .. weather .humidity .. ' %</b>' )
325
+ self :get_children_by_id (' uv' )[1 ]:set_markup (locale .uv .. uvi_index_color (weather .uv ))
312
326
end
313
327
}
314
328
@@ -324,7 +338,7 @@ local function worker(user_args)
324
338
325
339
local day_forecast = wibox .widget {
326
340
{
327
- text = os.date (' %a' , tonumber (day .date_epoch )),
341
+ text = locale . days [ os.date (' %a' , tonumber (day .date_epoch ))] ,
328
342
align = ' center' ,
329
343
font = font_name .. ' 9' ,
330
344
widget = wibox .widget .textbox
0 commit comments