项目作者: jonathanhefner

项目描述 :
Rails page titles via I18n
高级语言: Ruby
项目地址: git://github.com/jonathanhefner/dub_thee.git
创建时间: 2018-07-07T21:56:25Z
项目社区:https://github.com/jonathanhefner/dub_thee

开源协议:MIT License

下载


dub_thee

Rails page titles via I18n. For example, given the following
application layout:

  1. <!-- app/views/layouts/application.html.erb -->
  2. <html>
  3. <head>
  4. <title><%= page_title %> | My Web Site</title>
  5. </head>
  6. ...
  7. </html>

And the following translations:

  1. ## config/locales/page_title.en.yml
  2. en:
  3. page_title:
  4. home:
  5. index: "Welcome"

The “app/views/home/index.html.erb” view will automatically be titled
“Welcome | My Web Site”.

The page_title helper fetches the title via I18n.t, using the
controller name and action name to make up the translation key. Any
action name is allowed, however “create”, “update”, and “delete” are
special-cased to instead fetch the titles of their render-on-failure
counterparts: “new”, “edit”, and “show”, respectively.

Variable interpolation

For more dynamic titles, view assignment variables are made available
for interpolation. Additionally, the i18n-interpolate_nested gem
is included to enable attribute interpolation. For example, given the
following translations:

  1. ## config/locales/page_title.en.yml
  2. en:
  3. page_title:
  4. products:
  5. show: "%{product.name} (%{product.brand})"

The value of page_title for “app/views/products/show.html.erb” will be
equivalent to "#{@product[:name]} (#{@product[:brand]})".

Generic titles

Generic action titles (i.e. not controller-specific) are also supported.
To allow such titles to be slightly more dynamic, two additional values
are provided for interpolation: plural is the String#titleize‘d name
of the controller, and singular is the String#singularize‘d form of
plural. For example, given the following translations:

  1. ## config/locales/page_title.en.yml
  2. en:
  3. page_title:
  4. index: "%{plural}"
  5. new: "New %{singular}"

The value of page_title for “app/views/part_orders/index.html.erb”
will be "Part Orders", and for “app/views/locations/index.html.erb”
will be "Locations". Likewise, the value of page_title for
“app/views/part_orders/new.html.erb” will be "New Part Order", and for
“app/views/locations/new.html.erb” will be "New Location".

Additional logic

Additional logic can be incorporated by using the @page_title
variable. If @page_title is set, the page_title helper will simply
return @page_title. Thus, individual views can implement more nuanced
title logic. For example, given the following translations:

  1. ## config/locales/page_title.en.yml
  2. en:
  3. page_title:
  4. users:
  5. show: "%{user.name}"

And the following view:

  1. <!-- app/views/users/show.html.erb -->
  2. <% @page_title = "Your Profile" if current_user == @user %>
  3. ...

The value of page_title in “app/views/layouts/application.html.erb”
will be "Your Profile" if the current logged-in user is viewing their
own profile. Else, it will be equivalent to "#{@user[:name]}".

Installation

Add the gem to your Gemfile:

  1. $ bundle add dub_thee

And run the install generator:

  1. $ rails generate dub_thee:install

Contributing

Run bin/test to run the tests.

License

MIT License