• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

MushroomObserver / mushroom-observer / 15185724317

22 May 2025 11:46AM UTC coverage: 94.731% (-0.006%) from 94.737%
15185724317

push

github

web-flow
Merge pull request #3009 from MushroomObserver/njw-letsencrypt-fix-2

Allow robots to see account/login

1 of 1 new or added line in 1 file covered. (100.0%)

2 existing lines in 1 file now uncovered.

34127 of 36025 relevant lines covered (94.73%)

511.22 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

68.18
/app/controllers/application_controller/internationalization.rb
1
# frozen_string_literal: true
2

3
# see application_controller.rb
4
module ApplicationController::Internationalization
1✔
5
  ##############################################################################
6
  #
7
  #  :section: Internationalization
8
  #
9
  ##############################################################################
10

11
  # Before filter: Decide which locale to use for this request.  Sets the
12
  # Globalite default.  Tries to get the locale from:
13
  #
14
  # 1. parameters (user clicked on language in bottom left)
15
  # 2. user prefs (user edited their preferences)
16
  # 3. session (whatever we used last time)
17
  # 4. navigator (provides default)
18
  # 5. server (MO.default_locale)
19
  #
20
  def set_locale
1✔
21
    lang = Language.find_by(locale: specified_locale) || Language.official
3,540✔
22

23
    # Only change the Locale code if it needs changing.  There is about a 0.14
24
    # second performance hit every time we change it... even if we're only
25
    # changing it to what it already is!!
26
    change_locale_if_needed(lang.locale)
3,540✔
27

28
    # Update user preference.
29
    @user.update(locale: lang.locale) if @user && @user.locale != lang.locale
3,540✔
30

31
    logger.debug("[I18n] Locale set to #{I18n.locale}")
3,540✔
32

33
    # Tell Rails to continue to process request.
34
    true
3,540✔
35
  end
36

37
  def specified_locale
1✔
38
    params_locale || prefs_locale || session_locale || browser_locale
3,540✔
39
  end
40

41
  def params_locale
1✔
42
    return unless params[:user_locale]
3,540✔
43

44
    logger.debug("[I18n] loading locale: #{params[:user_locale]} from params")
2✔
45
    params[:user_locale]
2✔
46
  end
47

48
  def prefs_locale
1✔
49
    return unless @user&.locale.present? && params[:controller] != "ajax"
3,538✔
50

51
    logger.debug("[I18n] loading locale: #{@user.locale} from @user")
2,656✔
52
    @user.locale
2,656✔
53
  end
54

55
  def session_locale
1✔
56
    return unless session[:locale]
882✔
57

UNCOV
58
    logger.debug("[I18n] loading locale: #{session[:locale]} from session")
×
UNCOV
59
    session[:locale]
×
60
  end
61

62
  def browser_locale
1✔
63
    return unless (locale = valid_locale_from_request_header)
882✔
64

65
    logger.debug("[I18n] loading locale: #{locale} from request header")
882✔
66
    locale
882✔
67
  end
68

69
  def change_locale_if_needed(new_locale)
1✔
70
    return if I18n.locale.to_s == new_locale
3,540✔
71

72
    I18n.locale = new_locale
5✔
73
    session[:locale] = new_locale
5✔
74
  end
75

76
  # Before filter: Set timezone based on cookie set in application layout.
77
  def set_timezone
1✔
78
    tz = cookies[:tz]
3,381✔
79
    if tz.present?
3,381✔
80
      begin
81
        Time.zone = tz
×
82
      rescue StandardError
83
        logger.warn("TimezoneError: #{tz.inspect}")
×
84
      end
85
    end
86
    @js = js_enabled?(tz)
3,381✔
87
  end
88

89
  # Until we get rid of reliance on @js, this is a surrogate for
90
  # testing if the client's JS is enabled and sufficiently fully-featured.
91
  def js_enabled?(time_zone)
1✔
92
    time_zone.present? || Rails.env.test?
3,381✔
93
  end
94

95
  # Return Array of the browser's requested locales (HTTP_ACCEPT_LANGUAGE).
96
  # Example syntax:
97
  #
98
  #   en-au,en-gb;q=0.8,en;q=0.5,ja;q=0.3
99
  #
100
  def sorted_locales_from_request_header
1✔
101
    accepted_locales = request.env["HTTP_ACCEPT_LANGUAGE"]
882✔
102
    logger.debug("[globalite] HTTP header = #{accepted_locales.inspect}")
882✔
103
    return [] if accepted_locales.blank?
882✔
104

105
    locale_weights = map_locales_to_weights(accepted_locales)
×
106
    # Sort by decreasing weights.
107
    result = locale_weights.sort { |a, b| b[1] <=> a[1] }.pluck(0)
×
108
    logger.debug("[globalite] client accepted locales: #{result.join(", ")}")
×
109
    result
×
110
  end
111

112
  # Extract locales and weights, creating map from locale to weight.
113
  def map_locales_to_weights(locales)
1✔
114
    locales.split(",").each_with_object({}) do |term, loc_wts|
×
115
      next unless "#{term};q=1" =~ /^(.+?);q=([^;]+)/
×
116

117
      loc_wts[Regexp.last_match(1)] = (begin
×
118
                                         Regexp.last_match(2).to_f
×
119
                                       rescue StandardError
120
                                         -1.0
×
121
                                       end)
122
    end
123
  end
124

125
  # Returns our locale that best suits the HTTP_ACCEPT_LANGUAGE request header.
126
  # Returns a String, or <tt>nil</tt> if no valid match found.
127
  def valid_locale_from_request_header
1✔
128
    # Get list of languages browser requested, sorted in the order it prefers
129
    # them.
130
    requested_locales = sorted_locales_from_request_header.map do |locale|
882✔
131
      if locale =~ /^(\w\w)-(\w+)$/
×
132
        Regexp.last_match(1).downcase
×
133
      else
134
        locale.downcase
×
135
      end
136
    end
137

138
    # Lookup the closest match based on the given request priorities.
139
    lookup_valid_locale(requested_locales)
882✔
140
  end
141

142
  # Lookup the closest match based on the given request priorities.
143
  def lookup_valid_locale(requested_locales)
1✔
144
    requested_locales.each do |locale|
882✔
145
      logger.debug("[globalite] trying to match locale: #{locale}")
×
146
      language = locale.split("-").first
×
147
      next unless I18n.available_locales.include?(language.to_sym)
×
148

149
      logger.debug("[globalite] language match: #{language}")
×
150
      return language
×
151
    end
152
    "en"
882✔
153
  end
154

155
  private :js_enabled?, :map_locales_to_weights, :lookup_valid_locale
1✔
156
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc