From 9d200f957626c1415f3c34ad0be1a2dcabea3ea9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohnic=CC=81?= Date: Sat, 6 Aug 2011 13:29:29 +0200 Subject: [PATCH] extract translate helpers into WillPaginate::I18n module --- lib/will_paginate/i18n.rb | 21 ++++++++++++++ lib/will_paginate/railtie.rb | 4 +-- lib/will_paginate/view_helpers.rb | 13 ++------- .../view_helpers/link_renderer.rb | 2 +- spec/view_helpers/action_view_spec.rb | 28 +++++++++++++++++-- 5 files changed, 52 insertions(+), 16 deletions(-) create mode 100644 lib/will_paginate/i18n.rb diff --git a/lib/will_paginate/i18n.rb b/lib/will_paginate/i18n.rb new file mode 100644 index 000000000..30f3cba0d --- /dev/null +++ b/lib/will_paginate/i18n.rb @@ -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 diff --git a/lib/will_paginate/railtie.rb b/lib/will_paginate/railtie.rb index aa86b6855..bc833bf96 100644 --- a/lib/will_paginate/railtie.rb +++ b/lib/will_paginate/railtie.rb @@ -1,5 +1,6 @@ require 'will_paginate' require 'will_paginate/collection' +require 'will_paginate/i18n' module WillPaginate class Railtie < Rails::Railtie @@ -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 diff --git a/lib/will_paginate/view_helpers.rb b/lib/will_paginate/view_helpers.rb index dd3e5cb89..5f28c9aa0 100644 --- a/lib/will_paginate/view_helpers.rb +++ b/lib/will_paginate/view_helpers.rb @@ -1,5 +1,6 @@ # encoding: utf-8 require 'will_paginate/core_ext' +require 'will_paginate/i18n' module WillPaginate # = Will Paginate view helpers @@ -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. # @@ -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 diff --git a/lib/will_paginate/view_helpers/link_renderer.rb b/lib/will_paginate/view_helpers/link_renderer.rb index 4519f0b22..7016bcd00 100644 --- a/lib/will_paginate/view_helpers/link_renderer.rb +++ b/lib/will_paginate/view_helpers/link_renderer.rb @@ -49,7 +49,7 @@ def page_number(page) end def gap - text = @template.will_paginate_translate('views.will_paginate.page_gap') { '…' } + text = @template.will_paginate_translate(:page_gap) { '…' } %(#{text}) end diff --git a/spec/view_helpers/action_view_spec.rb b/spec/view_helpers/action_view_spec.rb index 23029b68a..62fe0e994 100644 --- a/spec/view_helpers/action_view_spec.rb +++ b/spec/view_helpers/action_view_spec.rb @@ -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 @@ -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 @@ -277,6 +278,27 @@ def renderer.gap() '~~' 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