# Swedish? meta just showed me the UI in Swedish

**URL:** <https://meta.discourse.org/t/swedish-meta-just-showed-me-the-ui-in-swedish/41137>\
**Category:** Bug\
**Created:** [2016年三月16日 13:59 UTC](https://meta.discourse.org/t/swedish-meta-just-showed-me-the-ui-in-swedish/41137 "2016-03-16T13:59:08Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![kpfleming](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/kpfleming/32/116414_2.png) [@kpfleming](https://meta.discourse.org/u/kpfleming)\
**Post date:** [2016年三月16日 13:59 UTC](https://meta.discourse.org/t/swedish-meta-just-showed-me-the-ui-in-swedish/41137/1 "2016-03-16T13:59:08Z")

</div>

I opened my browser, clicked ‘log out’ on the drop-down menu (because my browser doesn’t recover Discourse sessions properly when I log in), and got the anonymous home page. The buttons and top header were in Swedish (“Logga in” instead of “Login”). I do not have Swedish selected in my browser and never have.

---

<div class="post-metadata">

**Author:** ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)\
**Post date:** [2016年三月16日 16:02 UTC](https://meta.discourse.org/t/swedish-meta-just-showed-me-the-ui-in-swedish/41137/2 "2016-03-16T16:02:48Z")

</div>

I’m not sure if this is related, but I’ve noticed on meta that when, as an anonymous use, I change my browser’s language preference, I often need to refresh the page several times before the new locale file is served. Does this have anything to do with the locale file being served through a CDN? On my own forum, without a CDN, the new locale file is served on the first request.

---

<div class="post-metadata">

**Author:** ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)\
**Post date:** [2016年三月16日 19:26 UTC](https://meta.discourse.org/t/swedish-meta-just-showed-me-the-ui-in-swedish/41137/3 "2016-03-16T19:26:43Z")

</div>

This is a strong indicator that anonymous cache middlewere has a missing cache buster for locales

---

<div class="post-metadata">

**Author:** ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)\
**Post date:** [2016年三月16日 20:33 UTC](https://meta.discourse.org/t/swedish-meta-just-showed-me-the-ui-in-swedish/41137/4 "2016-03-16T20:33:16Z")

</div>

Can we add this? Where exactly would it go?

---

<div class="post-metadata">

**Author:** ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)\
**Post date:** [2016年三月16日 20:36 UTC](https://meta.discourse.org/t/swedish-meta-just-showed-me-the-ui-in-swedish/41137/5 "2016-03-16T20:36:40Z")

</div>

I will fix it later today

---

<div class="post-metadata">

**Author:** ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)\
**Post date:** [2016年三月17日 05:41 UTC](https://meta.discourse.org/t/swedish-meta-just-showed-me-the-ui-in-swedish/41137/6 "2016-03-17T05:41:33Z")

</div>

OK this is fiendish to fix and I am not sure what to do:

Trivially we can add `env['HTTP_ACCEPT_LANGUAGE']` to:

> <https://github.com/discourse/discourse/blob/main/lib/middleware/anonymous_cache.rb#L48>

Trouble is that our anonymous cache will automatically become much less efficient, every permutation out there of accept language will force a new entry in the cache.

The anon cache middleware is VERY early in the stack, at that point we don’t even have safe access to Site Settings, so we can’t even figure out if this feature is enabled.

So now, the existence of this feature is forcing us to do extra work for every anon request. Some options:

1. Move enabling and disabling into to GlobalSetting, then people must opt for this extra work on every request.

2. Disable the feature for anon (simplest but not fun)

3. Build a system for reaching into SiteSettings when there is no access to database in a multisite scenario (complicated, but doable)

4. Just shove the accpet header and deal with a terrible anon cache … mine is `HTTP_ACCEPT_LANGUAGE: en-US,en;q=0.8,he;q=0.6` very few have this.

Only 2) is easy, so @codinghorror we need to decide what amount of effort we need to invest here.

I estimate its half a day to a day to get 1 or 3 to work perfectly.

---

<div class="post-metadata">

**Author:** ![riking](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/riking/32/170938_2.png) [@riking](https://meta.discourse.org/u/riking)\
**Post date:** [2016年三月17日 05:59 UTC](https://meta.discourse.org/t/swedish-meta-just-showed-me-the-ui-in-swedish/41137/7 "2016-03-17T05:59:44Z")

</div>

Another option would be to reduce the header to the “top supported language” and cache key off that. So you have ~20-30 variations max, and you might need to keep them around even if the feature’s off.

---

<div class="post-metadata">

**Author:** ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)\
**Post date:** [2016年三月17日 06:01 UTC](https://meta.discourse.org/t/swedish-meta-just-showed-me-the-ui-in-swedish/41137/8 "2016-03-17T06:01:23Z")

</div>

> [@riking](#):
>
> Another option would be to reduce the header to the “top supported language” and cache key off that. So you have ~20-30 variations max

You would reduce it down to the locales discourse supports but it introduces some extra work unconditionally for all anon requests from now on

---

<div class="post-metadata">

**Author:** ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)\
**Post date:** [2016年三月17日 06:46 UTC](https://meta.discourse.org/t/swedish-meta-just-showed-me-the-ui-in-swedish/41137/9 "2016-03-17T06:46:19Z")

</div>

Given that the cost here is so extreme we are

- doing nothing at the moment, leaving the feature default off

- marking the feature as EXPERIMENTAL in the site setting copy description

cc: @simon

---

<div class="post-metadata">

**Author:** ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)\
**Post date:** [2016年三月17日 20:59 UTC](https://meta.discourse.org/t/swedish-meta-just-showed-me-the-ui-in-swedish/41137/10 "2016-03-17T20:59:09Z")

</div>

> [@sam](#):
>
> every permutation out there of accept language will force a new entry in the cache.

It wouldn’t be as bad if a global setting was added _and_ the accept-language header was parsed before it was used in the cache\_key. Something like this?

```rb
# lib/anonymous_locale.rb

class AnonymousLocale
  def self.locale(accept_language_header)
    if GlobalSetting.locale_from_header
      begin
        require 'http_accept_language' unless defined? HttpAcceptLanguage
        available_locales = I18n.available_locales.map { |locale| locale.to_s.gsub(/_/, '-') }
        parser = HttpAcceptLanguage::Parser.new(accept_language_header)
        parser.language_region_compatible_from(available_locales).gsub(/-/, '_')
      rescue
        I18n.default_locale
      end
    else
      I18n.default_locale
    end
  end
end

```

And then in anonymous\_cache:

```rb
def cache_key
  @cache_key ||= "ANON_CACHE_#{@env["HTTP_ACCEPT"]}_#{AnonymousLocale.locale(@env["HTTP_ACCEPT_LANGUAGE"])}_#{@env["HTTP_HOST"]}#{@env["REQUEST_URI"]}|m=#{is_mobile?}|c=#{is_crawler?}"
end

```

---

<div class="post-metadata">

**Author:** ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)\
**Post date:** [2020年十一月3日 06:29 UTC](https://meta.discourse.org/t/swedish-meta-just-showed-me-the-ui-in-swedish/41137/11 "2020-11-03T06:29:42Z")

</div>


