Skip to content

Commit

Permalink
Add month translations
Browse files Browse the repository at this point in the history
  • Loading branch information
olicooper committed Oct 20, 2024
1 parent 6ca4edc commit 2e51338
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 6 deletions.
3 changes: 2 additions & 1 deletion advanced-example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ nspanel_lovelace:
id: nspanel
sleep_timeout: 10
# locale:
## This can be a language code or custom json file (i.e. custom.json). Only en,en_GB,de have been added so far.
## This can be the ISO 639‑1 language code or custom json file (i.e. custom.json).
## Only en,en_GB,de have been added so far.
## NOTE: This feature is in beta so the translation keys and values could change at any time!
## Any custom translation files you create will need to be kept in sync with the the files on the repo
## https://github.com/olicooper/esphome-nspanel-lovelace-native/tree/dev/components/nspanel_lovelace/translations
Expand Down
86 changes: 84 additions & 2 deletions components/nspanel_lovelace/nspanel_lovelace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1426,6 +1426,8 @@ void NSPanelLovelace::init_display_(int baud_rate) {

#ifdef USE_TIME
// see: https://esphome.io/components/time/#strftime
// note: Because ESP-IDF doesn't support locale (due to memory constraints),
// the only way to translate the time is to manually replace the date/time strings
void NSPanelLovelace::update_datetime(const datetime_mode mode, const char *date_format, const char *time_format) {
ESPTime now = this->time_id_.value()->now();

Expand All @@ -1443,6 +1445,7 @@ void NSPanelLovelace::update_datetime(const datetime_mode mode, const char *date
// todo: fetch from config before using default value
if (datefmt.empty())
datefmt = this->date_format_;
auto timestr = now.strftime(datefmt);

DayOfWeekMap::dow_mode dow_mode = DayOfWeekMap::dow_mode::none;
if (datefmt.find("%A", 0) != std::string::npos) {
Expand All @@ -1455,11 +1458,90 @@ void NSPanelLovelace::update_datetime(const datetime_mode mode, const char *date
dow_mode = (DayOfWeekMap::dow_mode)(dow_mode |
DayOfWeekMap::dow_mode::short_dow);
}
this->day_of_week_map_.replace(timestr, dow_mode);

auto timestr = now.strftime(datefmt);
if (datefmt.find("%b", 0) != std::string::npos ||
datefmt.find("%B", 0) != std::string::npos ||
datefmt.find("%c", 0) != std::string::npos ||
datefmt.find("%h", 0) != std::string::npos) {
switch(now.month) {
case 1:
replace_first(timestr, "January",
get_translation(translation_item::month_january));
replace_first(timestr, "Jan",
get_translation(translation_item::month_jan));
break;
case 2:
replace_first(timestr, "February",
get_translation(translation_item::month_february));
replace_first(timestr, "Feb",
get_translation(translation_item::month_feb));
break;
case 3:
replace_first(timestr, "March",
get_translation(translation_item::month_march));
replace_first(timestr, "Mar",
get_translation(translation_item::month_mar));
break;
case 4:
replace_first(timestr, "April",
get_translation(translation_item::month_april));
replace_first(timestr, "Apr",
get_translation(translation_item::month_apr));
break;
case 5:
replace_first(timestr, "May",
get_translation(translation_item::month_may));
break;
case 6:
replace_first(timestr, "June",
get_translation(translation_item::month_june));
replace_first(timestr, "Jun",
get_translation(translation_item::month_jun));
break;
case 7:
replace_first(timestr, "July",
get_translation(translation_item::month_july));
replace_first(timestr, "Jul",
get_translation(translation_item::month_jul));
break;
case 8:
replace_first(timestr, "August",
get_translation(translation_item::month_august));
replace_first(timestr, "Aug",
get_translation(translation_item::month_aug));
break;
case 9:
replace_first(timestr, "September",
get_translation(translation_item::month_september));
replace_first(timestr, "Sep",
get_translation(translation_item::month_sep));
break;
case 10:
replace_first(timestr, "October",
get_translation(translation_item::month_october));
replace_first(timestr, "Oct",
get_translation(translation_item::month_oct));
break;
case 11:
replace_first(timestr, "November",
get_translation(translation_item::month_november));
replace_first(timestr, "Nov",
get_translation(translation_item::month_nov));
break;
case 12:
replace_first(timestr, "December",
get_translation(translation_item::month_december));
replace_first(timestr, "Dec",
get_translation(translation_item::month_dec));
break;
default:
break;
}
}
this->command_buffer_
.assign("date").append(1, SEPARATOR)
.append(this->day_of_week_map_.replace(timestr, dow_mode));
.append(timestr);
this->send_buffered_command_();
}

Expand Down
25 changes: 25 additions & 0 deletions components/nspanel_lovelace/translations.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,31 @@ struct translation_item {

static constexpr const char* turn_on = ha_action_type::turn_on;
static constexpr const char* turn_off = ha_action_type::turn_off;

// months of the year
static constexpr const char* month_january = "month_january";
static constexpr const char* month_jan = "month_jan";
static constexpr const char* month_february = "month_february";
static constexpr const char* month_feb = "month_feb";
static constexpr const char* month_march = "month_march";
static constexpr const char* month_mar = "month_mar";
static constexpr const char* month_april = "month_april";
static constexpr const char* month_apr = "month_apr";
static constexpr const char* month_may = "month_may";
static constexpr const char* month_june = "month_june";
static constexpr const char* month_jun = "month_jun";
static constexpr const char* month_july = "month_july";
static constexpr const char* month_jul = "month_jul";
static constexpr const char* month_august = "month_august";
static constexpr const char* month_aug = "month_aug";
static constexpr const char* month_september = "month_september";
static constexpr const char* month_sep = "month_sep";
static constexpr const char* month_october = "month_october";
static constexpr const char* month_oct = "month_oct";
static constexpr const char* month_november = "month_november";
static constexpr const char* month_nov = "month_nov";
static constexpr const char* month_december = "month_december";
static constexpr const char* month_dec = "month_dec";
};

// NOTE: This map is dynamically generated by the esphome build script from a
Expand Down
25 changes: 24 additions & 1 deletion components/nspanel_lovelace/translations/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,28 @@
"return_to_base": "Zurück zur Dockingstation",
"docked": "Angedockt",
"turn_on": "Einschalten",
"turn_off": "Ausschalten"
"turn_off": "Ausschalten",
"month_january": "Januar",
"month_jan": "Jan",
"month_february": "Februar",
"month_feb": "Feb",
"month_march": "März",
"month_mar": "März",
"month_april": "April",
"month_apr": "Apr",
"month_may": "Mai",
"month_june": "Juni",
"month_jun": "Jun",
"month_july": "Juli",
"month_jul": "Jul",
"month_august": "August",
"month_aug": "Aug",
"month_september": "September",
"month_sept": "Sep",
"month_october": "Oktober",
"month_oct": "Okt",
"month_november": "November",
"month_nov": "Nov",
"month_december": "Dezember",
"month_dec": "Dez"
}
25 changes: 24 additions & 1 deletion components/nspanel_lovelace/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,28 @@
"return_to_base": "Return to dock",
"docked": "Docked",
"turn_on": "Turn on",
"turn_off": "Turn off"
"turn_off": "Turn off",
"month_january": "January",
"month_jan": "Jan",
"month_february": "February",
"month_feb": "Feb",
"month_march": "March",
"month_mar": "Mar",
"month_april": "April",
"month_apr": "Apr",
"month_may": "May",
"month_june": "June",
"month_jun": "Jun",
"month_july": "July",
"month_jul": "Jul",
"month_august": "August",
"month_aug": "Aug",
"month_september": "September",
"month_sept": "Sep",
"month_october": "October",
"month_oct": "Oct",
"month_november": "November",
"month_nov": "Nov",
"month_december": "December",
"month_dec": "Dec"
}
25 changes: 24 additions & 1 deletion components/nspanel_lovelace/translations/en_GB.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,28 @@
"return_to_base": "Return to dock",
"docked": "Docked",
"turn_on": "Turn on",
"turn_off": "Turn off"
"turn_off": "Turn off",
"month_january": "January",
"month_jan": "Jan",
"month_february": "February",
"month_feb": "Feb",
"month_march": "March",
"month_mar": "Mar",
"month_april": "April",
"month_apr": "Apr",
"month_may": "May",
"month_june": "June",
"month_jun": "Jun",
"month_july": "July",
"month_jul": "Jul",
"month_august": "August",
"month_aug": "Aug",
"month_september": "September",
"month_sept": "Sep",
"month_october": "October",
"month_oct": "Oct",
"month_november": "November",
"month_nov": "Nov",
"month_december": "December",
"month_dec": "Dec"
}

0 comments on commit 2e51338

Please sign in to comment.