From c505ae70cc6d6844c88cc78cd4263128bcda5521 Mon Sep 17 00:00:00 2001 From: cobacdavid Date: Thu, 29 Jul 2021 16:45:58 +0200 Subject: [weather-widget] enable locale --- weather-widget/locale/en.lua | 14 ++++++++++++++ weather-widget/locale/fr.lua | 14 ++++++++++++++ weather-widget/weather.lua | 35 ++++++++++++++++++++++------------- 3 files changed, 50 insertions(+), 13 deletions(-) create mode 100644 weather-widget/locale/en.lua create mode 100644 weather-widget/locale/fr.lua (limited to 'weather-widget') diff --git a/weather-widget/locale/en.lua b/weather-widget/locale/en.lua new file mode 100644 index 0000000..377dc6f --- /dev/null +++ b/weather-widget/locale/en.lua @@ -0,0 +1,14 @@ +local en = { + warning_title = "Weather Widget", + parameter_warning = "Required parameters are not set: ", + directions = { + "N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", + "WSW", "W", "WNW", "NW", "NNW", "N" + }, + feels_like = "Feels like ", + wind = "Wind: ", + humidity = "Humidity: ", + uv = "UV: " +} + +return en diff --git a/weather-widget/locale/fr.lua b/weather-widget/locale/fr.lua new file mode 100644 index 0000000..de50814 --- /dev/null +++ b/weather-widget/locale/fr.lua @@ -0,0 +1,14 @@ +local fr = { + warning_title = "Widget Météo", + parameter_warning = "Les paramètres suivants sont obligatoires : ", + directions = { + "N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSO", "SO", + "OSO", "O", "ONO", "NO", "NNO", "N" + }, + feels_like = "ressentie à ", + wind = "Vent : ", + humidity = "Humidité : ", + uv = "Indice UV : " +} + +return fr diff --git a/weather-widget/weather.lua b/weather-widget/weather.lua index 1847fd4..9daabf1 100644 --- a/weather-widget/weather.lua +++ b/weather-widget/weather.lua @@ -17,14 +17,25 @@ local HOME_DIR = os.getenv("HOME") local WIDGET_DIR = HOME_DIR .. '/.config/awesome/awesome-wm-widgets/weather-widget' local GET_FORECAST_CMD = [[bash -c "curl -s --show-error -X GET '%s'"]] +local SYS_LANG = os.getenv("LANG"):sub(1, 2) +-- default language is ENglish +local LANG = gears.filesystem.file_readable(WIDGET_DIR .. "/" .. "locale/" .. + SYS_LANG .. ".lua") and SYS_LANG or "en" +local LCLE = require("awesome-wm-widgets.weather-widget.locale." .. LANG) + + local function show_warning(message) naughty.notify { preset = naughty.config.presets.critical, - title = 'Weather Widget', + title = LCLE.warning_title, text = message } end +if SYS_LANG ~= LANG then + show_warning("Your language is not supported yet. Language set to English") +end + local weather_widget = {} local warning_shown = false local tooltip = awful.tooltip { @@ -69,10 +80,7 @@ local icon_map = { local function to_direction(degrees) -- Ref: https://www.campbellsci.eu/blog/convert-wind-directions if degrees == nil then return "Unknown dir" end - local directions = { - "N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", - "WSW", "W", "WNW", "NW", "NNW", "N" - } + local directions = LCLE.directions return directions[math.floor((degrees % 360) / 22.5) + 1] end @@ -120,9 +128,9 @@ local function worker(user_args) --- Validate required parameters if args.coordinates == nil or args.api_key == nil then - show_warning('Required parameters are not set: ' .. - (args.coordinates == nil and 'coordinates' or '') .. - (args.api_key == nil and ', api_key ' or '')) + show_warning(LCLE.parameter_warning .. + (args.coordinates == nil and 'coordinates' or '') .. + (args.api_key == nil and ', api_key ' or '')) return end @@ -144,7 +152,8 @@ local function worker(user_args) '?lat=' .. coordinates[1] .. '&lon=' .. coordinates[2] .. '&appid=' .. api_key .. '&units=' .. units .. '&exclude=minutely' .. (show_hourly_forecast == false and ',hourly' or '') .. - (show_daily_forecast == false and ',daily' or '')) + (show_daily_forecast == false and ',daily' or '') .. + '&lang=' .. LANG) weather_widget = wibox.widget { { @@ -256,12 +265,12 @@ local function worker(user_args) ICONS_DIR .. icon_map[weather.weather[1].icon] .. icons_extension) self:get_children_by_id('temp')[1]:set_text(gen_temperature_str(weather.temp, '%.0f', false, units)) self:get_children_by_id('feels_like_temp')[1]:set_text( - 'Feels like ' .. gen_temperature_str(weather.feels_like, '%.0f', false, units)) + LCLE.feels_like .. gen_temperature_str(weather.feels_like, '%.0f', false, units)) self:get_children_by_id('description')[1]:set_text(weather.weather[1].description) self:get_children_by_id('wind')[1]:set_markup( - 'Wind: ' .. weather.wind_speed .. 'm/s (' .. to_direction(weather.wind_deg) .. ')') - self:get_children_by_id('humidity')[1]:set_markup('Humidity: ' .. weather.humidity .. '%') - self:get_children_by_id('uv')[1]:set_markup('UV: ' .. uvi_index_color(weather.uvi)) + LCLE.wind .. '' .. weather.wind_speed .. 'm/s (' .. to_direction(weather.wind_deg) .. ')') + self:get_children_by_id('humidity')[1]:set_markup(LCLE.humidity .. '' .. weather.humidity .. '%') + self:get_children_by_id('uv')[1]:set_markup(LCLE.uv .. uvi_index_color(weather.uvi)) end } -- cgit v1.2.3