Skip to content

Commit

Permalink
extract translate helpers into WillPaginate::I18n module
Browse files Browse the repository at this point in the history
  • Loading branch information
mislav committed Aug 6, 2011
1 parent 1ee8ecd commit 9d200f9
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 16 deletions.
21 changes: 21 additions & 0 deletions lib/will_paginate/i18n.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module WillPaginate
module I18n
def self.locale_dir
File.expand_path('../locale', __FILE__)
end

def self.load_path
Dir["#{locale_dir}/*.{rb,yml}"]
end

def will_paginate_translate(keys, options = {})
if defined? ::I18n
defaults = Array(keys).dup
defaults << Proc.new if block_given?
::I18n.translate(defaults.shift, options.merge(:default => defaults, :scope => :will_paginate))
else
yield key, options
end
end
end
end
4 changes: 2 additions & 2 deletions lib/will_paginate/railtie.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'will_paginate'
require 'will_paginate/collection'
require 'will_paginate/i18n'

module WillPaginate
class Railtie < Rails::Railtie
Expand All @@ -24,8 +25,7 @@ def self.setup_actioncontroller
end

def self.add_locale_path(config)
locale_path = File.expand_path('../locale', __FILE__)
config.i18n.railties_load_path.unshift(*Dir["#{locale_path}/*.{rb,yml}"])
config.i18n.railties_load_path.unshift(*WillPaginate::I18n.load_path)
end
end
end
13 changes: 3 additions & 10 deletions lib/will_paginate/view_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# encoding: utf-8
require 'will_paginate/core_ext'
require 'will_paginate/i18n'

module WillPaginate
# = Will Paginate view helpers
Expand Down Expand Up @@ -32,6 +33,8 @@ class << self
:container => true
}

include WillPaginate::I18n

# Returns HTML representing page links for a WillPaginate::Collection-like object.
# In case there is no more than one page in total, nil is returned.
#
Expand Down Expand Up @@ -150,15 +153,5 @@ def page_entries_info(collection, options = {})
end
end
end

def will_paginate_translate(keys, options = {})
if defined? ::I18n
defaults = Array(keys).dup
defaults << Proc.new if block_given?
::I18n.translate(defaults.shift, options.merge(:default => defaults, :scope => :will_paginate))
else
yield key, options
end
end
end
end
2 changes: 1 addition & 1 deletion lib/will_paginate/view_helpers/link_renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def page_number(page)
end

def gap
text = @template.will_paginate_translate('views.will_paginate.page_gap') { '&hellip;' }
text = @template.will_paginate_translate(:page_gap) { '&hellip;' }
%(<span class="gap">#{text}</span>)
end

Expand Down
28 changes: 25 additions & 3 deletions spec/view_helpers/action_view_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
require 'will_paginate/view_helpers/action_view'
require 'will_paginate/collection'

ActionView::Base.send(:include, WillPaginate::ActionView)

Routes = ActionDispatch::Routing::RouteSet.new

Routes.draw do
Expand All @@ -20,7 +18,10 @@
describe WillPaginate::ActionView do

before(:all) do
I18n.load_path << File.expand_path('../../../lib/will_paginate/locale/en.yml', __FILE__)
I18n.load_path.concat WillPaginate::I18n.load_path
end

before(:each) do
I18n.reload!
end

Expand Down Expand Up @@ -277,6 +278,27 @@ def renderer.gap() '<span class="my-gap">~~</span>' end
paginate(nil)
}.should raise_error(ActionView::TemplateError, /@developers/)
end

## i18n

it "is able to translate previous/next labels" do
translation :will_paginate => {
:previous_label => 'Go back',
:next_label => 'Load more'
}

paginate do |pagination|
assert_select 'span.disabled:first-child', 'Go back'
assert_select 'a[rel=next]', 'Load more'
end
end

private

def translation(data)
I18n.available_locales # triggers loading existing translations
I18n.backend.store_translations(:en, data)
end
end

class AdditionalLinkAttributesRenderer < WillPaginate::ActionView::LinkRenderer
Expand Down

0 comments on commit 9d200f9

Please sign in to comment.