2018-01-08 18:23:49 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-25 16:33:29 -04:00
|
|
|
class ExtraLocalesController < ApplicationController
|
|
|
|
layout :false
|
2019-04-30 02:58:18 -04:00
|
|
|
|
|
|
|
skip_before_action :check_xhr,
|
|
|
|
:preload_json,
|
|
|
|
:redirect_to_login_if_required,
|
|
|
|
:verify_authenticity_token
|
2016-08-25 16:33:29 -04:00
|
|
|
|
|
|
|
def show
|
|
|
|
bundle = params[:bundle]
|
2019-08-20 06:38:46 -04:00
|
|
|
|
|
|
|
raise Discourse::InvalidAccess.new if bundle !~ /^(admin|wizard)$/ || !current_user&.staff?
|
|
|
|
|
|
|
|
if params[:v]&.size == 32
|
2018-01-08 18:23:49 -05:00
|
|
|
hash = ExtraLocalesController.bundle_js_hash(bundle)
|
2019-08-20 06:38:46 -04:00
|
|
|
immutable_for(24.hours) if hash == params[:v]
|
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)
|
|
|
|
@bundle_js_hash ||= {}
|
2019-05-23 15:41:48 -04:00
|
|
|
@bundle_js_hash["#{bundle}_#{I18n.locale}"] ||= Digest::MD5.hexdigest(bundle_js(bundle))
|
2018-01-08 18:23:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.url(bundle)
|
|
|
|
if Rails.env == "production"
|
2018-01-09 16:19:51 -05:00
|
|
|
"#{Discourse.base_uri}/extra-locales/#{bundle}?v=#{bundle_js_hash(bundle)}"
|
2018-01-08 18:23:49 -05:00
|
|
|
else
|
|
|
|
"#{Discourse.base_uri}/extra-locales/#{bundle}"
|
|
|
|
end
|
|
|
|
end
|
2016-08-25 16:33:29 -04:00
|
|
|
|
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"
|
|
|
|
|
2016-09-28 16:26:36 -04:00
|
|
|
translations = JsLocaleHelper.translations_for(locale_str)
|
2017-02-24 05:31:21 -05:00
|
|
|
|
2019-05-23 15:23:31 -04:00
|
|
|
translations.keys.each do |l|
|
|
|
|
translations[l].keys.each do |k|
|
|
|
|
bundle_translations = translations[l].delete(k)
|
|
|
|
translations[l].deep_merge!(bundle_translations) if k == bundle_str
|
|
|
|
end
|
|
|
|
end
|
2017-02-24 05:31:21 -05:00
|
|
|
|
2016-09-30 02:29:30 -04:00
|
|
|
js = ""
|
2016-08-25 16:33:29 -04:00
|
|
|
|
2019-05-23 15:23:31 -04:00
|
|
|
if translations.present?
|
2017-02-24 05:31:21 -05:00
|
|
|
js = <<~JS.squish
|
|
|
|
(function() {
|
|
|
|
if (window.I18n) {
|
2019-05-23 15:23:31 -04:00
|
|
|
window.I18n.extras = #{translations.to_json};
|
2017-02-24 05:31:21 -05:00
|
|
|
}
|
|
|
|
})();
|
2016-09-30 02:29:30 -04:00
|
|
|
JS
|
|
|
|
end
|
|
|
|
|
2018-01-08 18:23:49 -05:00
|
|
|
js
|
2016-08-25 16:33:29 -04:00
|
|
|
end
|
|
|
|
end
|