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

MushroomObserver / mushroom-observer / 19713144386

26 Nov 2025 06:02PM UTC coverage: 95.185% (-0.005%) from 95.19%
19713144386

push

github

web-flow
Merge pull request #3509 from MushroomObserver/nimmo-name-misspelling-bug-fix

Fix name "edit misspelling" flash message "no changes made" bug

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

2 existing lines in 1 file now uncovered.

30780 of 32337 relevant lines covered (95.19%)

693.85 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
#  ==== Internationalization
4
#  all_locales::            Array of available locales for which we have
5
#                           translations.
6
#  set_locale::             (filter: determine which locale is requested)
7
#  set_timezone::           (filter: Set timezone from cookie set by client's
8
#                            browser.)
9
#  sorted_locales_from_request_header::
10
#                           (parse locale preferences from request header)
11
#  valid_locale_from_request_header::
12
#                           (choose locale that best matches request header)
13
#
14
module ApplicationController::Internationalization
1✔
15
  ##############################################################################
16
  #
17
  #  :section: Internationalization
18
  #
19
  ##############################################################################
20

21
  # Before filter: Decide which locale to use for this request.  Sets the
22
  # Globalite default.  Tries to get the locale from:
23
  #
24
  # 1. parameters (user clicked on language in bottom left)
25
  # 2. user prefs (user edited their preferences)
26
  # 3. session (whatever we used last time)
27
  # 4. navigator (provides default)
28
  # 5. server (MO.default_locale)
29
  #
30
  def set_locale
1✔
31
    lang = Language.find_by(locale: specified_locale) || Language.official
3,905✔
32

33
    # Only change the Locale code if it needs changing.  There is about a 0.14
34
    # second performance hit every time we change it... even if we're only
35
    # changing it to what it already is!!
36
    change_locale_if_needed(lang.locale)
3,905✔
37

38
    # Update user preference.
39
    @user.update(locale: lang.locale) if @user && @user.locale != lang.locale
3,905✔
40

41
    logger.debug("[I18n] Locale set to #{I18n.locale}")
3,905✔
42

43
    # Tell Rails to continue to process request.
44
    true
3,905✔
45
  end
46

47
  def specified_locale
1✔
48
    params_locale || prefs_locale || session_locale || browser_locale
3,905✔
49
  end
50

51
  def params_locale
1✔
52
    return unless params[:user_locale]
3,905✔
53

54
    logger.debug("[I18n] loading locale: #{params[:user_locale]} from params")
2✔
55
    params[:user_locale]
2✔
56
  end
57

58
  def prefs_locale
1✔
59
    return unless @user&.locale.present? && params[:controller] != "ajax"
3,903✔
60

61
    logger.debug("[I18n] loading locale: #{@user.locale} from @user")
2,920✔
62
    @user.locale
2,920✔
63
  end
64

65
  def session_locale
1✔
66
    return unless session[:locale]
983✔
67

UNCOV
68
    logger.debug("[I18n] loading locale: #{session[:locale]} from session")
×
UNCOV
69
    session[:locale]
×
70
  end
71

72
  def browser_locale
1✔
73
    return unless (locale = valid_locale_from_request_header)
983✔
74

75
    logger.debug("[I18n] loading locale: #{locale} from request header")
983✔
76
    locale
983✔
77
  end
78

79
  def change_locale_if_needed(new_locale)
1✔
80
    return if I18n.locale.to_s == new_locale
3,905✔
81

82
    I18n.locale = new_locale
5✔
83
    session[:locale] = new_locale
5✔
84
  end
85

86
  # Before filter: Set timezone based on cookie set in application layout.
87
  def set_timezone
1✔
88
    tz = cookies[:tz]
3,745✔
89
    if tz.present?
3,745✔
90
      begin
91
        Time.zone = tz
×
92
      rescue StandardError
93
        logger.warn("TimezoneError: #{tz.inspect}")
×
94
      end
95
    end
96
    @js = js_enabled?(tz)
3,745✔
97
  end
98

99
  # Until we get rid of reliance on @js, this is a surrogate for
100
  # testing if the client's JS is enabled and sufficiently fully-featured.
101
  def js_enabled?(time_zone)
1✔
102
    time_zone.present? || Rails.env.test?
3,745✔
103
  end
104

105
  # Return Array of the browser's requested locales (HTTP_ACCEPT_LANGUAGE).
106
  # Example syntax:
107
  #
108
  #   en-au,en-gb;q=0.8,en;q=0.5,ja;q=0.3
109
  #
110
  def sorted_locales_from_request_header
1✔
111
    accepted_locales = request.env["HTTP_ACCEPT_LANGUAGE"]
983✔
112
    logger.debug("[globalite] HTTP header = #{accepted_locales.inspect}")
983✔
113
    return [] if accepted_locales.blank?
983✔
114

115
    locale_weights = map_locales_to_weights(accepted_locales)
×
116
    # Sort by decreasing weights.
117
    result = locale_weights.sort { |a, b| b[1] <=> a[1] }.pluck(0)
×
118
    logger.debug("[globalite] client accepted locales: #{result.join(", ")}")
×
119
    result
×
120
  end
121

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

127
      loc_wts[Regexp.last_match(1)] = (begin
×
128
                                         Regexp.last_match(2).to_f
×
129
                                       rescue StandardError
130
                                         -1.0
×
131
                                       end)
132
    end
133
  end
134

135
  # Returns our locale that best suits the HTTP_ACCEPT_LANGUAGE request header.
136
  # Returns a String, or <tt>nil</tt> if no valid match found.
137
  def valid_locale_from_request_header
1✔
138
    # Get list of languages browser requested, sorted in the order it prefers
139
    # them.
140
    requested_locales = sorted_locales_from_request_header.map do |locale|
983✔
141
      if locale =~ /^(\w\w)-(\w+)$/
×
142
        Regexp.last_match(1).downcase
×
143
      else
144
        locale.downcase
×
145
      end
146
    end
147

148
    # Lookup the closest match based on the given request priorities.
149
    lookup_valid_locale(requested_locales)
983✔
150
  end
151

152
  # Lookup the closest match based on the given request priorities.
153
  def lookup_valid_locale(requested_locales)
1✔
154
    requested_locales.each do |locale|
983✔
155
      logger.debug("[globalite] trying to match locale: #{locale}")
×
156
      language = locale.split("-").first
×
157
      next unless I18n.available_locales.include?(language.to_sym)
×
158

159
      logger.debug("[globalite] language match: #{language}")
×
160
      return language
×
161
    end
162
    "en"
983✔
163
  end
164

165
  private :js_enabled?, :map_locales_to_weights, :lookup_valid_locale
1✔
166
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