diff options
| author | Louise Crow <louise.crow@gmail.com> | 2013-07-23 18:23:35 +0100 | 
|---|---|---|
| committer | Louise Crow <louise.crow@gmail.com> | 2013-07-23 18:23:35 +0100 | 
| commit | 8c8ff1c840451c9ed598c1b8c68ed72a1a938a38 (patch) | |
| tree | 5e6d753df4c436f4b87d46b3f1abf322975dd788 | |
| parent | 86c185f0d0195b300fa26b99a409d0596d590233 (diff) | |
Patch Globalize to compensate for the way gettext_i18n_rails patches 118n.locale in the handling of locales with underscores. Fixes #999.
| -rw-r--r-- | lib/i18n_fixes.rb | 23 | ||||
| -rw-r--r-- | spec/controllers/public_body_controller_spec.rb | 18 | ||||
| -rw-r--r-- | spec/fixtures/public_body_translations.yml | 14 | 
3 files changed, 49 insertions, 6 deletions
diff --git a/lib/i18n_fixes.rb b/lib/i18n_fixes.rb index 82d1b2c3a..9c1206215 100644 --- a/lib/i18n_fixes.rb +++ b/lib/i18n_fixes.rb @@ -17,7 +17,7 @@ end  def n_(*keys)    # The last parameter should be the values to do the interpolation with    if keys.count > 3 -    options = keys.pop  +    options = keys.pop    else      options = {}    end @@ -33,7 +33,7 @@ def gettext_interpolate(string, values)    safe = string.html_safe?    string = string.to_str.gsub(MATCH) do      pattern, key = $1, $1.to_sym -     +      if !values.include?(key)        raise I18n::MissingInterpolationArgument.new(pattern, string)      else @@ -50,7 +50,7 @@ end  module I18n -  # used by Globalize plugin.   +  # used by Globalize plugin.    # XXX much of this stuff should (might?) be in newer versions of Rails    @@fallbacks = nil    class << self @@ -120,7 +120,7 @@ module I18n          @defaults = defaults.map { |default| compute(default, false) }.flatten        end        attr_reader :defaults -       +        def [](locale)          raise InvalidLocale.new(locale) if locale.nil?          locale = locale.to_sym @@ -138,7 +138,7 @@ module I18n        end        protected -     +        def compute(tags, include_defaults = true)          result = Array(tags).collect do |tag|            tags = I18n::Locale::Tag::Simple.tag(tag).self_and_parents.map! { |t| t.to_sym } @@ -161,7 +161,18 @@ module GettextI18nRails    class Backend        def available_locales            FastGettext.available_locales.map{|l| l.to_sym} || [] -      end  +      end    end  end +# Monkeypatch Globalize to compensate for the way gettext_i18n_rails patches +# I18n.locale= so that it changes underscores in locale names (as used in the gettext world) +# to the dashes that I18n prefers +module Globalize +    class << self +       def locale +           read_locale || I18n.locale.to_s.gsub('-', '_').to_sym +       end +    end +end + diff --git a/spec/controllers/public_body_controller_spec.rb b/spec/controllers/public_body_controller_spec.rb index 68e02e000..4e1841164 100644 --- a/spec/controllers/public_body_controller_spec.rb +++ b/spec/controllers/public_body_controller_spec.rb @@ -48,6 +48,12 @@ describe PublicBodyController, "when showing a body" do          response.should contain("Baguette")      end +    it 'should show public body names in the selected locale language if present for a locale with underscores' do +        AlaveteliLocalization.set_locales('he_IL en', 'en') +        get :show, {:url_name => 'dfh', :view => 'all', :locale => 'he_IL'} +        response.should contain('Hebrew Humpadinking') +    end +      it "should redirect use to the relevant locale even when url_name is for a different locale" do          get :show, {:url_name => "edfh", :view => 'all'}          response.should redirect_to "http://test.host/body/dfh" @@ -85,6 +91,18 @@ describe PublicBodyController, "when listing bodies" do          assigns[:public_bodies].include?(@english_only).should == true      end +    it 'should show public body names in the selected locale language if present' do +        get :list, {:locale => 'es'} +        response.should contain('El Department for Humpadinking') +    end + +    it 'should show public body names in the selected locale language if present for a locale with underscores' do +        AlaveteliLocalization.set_locales('he_IL en', 'en') +        get :list, {:locale => 'he_IL'} +        response.should contain('Hebrew Humpadinking') +    end + +      it "should list bodies in alphabetical order" do          # Note that they are alphabetised by localised name          get :list diff --git a/spec/fixtures/public_body_translations.yml b/spec/fixtures/public_body_translations.yml index 61e07fb5b..de1bf2f18 100644 --- a/spec/fixtures/public_body_translations.yml +++ b/spec/fixtures/public_body_translations.yml @@ -101,3 +101,17 @@ other_public_body_translation:    notes: More notes    publication_scheme: ""    disclosure_log: "" + +humpadink_he_IL_public_body_translation: +  name: "Hebrew Humpadinking" +  first_letter: D +  request_email: humpadink-requests@localhost +  id: 9 +  public_body_id: 3 +  short_name: DfH +  url_name: dfh +  locale: he_IL +  notes: An albatross told me!!! +  publication_scheme: "" +  disclosure_log: "" +  | 
