How to show block based on browser language? In Drupal 8, I would like to show a block to a visitor based on their browser language (I guess based on the Accept-Language HTTP header).

The page should stay in English but there should be something like "this site is available in your language, click here" (translated into the relevant language, of course).
I don't think this is possible without custom code if you want to do this differently from the current language.

However, you don't need to do all the heavy lifting yourself. See LanguageNegotiationBrowser and the code there, you should be able to more or less copy that 1:1.

Then it will only do it if the language actually exists, respects language mappings and so on. You just have to call that, check if it's different from the current language and if so, display something.

However, properly caching that is going to be fun:

You need a cache context for an arbitrary http header, similar to CookiesCacheContext. so something like http_header:HTTP_ACCEPT_LANGUAGE as a cache context.

That will however not work for anonymous page cache, since that doesn't check cache contexts at all. To work around that, you need your own page cache implementation, that caches by that header, which will obviously result in lots and lots of variations.

So.. maybe you should just do this completely in JS?