Add i18n support #5

Closed
stne3960 wants to merge 13 commits from basic-scaffolding-frontend-lingui into basic-scaffolding-frontend
Showing only changes of commit fb9f1cab07 - Show all commits

View File

@ -1,9 +1,15 @@
const supportedLocales = ["sv", "en"];
export function getDefaultLocale(): string {
const lang = navigator.language.split("-")[0]; // "en-US" -> "en"
if (supportedLocales.includes(lang)) {
return lang;
const userPreferredLangs = navigator.languages.map(
stne3960 marked this conversation as resolved Outdated

Should maybe iterate over navigator.languages instead to find the most preferred locale? If a user sends "Accept-Language: de-DE, en-US, sv-SE" they will get Swedish instead of English since German isn't available.

Also should we maybe default to English if no available language is found instead of Swedish?

Should maybe iterate over `navigator.languages` instead to find the most preferred locale? If a user sends "Accept-Language: de-DE, en-US, sv-SE" they will get Swedish instead of English since German isn't available. Also should we maybe default to English if no available language is found instead of Swedish?

I agree that we should interate over navigator.languages instead.

Regarding defaulting to english if no language is found, very good question. We are Swedish public agency, most outward facing systems default to Swedish, but on the other hand, if they don't have Swedish as an accepted language, there's probably a high likelyhood that they are non-Swedish speakers. I think there's a strong case here to default to English.

I agree that we should interate over `navigator.languages` instead. Regarding defaulting to english if no language is found, very good question. We are Swedish public agency, most outward facing systems default to Swedish, but on the other hand, if they don't have Swedish as an accepted language, there's probably a high likelyhood that they are non-Swedish speakers. I think there's a strong case here to default to English.

Based on that I'd say defaulting to English makes sense and adhering to the "Accept-Language" header is just nice for users.

- https://www.su.se defaults to Swedish no matter what even if sending "Accept-Language: en-US". - https://scipro.dsv.su.se is only available in English. - https://daisy.dsv.su.se defaults to English and adheres to the "Accept-Language" header. - https://nextilearn.dsv.su.se defaults to English and seems to ignore the "Accept-Language" header. Based on that I'd say defaulting to English makes sense and adhering to the "Accept-Language" header is just nice for users.
(lang) => lang.split("-")[0],
);
for (const lang of userPreferredLangs) {
if (supportedLocales.includes(lang)) {
return lang;
}
}
return "sv"; // fallback to Swedish
return "en"; // fallback to English
}