mirror of
https://github.com/discourse/discourse.git
synced 2025-05-25 00:32:52 +08:00
Support for replacing Site Message content with keys specific to a multisite.
This commit is contained in:
31
lib/multisite_i18n.rb
Normal file
31
lib/multisite_i18n.rb
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
# Allow us to override i18n keys based on the current site you're viewing.
|
||||||
|
module MultisiteI18n
|
||||||
|
|
||||||
|
class << self
|
||||||
|
|
||||||
|
# It would be nice if there was an easier way to detect if a key is missing.
|
||||||
|
def translation_or_nil(key, opts)
|
||||||
|
missing_text = "missing multisite translation"
|
||||||
|
result = I18n.t(key, opts.merge(default: missing_text))
|
||||||
|
return nil if result == missing_text
|
||||||
|
result
|
||||||
|
end
|
||||||
|
|
||||||
|
def site_translate(current_site, key, opts=nil)
|
||||||
|
opts ||= {}
|
||||||
|
translation = MultisiteI18n.translation_or_nil("#{current_site || ""}.#{key}", opts)
|
||||||
|
if translation.blank?
|
||||||
|
return I18n.t(key, opts)
|
||||||
|
else
|
||||||
|
return translation
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def t(*args)
|
||||||
|
MultisiteI18n.site_translate(RailsMultisite::ConnectionManagement.current_db, *args)
|
||||||
|
end
|
||||||
|
|
||||||
|
alias :translate :t
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
@ -1,5 +1,6 @@
|
|||||||
# Handle sending a message to a user from the system.
|
# Handle sending a message to a user from the system.
|
||||||
require_dependency 'post_creator'
|
require_dependency 'post_creator'
|
||||||
|
require_dependency 'multisite_i18n'
|
||||||
|
|
||||||
class SystemMessage
|
class SystemMessage
|
||||||
|
|
||||||
@ -14,20 +15,20 @@ class SystemMessage
|
|||||||
def create(type, params = {})
|
def create(type, params = {})
|
||||||
|
|
||||||
defaults = {site_name: SiteSetting.title,
|
defaults = {site_name: SiteSetting.title,
|
||||||
username: @recipient.username,
|
username: @recipient.username,
|
||||||
user_preferences_url: "#{Discourse.base_url}/users/#{@recipient.username_lower}/preferences",
|
user_preferences_url: "#{Discourse.base_url}/users/#{@recipient.username_lower}/preferences",
|
||||||
new_user_tips: I18n.t("system_messages.usage_tips.text_body_template"),
|
new_user_tips: MultisiteI18n.t("system_messages.usage_tips.text_body_template"),
|
||||||
site_password: "",
|
site_password: "",
|
||||||
base_url: Discourse.base_url}
|
base_url: Discourse.base_url}
|
||||||
|
|
||||||
params = defaults.merge(params)
|
params = defaults.merge(params)
|
||||||
|
|
||||||
if SiteSetting.restrict_access?
|
if SiteSetting.restrict_access?
|
||||||
params[:site_password] = I18n.t('system_messages.site_password', access_password: SiteSetting.access_password)
|
params[:site_password] = MultisiteI18n.t('system_messages.site_password', access_password: SiteSetting.access_password)
|
||||||
end
|
end
|
||||||
|
|
||||||
title = I18n.t("system_messages.#{type}.subject_template", params)
|
title = MultisiteI18n.t("system_messages.#{type}.subject_template", params)
|
||||||
raw_body = I18n.t("system_messages.#{type}.text_body_template", params)
|
raw_body = MultisiteI18n.t("system_messages.#{type}.text_body_template", params)
|
||||||
|
|
||||||
PostCreator.create(SystemMessage.system_user,
|
PostCreator.create(SystemMessage.system_user,
|
||||||
raw: raw_body,
|
raw: raw_body,
|
||||||
|
37
spec/components/multisite_i18n_spec.rb
Normal file
37
spec/components/multisite_i18n_spec.rb
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
require 'spec_helper'
|
||||||
|
require_dependency 'multisite_i18n'
|
||||||
|
|
||||||
|
describe MultisiteI18n do
|
||||||
|
|
||||||
|
before do
|
||||||
|
I18n.stubs(:t).with('test', {}).returns('default i18n')
|
||||||
|
MultisiteI18n.stubs(:translation_or_nil).with("default.test", {}).returns(nil)
|
||||||
|
MultisiteI18n.stubs(:translation_or_nil).with("other_site.test", {}).returns("overwritten i18n")
|
||||||
|
end
|
||||||
|
|
||||||
|
context "no value for a multisite key" do
|
||||||
|
it "it returns the default i18n key" do
|
||||||
|
MultisiteI18n.site_translate('default', 'test').should == "default i18n"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "with a value for the multisite key" do
|
||||||
|
it "returns the overwritten value" do
|
||||||
|
MultisiteI18n.site_translate('other_site', 'test').should == "overwritten i18n"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "when we call t, it uses the current site" do
|
||||||
|
|
||||||
|
it "returns the original" do
|
||||||
|
MultisiteI18n.t('test').should == 'default i18n'
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns the overwritten" do
|
||||||
|
RailsMultisite::ConnectionManagement.stubs(:current_db).returns('other_site')
|
||||||
|
MultisiteI18n.t('test').should == "overwritten i18n"
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
Reference in New Issue
Block a user