preact-www icon indicating copy to clipboard operation
preact-www copied to clipboard

getDefaultLanguage could account for regional fallbacks.

Open SleeplessByte opened this issue 8 years ago • 0 comments

When the documentation website loads, the language pack is set by calling into a library file.

https://github.com/developit/preact-www/blob/a42eb4b44458f107b6b6097806317f5f1f439909/src/lib/default-language.js#L4-L10

The handling is currently blindly adhering the browser values and doesn't account for regional fallbacks. It yields me the french website, whereas my browser is set to English (Great Britain / United Kingdom). If anything, I expected to get the English website.

> navigator.languages
> (9) ["en-GB", "nl-NL", "nl", "fr-BE", "fr", "de-DE", "de", "en-US", "en"]

> navigator.language
> "en-GB"

All you'd have to do is fallback to a language code without a region:

const lang = String(langs[i]).toLowerCase();
if (available[lang]) return lang;
const withoutRegionLang = lang.slice(0, 2);
if (available[withoutRegionLang]) return withoutRegionLang;

Naturally the other way is to "change your browser settings" but let's not forget that there are plenty of instances where you can't change the settings. If my primary language is (a variant) of German, and there is a generic German translation, I'd probably want to get the German generic variant instead of a second, third or lower preference, generic language.

SleeplessByte avatar Jan 09 '18 20:01 SleeplessByte