Skip to content

Commit 50001f9

Browse files
authored
Feat/respect Home Assistant locale (#230)
* respect user set locale in `hours_to_show: current_week` * Added support for plotly locales
1 parent 226647a commit 50001f9

File tree

5 files changed

+120
-0
lines changed

5 files changed

+120
-0
lines changed

readme.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ Or make space inside the the plot like this:
296296
```yaml
297297
time_offset: 3h
298298
```
299+
299300
## Offsets
300301

301302
Offsets are useful to shift data in the temporal axis. For example, if you have a sensor that reports the forecasted temperature 3 hours from now, it means that the current value should be plotted in the future. With the `time_offset` attribute you can shift the data so it is placed in the correct position.
@@ -952,6 +953,19 @@ refresh_interval: 0 # never update.
952953
refresh_interval: 5 # update every 5 seconds
953954
```
954955

956+
## localization:
957+
958+
The locale is directly taken from Home Assistant's configuration, but can be overridden like this:
959+
960+
```yaml
961+
config:
962+
locale: ar
963+
```
964+
965+
** Home Assistant custom Number and Date format will be ignored, only the language determines the locale **
966+
967+
When using `hours_to_show: current_week`, the "First day of the week" configured in Home Assistant is used
968+
955969
# deprecations:
956970

957971
### `no_theme`

src/duration/duration.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { HomeAssistant } from "custom-card-helpers";
12
import {
23
endOfDay,
34
endOfHour,
@@ -6,6 +7,7 @@ import {
67
endOfQuarter,
78
endOfWeek,
89
endOfYear,
10+
setDefaultOptions,
911
startOfDay,
1012
startOfHour,
1113
startOfMinute,
@@ -63,6 +65,28 @@ export const isTimeDuration = (str: any) => {
6365
}
6466
};
6567

68+
export const setDateFnDefaultOptions = (hass: HomeAssistant) => {
69+
const first_weekday: "sunday" | "saturday" | "monday" | "language" = (
70+
hass.locale as any
71+
).first_weekday;
72+
const weekStartsOn = (
73+
{
74+
language: undefined,
75+
sunday: 0,
76+
monday: 1,
77+
tuesday: 2,
78+
wednesday: 3,
79+
thursday: 4,
80+
friday: 5,
81+
saturday: 6,
82+
} as const
83+
)[first_weekday];
84+
85+
setDefaultOptions({
86+
locale: { code: hass.locale.language },
87+
weekStartsOn,
88+
});
89+
};
6690
export const parseRelativeTime = (str: string): [number, number] => {
6791
const now = new Date();
6892
switch (str) {
@@ -89,6 +113,7 @@ export const isRelativeTime = (str: any) => {
89113
parseRelativeTime(str);
90114
return true;
91115
} catch (e) {
116+
console.log(e);
92117
return false;
93118
}
94119
};

src/parse-config/defaults.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ const defaultYamlOptional: {
8787
displaylogo: false,
8888
scrollZoom: true,
8989
modeBarButtonsToRemove: ["resetScale2d", "toImage", "lasso2d", "select2d"],
90+
locale: ({ hass }) => hass.locale?.language,
9091
},
9192
layout: {
9293
height: 285,

src/parse-config/parse-config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
isTimeDuration,
1111
parseRelativeTime,
1212
parseTimeDuration,
13+
setDateFnDefaultOptions,
1314
} from "../duration/duration";
1415
import { parseStatistics } from "./parse-statistics";
1516
import { HomeAssistant } from "custom-card-helpers";
@@ -59,6 +60,7 @@ class ConfigParser {
5960
this.errors = [];
6061
this.hass = hass;
6162
this.yaml_with_defaults = addPreParsingDefaults(input_yaml, css_vars);
63+
setDateFnDefaultOptions(hass);
6264

6365
this.fnParam = {
6466
vars: {},

src/plotly.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,84 @@ Plotly.register([
5959

6060
// components
6161
require("plotly.js/lib/calendars"),
62+
63+
// locales
64+
require("plotly.js/lib/locales/af.js"),
65+
require("plotly.js/lib/locales/am.js"),
66+
require("plotly.js/lib/locales/ar-dz.js"),
67+
require("plotly.js/lib/locales/ar-eg.js"),
68+
require("plotly.js/lib/locales/ar.js"),
69+
require("plotly.js/lib/locales/az.js"),
70+
require("plotly.js/lib/locales/bg.js"),
71+
require("plotly.js/lib/locales/bs.js"),
72+
require("plotly.js/lib/locales/ca.js"),
73+
require("plotly.js/lib/locales/cs.js"),
74+
require("plotly.js/lib/locales/cy.js"),
75+
require("plotly.js/lib/locales/da.js"),
76+
require("plotly.js/lib/locales/de-ch.js"),
77+
require("plotly.js/lib/locales/de.js"),
78+
require("plotly.js/lib/locales/el.js"),
79+
require("plotly.js/lib/locales/eo.js"),
80+
require("plotly.js/lib/locales/es-ar.js"),
81+
require("plotly.js/lib/locales/es-pe.js"),
82+
require("plotly.js/lib/locales/es.js"),
83+
require("plotly.js/lib/locales/et.js"),
84+
require("plotly.js/lib/locales/eu.js"),
85+
require("plotly.js/lib/locales/fa.js"),
86+
require("plotly.js/lib/locales/fi.js"),
87+
require("plotly.js/lib/locales/fo.js"),
88+
require("plotly.js/lib/locales/fr-ch.js"),
89+
require("plotly.js/lib/locales/fr.js"),
90+
require("plotly.js/lib/locales/gl.js"),
91+
require("plotly.js/lib/locales/gu.js"),
92+
require("plotly.js/lib/locales/he.js"),
93+
require("plotly.js/lib/locales/hi-in.js"),
94+
require("plotly.js/lib/locales/hr.js"),
95+
require("plotly.js/lib/locales/hu.js"),
96+
require("plotly.js/lib/locales/hy.js"),
97+
require("plotly.js/lib/locales/id.js"),
98+
require("plotly.js/lib/locales/is.js"),
99+
require("plotly.js/lib/locales/it.js"),
100+
require("plotly.js/lib/locales/ja.js"),
101+
require("plotly.js/lib/locales/ka.js"),
102+
require("plotly.js/lib/locales/km.js"),
103+
require("plotly.js/lib/locales/ko.js"),
104+
require("plotly.js/lib/locales/lt.js"),
105+
require("plotly.js/lib/locales/lv.js"),
106+
require("plotly.js/lib/locales/me-me.js"),
107+
require("plotly.js/lib/locales/me.js"),
108+
require("plotly.js/lib/locales/mk.js"),
109+
require("plotly.js/lib/locales/ml.js"),
110+
require("plotly.js/lib/locales/ms.js"),
111+
require("plotly.js/lib/locales/mt.js"),
112+
require("plotly.js/lib/locales/nl-be.js"),
113+
require("plotly.js/lib/locales/nl.js"),
114+
require("plotly.js/lib/locales/no.js"),
115+
require("plotly.js/lib/locales/pa.js"),
116+
require("plotly.js/lib/locales/pl.js"),
117+
require("plotly.js/lib/locales/pt-br.js"),
118+
require("plotly.js/lib/locales/pt-pt.js"),
119+
require("plotly.js/lib/locales/rm.js"),
120+
require("plotly.js/lib/locales/ro.js"),
121+
require("plotly.js/lib/locales/ru.js"),
122+
require("plotly.js/lib/locales/si.js"),
123+
require("plotly.js/lib/locales/sk.js"),
124+
require("plotly.js/lib/locales/sl.js"),
125+
require("plotly.js/lib/locales/sq.js"),
126+
require("plotly.js/lib/locales/sr-sr.js"),
127+
require("plotly.js/lib/locales/sr.js"),
128+
require("plotly.js/lib/locales/sv.js"),
129+
require("plotly.js/lib/locales/sw.js"),
130+
require("plotly.js/lib/locales/ta.js"),
131+
require("plotly.js/lib/locales/th.js"),
132+
require("plotly.js/lib/locales/tr.js"),
133+
require("plotly.js/lib/locales/tt.js"),
134+
require("plotly.js/lib/locales/uk.js"),
135+
require("plotly.js/lib/locales/ur.js"),
136+
require("plotly.js/lib/locales/vi.js"),
137+
require("plotly.js/lib/locales/zh-cn.js"),
138+
require("plotly.js/lib/locales/zh-hk.js"),
139+
require("plotly.js/lib/locales/zh-tw.js"),
62140
]);
63141

64142
export default Plotly;

0 commit comments

Comments
 (0)