Skip to content

Instantly share code, notes, and snippets.

@svenfuchs
Forked from mislav/en.yml
Created August 4, 2011 23:23
Show Gist options
  • Save svenfuchs/1126559 to your computer and use it in GitHub Desktop.
Save svenfuchs/1126559 to your computer and use it in GitHub Desktop.
will_paginate i18n keys

Variations of output:

  1. No users found
  2. Displaying 1 user
  3. Displaying all 27 users
  4. Displaying users 12 - 18 of 27 in total

The problem is, "user/users" is a variable name since this view helpers accepts collections of any kind of objects. So I've really got 2 dimensions for i18n:

  1. translation for the name of the object ("User" in this case) and its plural form
  2. translation of view helper output outlined above.

Below are translations that handle scenario 2), but I have no inspiration about 1).

en:
# ommitting the :views namespace here probably isn't an option?
will_paginate:
page_entries_info:
single_page:
zero: "No %{model} found"
one: "Displaying 1 %{model}"
other: "Displaying all %{count} %{model}"
multi_page: "Displaying %{model} %{from} - %{to} of %{total} in total"
models:
user:
# zero: users # not required in English, but other locales could use this
one: user
other: users
pages: # controller name
page_entries_info:
single_page:
zero: "Nothing found"
one: "Displaying 1 page"
other: "Displaying all %{count} pages"
multi_page: "Displaying pages %{from} - %{to} of %{total} in total"
very_special_things: # controller name
index: # action name
page_entries_info:
single_page:
zero: "No thingongs found"
one: "Displaying 1 thingily"
other: "Displaying all %{count} things"
multi_page: "Displaying things %{from} - %{to} of %{total} in total"
module SomeGoodPlaceInWillPaginate
def translate(key, options = {})
# I've just copied these :cascade options from http://svenfuchs.com/2011/2/11/organizing-translations-with-i18n-cascade-and-i18n-missingtranslations
# I don't think they actually work exactly like this, but i'm too tired right now :) Would need to look up in I18n::Backend::Cascade.
#
# super would call the ActionView#translate helper here
super(key, options.merge(:cascade => { :offset => 2, :skip_root => false }))
end
alias t translate
end
# somewhere else ...
scope = :'views.will_paginate'
key = resource.class.name.underscore
default = ... # not sure. there's now Model.human_name. not sure it accepts a :count in Rails 2.3.x already.
model = t(key, :count => count, :default => default, :scope => [scope, :models])
t(:page_entries_info, :count => count, :model => model, :scope => scope)
# any controller/action, with resource == User, count == 0
"No users found"
# on very_special_things/index, count == 0
"No thingongs found"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment