Allow different default locales per tenant

Note that, for everything to work consistently, we need to make sure
that the default locale is one of the available locales.

Also note that we aren't overwriting the `#save ` method set by
globalize. I didn't feel too comfortable changing a monkey-patch which
ideally shouldn't be there in the first place, I haven't found a case
where `Globalize.locale` is `nil` (since it defaults to `I18n.locale`,
which should never be `nil`), so using `I18n.default_locale` probably
doesn't affect us.
This commit is contained in:
Javi Martín
2024-03-04 20:03:25 +01:00
parent 722e50a669
commit 6de4737b70
22 changed files with 156 additions and 26 deletions

View File

@@ -260,7 +260,7 @@ describe Setting do
end
describe ".available_locales" do
before { allow(I18n).to receive(:available_locales).and_return(%i[de en es pt-BR]) }
before { allow(I18n).to receive_messages(default_locale: :de, available_locales: %i[de en es pt-BR]) }
it "uses I18n available locales by default" do
Setting["locales.enabled"] = ""
@@ -280,6 +280,12 @@ describe Setting do
expect(Setting.enabled_locales).to eq %i[de en pt-BR]
end
it "adds the default locale to the list of available locales" do
Setting["locales.enabled"] = "en es"
expect(Setting.enabled_locales).to eq %i[de en es]
end
it "ignores extra whitespace between locales" do
Setting["locales.enabled"] = " de en pt-BR "
@@ -293,9 +299,9 @@ describe Setting do
end
it "ignores words that don't make sense in this context" do
Setting["locales.enabled"] = "yes es 1234 en SuperCool"
Setting["locales.enabled"] = "yes de 1234 en SuperCool"
expect(Setting.enabled_locales).to eq %i[es en]
expect(Setting.enabled_locales).to eq %i[de en]
end
it "uses I18n available locales when no locale is available" do
@@ -304,4 +310,44 @@ describe Setting do
expect(Setting.enabled_locales).to eq %i[de en es pt-BR]
end
end
describe ".default_locale" do
before { allow(I18n).to receive_messages(default_locale: :en, available_locales: %i[de en es pt-BR]) }
it "uses I18n default locale by default" do
Setting["locales.default"] = ""
expect(Setting.default_locale).to eq :en
end
it "allows defining the default locale" do
Setting["locales.default"] = "de"
expect(Setting.default_locale).to eq :de
end
it "handles locales which include a dash" do
Setting["locales.default"] = "pt-BR"
expect(Setting.default_locale).to eq :"pt-BR"
end
it "ignores extra whitespace in the locale name" do
Setting["locales.default"] = " es "
expect(Setting.default_locale).to eq :es
end
it "ignores locales which aren't available" do
Setting["locales.default"] = "fr"
expect(Setting.default_locale).to eq :en
end
it "ignores an array of several locales" do
Setting["locales.default"] = "de es"
expect(Setting.default_locale).to eq :en
end
end
end