I am trying to add translations to my custom card, especially for the card editor.
In config_flow this is no problem but it looks like that you can not access the language vars from a custom component inside the JavaScript custom card.
I am using the old vanilla JS System, not the TS or LitElements. And if possible, I want to stay with this because I am not familar with this.
my de.json (working in config flow) has this structure
{
"config": {
"error": {
"station_not_found": "Die angegebene Station wurde nicht gefunden. Bitte überprüfen Sie den Namen und versuchen Sie es erneut."
},
"step": {
"user": {
"sections": {
"advanced_options": {
"name": "Erweiterte Einstellungen",
"data": {
"hidename": "Den Namen der Karte (inkl. Uhr) ausblenden",
Now I want to access the hidename
string
const hidenameLabel = this.hass.localize("config.step.user.sections.advanced_options.data.hidename");
console.log("AnotherMVG - hidenameLabel:", hidenameLabel);
but I only get:
AnotherMVG - hidenameLabel: <empty string>
Somehow this is clear because there is no reference to the custom component.
I also played around with a reference like
this.hass.localize("custom_component.another_mvg.config.step.user.sections.advanced_options.data.hidename");
this.hass.localize("custom_components.another_mvg.config.step.user.sections.advanced_options.data.hidename");
but also this is not working
If I check
console.log(this.hass.resources);
there are no translations from custom components available
Localize itself is working, if I try:
console.log(this.hass.localize("ui.card.weather.twice_daily"));
I get a valid translation
If I try to load the de.json by my own
async loadTranslations() {
const language = this.hass?.language || "en";
try {
const response = await fetch(`/local/custom_components/another_mvg/translations/${language}.json`);
console.warn("Translation file loaded. ", response);
return response.ok ? await response.json() : {};
} catch (error) {
console.warn("Translation not loaded.");
return {};
}
}
of course this will not work because its the server path
If I try it with
const response = await fetch(`/custom_components/another_mvg/translations/${language}.json`);
or
const response = await fetch(`/another_mvg/translations/${language}.json`);
I am also not able to load the file, I get the 404
Any idea how to access the own translation ?
I theory it could be that easy if HA loads all translations from custom components and add a custom_component.name_of_the_addon.
in front like custom_component.another_mvg.