Class: ExtraLocalesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/extra_locales_controller.rb

Constant Summary

Constants inherited from ApplicationController

ApplicationController::LEGACY_NO_THEMES, ApplicationController::LEGACY_NO_UNOFFICIAL_PLUGINS, ApplicationController::NO_PLUGINS, ApplicationController::NO_THEMES, ApplicationController::NO_UNOFFICIAL_PLUGINS, ApplicationController::SAFE_MODE

Constants included from CanonicalURL::ControllerExtensions

CanonicalURL::ControllerExtensions::ALLOWED_CANONICAL_PARAMS

Instance Attribute Summary

Attributes inherited from ApplicationController

#theme_id

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationController

#application_layout, #can_cache_content?, #clear_notifications, #conditionally_allow_site_embedding, #current_homepage, #discourse_expires_in, #dont_cache_page, #ember_cli_required?, #fetch_user_from_params, #guardian, #handle_permalink, #handle_theme, #handle_unverified_request, #has_escaped_fragment?, #immutable_for, #no_cookies, #perform_refresh_session, #post_ids_including_replies, #preload_json, #rate_limit_second_factor!, #redirect_with_client_support, #render_json_dump, #render_serialized, requires_plugin, #rescue_discourse_actions, #resolve_safe_mode, #secure_session, #serialize_data, #set_current_user_for_logs, #set_layout, #set_mobile_view, #set_mp_snapshot_fields, #show_browser_update?, #store_preloaded, #use_crawler_layout?, #with_resolved_locale

Methods included from VaryHeader

#ensure_vary_header

Methods included from ReadOnlyMixin

#add_readonly_header, #allowed_in_staff_writes_only_mode?, #block_if_readonly_mode, #check_readonly_mode, included, #staff_writes_only_mode?

Methods included from Hijack

#hijack

Methods included from GlobalPath

#cdn_path, #cdn_relative_path, #full_cdn_url, #path, #upload_cdn_path

Methods included from JsonError

#create_errors_json

Methods included from CanonicalURL::ControllerExtensions

#canonical_url, #default_canonical, included

Methods included from CurrentUser

#clear_current_user, #current_user, has_auth_cookie?, #is_api?, #is_user_api?, #log_off_user, #log_on_user, lookup_from_env, #refresh_session

Class Method Details

.bundle_js(bundle) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'app/controllers/extra_locales_controller.rb', line 55

def self.bundle_js(bundle)
  locale_str = I18n.locale.to_s
  bundle_str = "#{bundle}_js"

  if bundle == OVERRIDES_BUNDLE
    JsLocaleHelper.output_client_overrides(locale_str)
  else
    JsLocaleHelper.output_extra_locales(bundle_str, locale_str)
  end
end

.bundle_js_hash(bundle) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/controllers/extra_locales_controller.rb', line 31

def self.bundle_js_hash(bundle)
  if bundle == OVERRIDES_BUNDLE
    site = RailsMultisite::ConnectionManagement.current_db

    @by_site ||= {}
    @by_site[site] ||= {}
    @by_site[site][I18n.locale] ||= begin
      js = bundle_js(bundle)
      js.present? ? Digest::MD5.hexdigest(js) : nil
    end
  else
    @bundle_js_hash ||= {}
    @bundle_js_hash["#{bundle}_#{I18n.locale}"] ||= Digest::MD5.hexdigest(bundle_js(bundle))
  end
end

.clear_cache!Object



66
67
68
69
# File 'app/controllers/extra_locales_controller.rb', line 66

def self.clear_cache!
  site = RailsMultisite::ConnectionManagement.current_db
  @by_site&.delete(site)
end

.client_overrides_exist?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'app/controllers/extra_locales_controller.rb', line 51

def self.client_overrides_exist?
  bundle_js_hash(OVERRIDES_BUNDLE).present?
end

.url(bundle) ⇒ Object



47
48
49
# File 'app/controllers/extra_locales_controller.rb', line 47

def self.url(bundle)
  "#{Discourse.base_path}/extra-locales/#{bundle}?v=#{bundle_js_hash(bundle)}"
end

Instance Method Details

#showObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/controllers/extra_locales_controller.rb', line 14

def show
  bundle = params[:bundle]
  raise Discourse::InvalidAccess.new if !valid_bundle?(bundle)

  version = params[:v]
  if version.present?
    if version.kind_of?(String) && version.length == MD5_HASH_LENGTH
      hash = ExtraLocalesController.bundle_js_hash(bundle)
      immutable_for(1.year) if hash == version
    else
      raise Discourse::InvalidParameters.new(:v)
    end
  end

  render plain: ExtraLocalesController.bundle_js(bundle), content_type: "application/javascript"
end