Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions src/util/hass-translation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,18 @@ export function findAvailableLanguage(language: string) {
return LOCALE_LOOKUP[langLower];
}

return Object.keys(translationMetadata.translations).find(
const translation = Object.keys(translationMetadata.translations).find(
(lang) => lang.toLowerCase() === langLower
);
if (translation) {
return translation;
}

if (language.includes("-")) {
return findAvailableLanguage(language.split("-")[0]);
}

return undefined;
}

/**
Expand Down Expand Up @@ -94,13 +103,6 @@ export function getLocalLanguage() {
if (language) {
return language;
}
if (navigator.language && navigator.language.includes("-")) {
language = findAvailableLanguage(navigator.language.split("-")[0]);
if (language) {
return language;
}
}
Comment on lines -97 to -102
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think adding it to navigator.languages makes sense, but we should not remove it here? We should instead make a helper function for:

      language = findAvailableLanguage(locale);
      if (language) {
        return language;
      }
      if (locale.includes("-")) {
        language = findAvailableLanguage(locale.split("-")[0]);
        if (language) {
          return language;
        }
      }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I see. I misunderstood what navigator.language was. I will make a method and call it in both places.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made a change that is a little different. I couldn't think of a good name for the helper function, and it seemed like something we would want to always check when searching for an available translation, so I added it to that function.

I'll understand if you prefer not to have the recursive call there and want me to break it out differently.

This is my first time doing a CR on github, first time submitting to HA (or any OS project, for that matter), and first time working with TS, so I'm not super familiar with the industry best practices, nor with the project best practices. I am more than happy to make changes to make it

I am in the discord (@Zondebok) if you want to discuss directly.


// Final fallback
return "en";
}
Expand Down