2018-01-08 18:23:49 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-25 16:33:29 -04:00
|
|
|
class ExtraLocalesController < ApplicationController
|
2023-12-06 07:19:09 -05:00
|
|
|
layout false
|
2019-04-30 02:58:18 -04:00
|
|
|
|
|
|
|
skip_before_action :check_xhr,
|
|
|
|
:preload_json,
|
|
|
|
:redirect_to_login_if_required,
|
2024-06-25 07:32:18 -04:00
|
|
|
:redirect_to_profile_if_required,
|
2019-04-30 02:58:18 -04:00
|
|
|
:verify_authenticity_token
|
2016-08-25 16:33:29 -04:00
|
|
|
|
2019-11-04 20:15:44 -05:00
|
|
|
OVERRIDES_BUNDLE ||= "overrides"
|
2019-11-11 16:30:31 -05:00
|
|
|
MD5_HASH_LENGTH ||= 32
|
2019-11-04 20:15:44 -05:00
|
|
|
|
2016-08-25 16:33:29 -04:00
|
|
|
def show
|
|
|
|
bundle = params[:bundle]
|
2019-11-04 20:15:44 -05:00
|
|
|
raise Discourse::InvalidAccess.new if !valid_bundle?(bundle)
|
2019-08-20 06:38:46 -04:00
|
|
|
|
2019-11-11 16:30:31 -05:00
|
|
|
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
|
2018-01-08 18:23:49 -05:00
|
|
|
end
|
2019-08-20 06:38:46 -04:00
|
|
|
|
2018-01-08 18:23:49 -05:00
|
|
|
render plain: ExtraLocalesController.bundle_js(bundle), content_type: "application/javascript"
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.bundle_js_hash(bundle)
|
2019-11-04 20:15:44 -05:00
|
|
|
if bundle == OVERRIDES_BUNDLE
|
|
|
|
site = RailsMultisite::ConnectionManagement.current_db
|
2018-01-08 18:23:49 -05:00
|
|
|
|
2019-11-04 20:15:44 -05:00
|
|
|
@by_site ||= {}
|
|
|
|
@by_site[site] ||= {}
|
|
|
|
@by_site[site][I18n.locale] ||= begin
|
|
|
|
js = bundle_js(bundle)
|
|
|
|
js.present? ? Digest::MD5.hexdigest(js) : nil
|
|
|
|
end
|
2018-01-08 18:23:49 -05:00
|
|
|
else
|
2019-11-04 20:15:44 -05:00
|
|
|
@bundle_js_hash ||= {}
|
|
|
|
@bundle_js_hash["#{bundle}_#{I18n.locale}"] ||= Digest::MD5.hexdigest(bundle_js(bundle))
|
2018-01-08 18:23:49 -05:00
|
|
|
end
|
|
|
|
end
|
2016-08-25 16:33:29 -04:00
|
|
|
|
2019-11-04 20:15:44 -05:00
|
|
|
def self.url(bundle)
|
2020-10-09 07:51:24 -04:00
|
|
|
"#{Discourse.base_path}/extra-locales/#{bundle}?v=#{bundle_js_hash(bundle)}"
|
2019-11-04 20:15:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.client_overrides_exist?
|
|
|
|
bundle_js_hash(OVERRIDES_BUNDLE).present?
|
|
|
|
end
|
|
|
|
|
2018-01-08 18:23:49 -05:00
|
|
|
def self.bundle_js(bundle)
|
2016-09-28 16:26:36 -04:00
|
|
|
locale_str = I18n.locale.to_s
|
2017-02-24 05:31:21 -05:00
|
|
|
bundle_str = "#{bundle}_js"
|
|
|
|
|
2019-11-04 20:15:44 -05:00
|
|
|
if bundle == OVERRIDES_BUNDLE
|
|
|
|
JsLocaleHelper.output_client_overrides(locale_str)
|
|
|
|
else
|
|
|
|
JsLocaleHelper.output_extra_locales(bundle_str, locale_str)
|
2019-05-23 15:23:31 -04:00
|
|
|
end
|
2019-11-04 20:15:44 -05:00
|
|
|
end
|
2017-02-24 05:31:21 -05:00
|
|
|
|
2019-11-04 20:15:44 -05:00
|
|
|
def self.clear_cache!
|
|
|
|
site = RailsMultisite::ConnectionManagement.current_db
|
|
|
|
@by_site&.delete(site)
|
|
|
|
end
|
2016-08-25 16:33:29 -04:00
|
|
|
|
2019-11-04 20:15:44 -05:00
|
|
|
private
|
2016-09-30 02:29:30 -04:00
|
|
|
|
2019-11-04 20:15:44 -05:00
|
|
|
def valid_bundle?(bundle)
|
2023-01-20 13:52:49 -05:00
|
|
|
bundle == OVERRIDES_BUNDLE || (bundle =~ /\A(admin|wizard)\z/ && current_user&.staff?)
|
2016-08-25 16:33:29 -04:00
|
|
|
end
|
|
|
|
end
|