Comments (6)
It falls back to "en" if selectedLanguage
is not set in localStorage
, requiring hass
in the function means that you need to pass down that object everywhere you might want translations.
from boilerplate-card.
@ludeeus agreed that it is not desirable to have the localize
function depending on the hass
object.
But on the other hand, with the current solution users need to make sure that for each device/browser they have set the HA language, or the variable is not present in localStorage
, and it falls back to english as you mentioned.
This fallback is not very user-friendly, as the card is perceived as inconsistent/buggy if the translation is not the same for all their devices.
As a workaround, a custom card could implement a 'check' to see if the language is present in localStorage
and if not, set it.
This check could be done in the set hass
method.
I don't think this is a good way of working though, as i would say you don't want custom cards to 'mess' with the localStorage
.
Ideally i'd like to see that the HA frontend would implement such a check, such that Lovelace cards can rely on localStorage
.
Curious about your opinion :)
from boilerplate-card.
Did some checks, even if you where to use hass.selectedLanguage
, it would result in the same.
This is from my installation
something needs to be the fallback, and for now, it looks like the current implementation is more stable.
from boilerplate-card.
I did some testing as well.
Here's what i did.
I wrapped the line that detects the language in a function:
function getLanguage(): string {
return (localStorage.getItem('selectedLanguage') || 'en').replace(/['"]+/g, '').replace('-', '_');
}
Apparently, getLanguage()
actually returns 'null'
(as a string, not actual null
).
Don't know why, but multiple users of my custom card reported the same (my card does a console log of the function output).
Also, the hass
object has a selectedLanguage
and a language
property. Don't ask me about the difference.
So i made the workaround like this:
set hass(hass) {
if (!getLanguage() || getLanguage() == "null" && hass.selectedLanguage) {
localStorage.setItem("selectedLanguage", hass.selectedLanguage);
}
else if (!getLanguage() || getLanguage() == "null" && hass.language) {
localStorage.setItem("selectedLanguage", hass.language);
}
}
With this workaround in place, users who previously had issues with language not being detected, are now reporting that the card translated fine to the language of their HA config.
I cannot really connect the dots here, but if it works, it works.
Again, I don't like having to do this workaround, I would prefer to be able to rely on the localStorage
variable to be set.
I'd say there is definitely room for improvement here.
from boilerplate-card.
There is no use in setting something that holds the default value.
I think this should work in most cases:
let lang = localStorage.getItem('selectedLanguage')?.replace(/['"]+/g, '').replace('-', '_');
if (!lang) {
const _hass = document.querySelector("home-assistant").hass
lang = hass.selectedLanguage || hass.language || "en"
}
from boilerplate-card.
I added your suggestion. Works fine, thanks!
It seems like a good best of both worlds solution.
I'm closing this issue, as it is not really a bug.
Maybe consider to add it to the boilerplate card, as it is an improvement that other developers could benefit from as well?
Thanks for the discussion
from boilerplate-card.
Related Issues (20)
- " and handle the changes/suggestions HOT 1
- Support for templating HOT 6
- Update template to use lastest version of custom-card-helpers HOT 5
- Better documentation HOT 1
- Undefined entity when loading Card HOT 3
- `paper-dropdown-menu` custom element doesnt work anymore with lazy loading HOT 8
- Error when removing config properties HOT 1
- Request: Upgrade lit-element to lit? HOT 1
- Outdated Prettier version breaks on part of the card file and doesn't auto-format HOT 1
- This is undefined
- you are still relying on mwc elementes to be defined globally by HA HOT 7
- Add the ability to use sliders
- Changes not being detected
- `container start` fails - " unable to connect to github.com" - Replace `git://` by `https://` ? HOT 3
- Code Splitting problem when building HOT 1
- Installing Home Assistant in devcontainer fails HOT 1
- Bring the boilerplate-card to a modern version HOT 1
- New visual editor for card config HOT 1
- add info.md and hacs.json HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from boilerplate-card.