discourse/app/controllers/extra_locales_controller.rb

65 lines
1.6 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2016-08-25 16:33:29 -04:00
class ExtraLocalesController < ApplicationController
layout :false
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]
raise Discourse::InvalidAccess.new if bundle !~ /^(admin|wizard)$/ || !current_user&.staff?
if params[:v]&.size == 32
hash = ExtraLocalesController.bundle_js_hash(bundle)
immutable_for(24.hours) if hash == params[:v]
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}_#{I18n.locale}"] ||= Digest::MD5.hexdigest(bundle_js(bundle))
end
def self.url(bundle)
if Rails.env == "production"
"#{Discourse.base_uri}/extra-locales/#{bundle}?v=#{bundle_js_hash(bundle)}"
else
"#{Discourse.base_uri}/extra-locales/#{bundle}"
end
end
2016-08-25 16:33:29 -04:00
def self.bundle_js(bundle)
locale_str = I18n.locale.to_s
bundle_str = "#{bundle}_js"
translations = JsLocaleHelper.translations_for(locale_str)
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
js = ""
2016-08-25 16:33:29 -04:00
if translations.present?
js = <<~JS.squish
(function() {
if (window.I18n) {
window.I18n.extras = #{translations.to_json};
}
})();
JS
end
js
2016-08-25 16:33:29 -04:00
end
end