diff options
author | Louise Crow <louise.crow@gmail.com> | 2013-07-18 17:10:31 +0100 |
---|---|---|
committer | Louise Crow <louise.crow@gmail.com> | 2013-07-18 17:43:26 +0100 |
commit | bc0a6d54c4e5f3673a761ca434c49cbfebb89b09 (patch) | |
tree | ac7165739b51316122040be55d8ed5561f440ddf | |
parent | 9fe48c5b9db1c0ba6c1a59f0d092c88e5cae1aa0 (diff) |
Extract code for setting locales in FastGettext and I18n
-rw-r--r-- | config/initializers/alaveteli.rb | 12 | ||||
-rw-r--r-- | lib/alaveteli_localization.rb | 13 | ||||
-rw-r--r-- | spec/integration/localisation_spec.rb | 9 | ||||
-rw-r--r-- | spec/spec_helper.rb | 7 |
4 files changed, 22 insertions, 19 deletions
diff --git a/config/initializers/alaveteli.rb b/config/initializers/alaveteli.rb index d78bc3925..deee3f6da 100644 --- a/config/initializers/alaveteli.rb +++ b/config/initializers/alaveteli.rb @@ -34,14 +34,6 @@ if AlaveteliConfiguration::force_ssl ActionMailer::Base.default_url_options[:protocol] = "https" end -# fallback locale and available locales -available_locales = AlaveteliConfiguration::available_locales.split(/ /) -default_locale = AlaveteliConfiguration::default_locale - -FastGettext.default_available_locales = available_locales -I18n.locale = default_locale -I18n.available_locales = available_locales.map {|locale_name| locale_name.to_sym} -I18n.default_locale = default_locale # Load monkey patches and other things from lib/ require 'ruby19.rb' @@ -57,6 +49,10 @@ require 'public_body_categories' require 'ability' require 'normalize_string' require 'alaveteli_file_types' +require 'alaveteli_localization' + +AlaveteliLocalization.set_locales(AlaveteliConfiguration::available_locales, + AlaveteliConfiguration::default_locale) # Allow tests to be run under a non-superuser database account if required if Rails.env == 'test' and ActiveRecord::Base.configurations['test']['constraint_disabling'] == false diff --git a/lib/alaveteli_localization.rb b/lib/alaveteli_localization.rb new file mode 100644 index 000000000..25de002c4 --- /dev/null +++ b/lib/alaveteli_localization.rb @@ -0,0 +1,13 @@ +class AlaveteliLocalization + class << self + def set_locales(available_locales, default_locale) + # fallback locale and available locales + available_locales = available_locales.split(/ /) + FastGettext.default_available_locales = available_locales + I18n.locale = default_locale + I18n.available_locales = available_locales.map { |locale_name| locale_name.to_sym } + I18n.default_locale = default_locale + end + + end +end diff --git a/spec/integration/localisation_spec.rb b/spec/integration/localisation_spec.rb index 140f9f785..64d39f0af 100644 --- a/spec/integration/localisation_spec.rb +++ b/spec/integration/localisation_spec.rb @@ -7,21 +7,19 @@ describe "when generating urls" do end it "should generate URLs that include the locale when using one that includes an underscore" do - AlaveteliConfiguration.stub!(:available_locales).and_return('en_GB') get('/en_GB') response.body.should match /href="\/en_GB\// end it "should fall back to the language if the territory is unknown" do - AlaveteliConfiguration.stub!(:available_locales).and_return('es en') - AlaveteliConfiguration.stub!(:default_locale).and_return('es') + AlaveteliLocalization.set_locales(available_locales='es en', default_locale='en') get('/', {}, {'HTTP_ACCEPT_LANGUAGE' => 'en_US'}) response.body.should match /href="\/en\// response.body.should_not match /href="\/en_US\// end it "should generate URLs without a locale prepended when there's only one locale set" do - AlaveteliConfiguration.stub!(:available_locales).and_return('en') + AlaveteliLocalization.set_locales(available_locales='en', default_locale='en') get('/') response.should_not contain @home_link_regex end @@ -39,8 +37,7 @@ describe "when generating urls" do describe 'when there is more than one locale' do before do - AlaveteliConfiguration.stub!(:available_locales).and_return('en es') - AlaveteliConfiguration.stub!(:default_locale).and_return('en') + AlaveteliLocalization.set_locales(available_locales='es en', default_locale='en') end it "should generate URLs with a locale prepended when there's more than one locale set" do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index c7fb0af75..7e3df4f39 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -88,12 +88,9 @@ Spork.prefork do # ApplicationController#set_gettext_locale which sets the locale and so you may be setting # the locale in your tests and not even realising it. So, let's make things easier for # ourselves and just always restore the locale for all tests. - config.before(:each) do - @save_i18n_locale = I18n.locale - end - config.after(:each) do - I18n.locale = @save_i18n_locale + AlaveteliLocalization.set_locales(AlaveteliConfiguration::available_locales, + AlaveteliConfiguration::default_locale) end # Turn routing-filter off in functional and unit tests as per |