discourse/app/controllers/extra_locales_controller.rb

65 lines
1.6 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2016-08-26 04:33:29 +08:00
class ExtraLocalesController < ApplicationController
layout :false
skip_before_action :check_xhr,
:preload_json,
:redirect_to_login_if_required,
:verify_authenticity_token
2016-08-26 04:33:29 +08:00
def show
bundle = params[:bundle]
raise Discourse::InvalidAccess.new unless bundle =~ /^(admin|wizard)$/
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}_#{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-26 04:33:29 +08: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-26 04:33:29 +08:00
if translations.present?
js = <<~JS.squish
(function() {
if (window.I18n) {
window.I18n.extras = #{translations.to_json};
}
})();
JS
end
js
2016-08-26 04:33:29 +08:00
end
end