2018-01-08 17:23:49 -06:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-25 15:33:29 -05:00
|
|
|
class ExtraLocalesController < ApplicationController
|
|
|
|
|
|
|
|
layout :false
|
2019-04-30 01:58:18 -05:00
|
|
|
|
|
|
|
skip_before_action :check_xhr,
|
|
|
|
:preload_json,
|
|
|
|
:redirect_to_login_if_required,
|
|
|
|
:verify_authenticity_token
|
2016-08-25 15:33:29 -05:00
|
|
|
|
|
|
|
def show
|
|
|
|
bundle = params[:bundle]
|
2017-02-24 04:31:21 -06:00
|
|
|
raise Discourse::InvalidAccess.new unless bundle =~ /^(admin|wizard)$/
|
2018-01-08 17:23:49 -06:00
|
|
|
if params[:v] && params[:v].length == 32
|
|
|
|
hash = ExtraLocalesController.bundle_js_hash(bundle)
|
|
|
|
if hash == params[:v]
|
|
|
|
immutable_for 24.hours
|
|
|
|
end
|
|
|
|
end
|
|
|
|
render plain: ExtraLocalesController.bundle_js(bundle), content_type: "application/javascript"
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.bundle_js_hash(bundle)
|
|
|
|
@bundle_js_hash ||= {}
|
|
|
|
@bundle_js_hash[bundle] = Digest::MD5.hexdigest(bundle_js(bundle))
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.url(bundle)
|
|
|
|
if Rails.env == "production"
|
2018-01-09 15:19:51 -06:00
|
|
|
"#{Discourse.base_uri}/extra-locales/#{bundle}?v=#{bundle_js_hash(bundle)}"
|
2018-01-08 17:23:49 -06:00
|
|
|
else
|
|
|
|
"#{Discourse.base_uri}/extra-locales/#{bundle}"
|
|
|
|
end
|
|
|
|
end
|
2016-08-25 15:33:29 -05:00
|
|
|
|
2018-01-08 17:23:49 -06:00
|
|
|
def self.bundle_js(bundle)
|
2016-09-28 15:26:36 -05:00
|
|
|
locale_str = I18n.locale.to_s
|
2017-02-24 04:31:21 -06:00
|
|
|
bundle_str = "#{bundle}_js"
|
|
|
|
|
2016-09-28 15:26:36 -05:00
|
|
|
translations = JsLocaleHelper.translations_for(locale_str)
|
2017-02-24 04:31:21 -06:00
|
|
|
|
|
|
|
for_key = {}
|
|
|
|
translations.values.each { |v| for_key.deep_merge!(v[bundle_str]) if v.has_key?(bundle_str) }
|
|
|
|
|
2016-09-30 01:29:30 -05:00
|
|
|
js = ""
|
2016-08-25 15:33:29 -05:00
|
|
|
|
2016-09-30 01:29:30 -05:00
|
|
|
if for_key.present?
|
2017-02-24 04:31:21 -06:00
|
|
|
if plugin_for_key = JsLocaleHelper.plugin_translations(locale_str)[bundle_str]
|
2016-09-30 01:29:30 -05:00
|
|
|
for_key.deep_merge!(plugin_for_key)
|
2016-09-28 15:26:36 -05:00
|
|
|
end
|
2016-08-25 15:33:29 -05:00
|
|
|
|
2017-02-24 04:31:21 -06:00
|
|
|
js = <<~JS.squish
|
|
|
|
(function() {
|
|
|
|
if (window.I18n) {
|
|
|
|
window.I18n.extras = window.I18n.extras || [];
|
|
|
|
window.I18n.extras.push(#{for_key.to_json});
|
|
|
|
}
|
|
|
|
})();
|
2016-09-30 01:29:30 -05:00
|
|
|
JS
|
|
|
|
end
|
|
|
|
|
2018-01-08 17:23:49 -06:00
|
|
|
js
|
2016-08-25 15:33:29 -05:00
|
|
|
end
|
|
|
|
end
|