Add support for native names for other languages than HA has (#16393)

* Add support for native names for other languages than HA has

* catch unsupported locales
This commit is contained in:
Bram Kragten 2023-05-03 13:54:59 +02:00 committed by GitHub
parent 4a0d84d2f6
commit fc97ca324c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -68,10 +68,24 @@ export class HaLanguagePicker extends LitElement {
if (nativeName) {
const translations = this.hass.translationMetadata.translations;
options = languages.map((lang) => ({
value: lang,
label: translations[lang]?.nativeName ?? lang,
}));
options = languages.map((lang) => {
let label = translations[lang]?.nativeName;
if (!label) {
try {
// this will not work if Intl.DisplayNames is polyfilled, it will return in the language of the user
label = new Intl.DisplayNames(lang, {
type: "language",
fallback: "code",
}).of(lang)!;
} catch (err) {
label = lang;
}
}
return {
value: lang,
label,
};
});
} else {
options = languages.map((lang) => ({
value: lang,