mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Introduce I18nConfig as single point of truth
It will be used in application.rb and views. See https://github.com/openfoodfoundation/openfoodnetwork/issues/2113
This commit is contained in:
22
lib/open_food_network/i18n_config.rb
Normal file
22
lib/open_food_network/i18n_config.rb
Normal file
@@ -0,0 +1,22 @@
|
||||
module OpenFoodNetwork
|
||||
# Provides access to the language settings.
|
||||
# Currently, language settings are read from the environment.
|
||||
# See: config/application.yml
|
||||
class I18nConfig
|
||||
def self.selectable_locales
|
||||
ENV["AVAILABLE_LOCALES"].andand.split(/[\s,]/).andand.map(&:strip) || []
|
||||
end
|
||||
|
||||
def self.available_locales
|
||||
(selectable_locales + [default_locale, 'en']).uniq
|
||||
end
|
||||
|
||||
def self.default_locale
|
||||
ENV["LOCALE"] || ENV["I18N_LOCALE"] || source_locale
|
||||
end
|
||||
|
||||
def self.source_locale
|
||||
"en"
|
||||
end
|
||||
end
|
||||
end
|
||||
94
spec/lib/open_food_network/i18n_config_spec.rb
Normal file
94
spec/lib/open_food_network/i18n_config_spec.rb
Normal file
@@ -0,0 +1,94 @@
|
||||
require 'spec_helper'
|
||||
require 'open_food_network/i18n_config'
|
||||
|
||||
module OpenFoodNetwork
|
||||
describe I18nConfig do
|
||||
context "in default test configuration" do
|
||||
before do
|
||||
allow(ENV).to receive(:[]).with("LOCALE").and_return("en")
|
||||
allow(ENV).to receive(:[]).with("AVAILABLE_LOCALES").and_return("en,es")
|
||||
end
|
||||
|
||||
it "provides the source locale" do
|
||||
expect(I18nConfig.source_locale).to eq "en"
|
||||
end
|
||||
|
||||
it "provides the default locale" do
|
||||
expect(I18nConfig.default_locale).to eq "en"
|
||||
end
|
||||
|
||||
it "provides the default selectable locales" do
|
||||
expect(I18nConfig.selectable_locales).to eq ["en", "es"]
|
||||
end
|
||||
|
||||
it "provides the default available locales" do
|
||||
expect(I18nConfig.available_locales).to eq ["en", "es"]
|
||||
end
|
||||
end
|
||||
|
||||
context "without configuration" do
|
||||
before do
|
||||
allow(ENV).to receive(:[]).with("LOCALE").and_return(nil)
|
||||
allow(ENV).to receive(:[]).with("I18N_LOCALE").and_return(nil)
|
||||
allow(ENV).to receive(:[]).with("AVAILABLE_LOCALES").and_return(nil)
|
||||
end
|
||||
|
||||
it "provides the source locale" do
|
||||
expect(I18nConfig.source_locale).to eq "en"
|
||||
end
|
||||
|
||||
it "provides the default locale" do
|
||||
expect(I18nConfig.default_locale).to eq "en"
|
||||
end
|
||||
|
||||
it "provides the default selectable locales" do
|
||||
expect(I18nConfig.selectable_locales).to eq []
|
||||
end
|
||||
|
||||
it "provides the default available locales" do
|
||||
expect(I18nConfig.available_locales).to eq ["en"]
|
||||
end
|
||||
end
|
||||
|
||||
context "with UK configuration" do
|
||||
before do
|
||||
allow(ENV).to receive(:[]).with("LOCALE").and_return("en_GB")
|
||||
allow(ENV).to receive(:[]).with("I18N_LOCALE").and_return(nil)
|
||||
allow(ENV).to receive(:[]).with("AVAILABLE_LOCALES").and_return("en_GB")
|
||||
end
|
||||
|
||||
it "provides the source locale" do
|
||||
expect(I18nConfig.source_locale).to eq "en"
|
||||
end
|
||||
|
||||
it "provides the default locale" do
|
||||
expect(I18nConfig.default_locale).to eq "en_GB"
|
||||
end
|
||||
|
||||
it "provides the default selectable locales" do
|
||||
expect(I18nConfig.selectable_locales).to eq ["en_GB"]
|
||||
end
|
||||
|
||||
it "provides the default available locales" do
|
||||
expect(I18nConfig.available_locales).to eq ["en_GB", "en"]
|
||||
end
|
||||
end
|
||||
|
||||
context "with human syntax" do
|
||||
before do
|
||||
allow(ENV).to receive(:[]).with("LOCALE").and_return("es")
|
||||
allow(ENV).to receive(:[]).with("AVAILABLE_LOCALES").and_return("es, fr")
|
||||
end
|
||||
|
||||
xit "provides the default selectable locales" do
|
||||
# current: expect(I18nConfig.selectable_locales).to eq ["es", "", "fr"]
|
||||
expect(I18nConfig.selectable_locales).to eq ["es", "fr"]
|
||||
end
|
||||
|
||||
xit "provides the default available locales" do
|
||||
# current: expect(I18nConfig.available_locales).to eq ["es", "", "fr", "en"]
|
||||
expect(I18nConfig.available_locales).to eq ["es", "fr", "en"]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user