mirror of
https://github.com/discourse/discourse.git
synced 2025-05-22 06:21:12 +08:00
FEATURE: Allow plugins to register a new locale
This commit is contained in:
@ -10,8 +10,8 @@ describe Plugin::Instance do
|
||||
context "find_all" do
|
||||
it "can find plugins correctly" do
|
||||
plugins = Plugin::Instance.find_all("#{Rails.root}/spec/fixtures/plugins")
|
||||
expect(plugins.count).to eq(1)
|
||||
plugin = plugins[0]
|
||||
expect(plugins.count).to eq(2)
|
||||
plugin = plugins[1]
|
||||
|
||||
expect(plugin.name).to eq("plugin-name")
|
||||
expect(plugin.path).to eq("#{Rails.root}/spec/fixtures/plugins/my_plugin/plugin.rb")
|
||||
@ -268,4 +268,108 @@ describe Plugin::Instance do
|
||||
expect(called).to eq(1)
|
||||
end
|
||||
end
|
||||
|
||||
context "locales" do
|
||||
let(:plugin_path) { "#{Rails.root}/spec/fixtures/plugins/custom_locales" }
|
||||
let!(:plugin) { Plugin::Instance.new(nil, "#{plugin_path}/plugin.rb") }
|
||||
let(:plural) do
|
||||
{
|
||||
keys: [:one, :few, :other],
|
||||
rule: lambda do |n|
|
||||
return :one if n == 1
|
||||
return :few if n < 10
|
||||
:other
|
||||
end
|
||||
}
|
||||
end
|
||||
|
||||
def register_locale(locale, opts)
|
||||
plugin.register_locale(locale, opts)
|
||||
plugin.activate!
|
||||
|
||||
DiscoursePluginRegistry.locales[locale]
|
||||
end
|
||||
|
||||
it "enables the registered locales only on activate" do
|
||||
plugin.register_locale("foo", name: "Foo", nativeName: "Foo Bar", plural: plural)
|
||||
plugin.register_locale("es_MX", name: "Spanish (Mexico)", nativeName: "Español (México)", fallbackLocale: "es")
|
||||
expect(DiscoursePluginRegistry.locales.count).to eq(0)
|
||||
|
||||
plugin.activate!
|
||||
expect(DiscoursePluginRegistry.locales.count).to eq(2)
|
||||
end
|
||||
|
||||
it "allows finding the locale by string and symbol" do
|
||||
register_locale("foo", name: "Foo", nativeName: "Foo Bar", plural: plural)
|
||||
|
||||
expect(DiscoursePluginRegistry.locales).to have_key(:foo)
|
||||
expect(DiscoursePluginRegistry.locales).to have_key('foo')
|
||||
end
|
||||
|
||||
it "correctly registers a new locale" do
|
||||
locale = register_locale("foo", name: "Foo", nativeName: "Foo Bar", plural: plural)
|
||||
|
||||
expect(DiscoursePluginRegistry.locales.count).to eq(1)
|
||||
expect(DiscoursePluginRegistry.locales).to have_key(:foo)
|
||||
|
||||
expect(locale[:fallbackLocale]).to be_nil
|
||||
expect(locale[:message_format]).to eq(["foo", "#{plugin_path}/lib/javascripts/locale/message_format/foo.js"])
|
||||
expect(locale[:moment_js]).to eq(["foo", "#{plugin_path}/lib/javascripts/locale/moment_js/foo.js"])
|
||||
expect(locale[:plural]).to eq(plural.with_indifferent_access)
|
||||
|
||||
expect(Rails.configuration.assets.precompile).to include("locales/foo.js")
|
||||
end
|
||||
|
||||
it "correctly registers a new locale using a fallback locale" do
|
||||
locale = register_locale("es_MX", name: "Spanish (Mexico)", nativeName: "Español (México)", fallbackLocale: "es")
|
||||
|
||||
expect(DiscoursePluginRegistry.locales.count).to eq(1)
|
||||
expect(DiscoursePluginRegistry.locales).to have_key(:es_MX)
|
||||
|
||||
expect(locale[:fallbackLocale]).to eq("es")
|
||||
expect(locale[:message_format]).to eq(["es", "#{Rails.root}/lib/javascripts/locale/es.js"])
|
||||
expect(locale[:moment_js]).to eq(["es", "#{Rails.root}/lib/javascripts/moment_locale/es.js"])
|
||||
expect(locale[:plural]).to be_nil
|
||||
|
||||
expect(Rails.configuration.assets.precompile).to include("locales/es_MX.js")
|
||||
end
|
||||
|
||||
it "correctly registers a new locale when some files exist in core" do
|
||||
locale = register_locale("tlh", name: "Klingon", nativeName: "tlhIngan Hol", plural: plural)
|
||||
|
||||
expect(DiscoursePluginRegistry.locales.count).to eq(1)
|
||||
expect(DiscoursePluginRegistry.locales).to have_key(:tlh)
|
||||
|
||||
expect(locale[:fallbackLocale]).to be_nil
|
||||
expect(locale[:message_format]).to eq(["tlh", "#{plugin_path}/lib/javascripts/locale/message_format/tlh.js"])
|
||||
expect(locale[:moment_js]).to eq(["tlh", "#{Rails.root}/lib/javascripts/moment_locale/tlh.js"])
|
||||
expect(locale[:plural]).to eq(plural.with_indifferent_access)
|
||||
|
||||
expect(Rails.configuration.assets.precompile).to include("locales/tlh.js")
|
||||
end
|
||||
|
||||
it "does not register a new locale when the fallback locale does not exist" do
|
||||
register_locale("bar", name: "Bar", nativeName: "Bar", fallbackLocale: "foo")
|
||||
expect(DiscoursePluginRegistry.locales.count).to eq(0)
|
||||
end
|
||||
|
||||
[
|
||||
"config/locales/client.foo.yml",
|
||||
"config/locales/server.foo.yml",
|
||||
"lib/javascripts/locale/message_format/foo.js",
|
||||
"lib/javascripts/locale/moment_js/foo.js",
|
||||
"assets/locales/foo.js.erb"
|
||||
].each do |path|
|
||||
it "does not register a new locale when #{path} is missing" do
|
||||
path = "#{plugin_path}/#{path}"
|
||||
File.stubs('exist?').returns(false)
|
||||
File.stubs('exist?').with(regexp_matches(/#{Regexp.quote(plugin_path)}.*/)).returns(true)
|
||||
File.stubs('exist?').with(path).returns(false)
|
||||
|
||||
register_locale("foo", name: "Foo", nativeName: "Foo Bar", plural: plural)
|
||||
expect(DiscoursePluginRegistry.locales.count).to eq(0)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
Reference in New Issue
Block a user