Skip to content

Commit

Permalink
Improve page title generation to use better practices for i18n singul…
Browse files Browse the repository at this point in the history
…ar/plural lookup. Improved model name lookup for URL slug form hint.
  • Loading branch information
reidab committed Nov 5, 2011
1 parent 13f685f commit 051d886
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 20 deletions.
13 changes: 8 additions & 5 deletions app/controllers/application_controller.rb
Expand Up @@ -93,24 +93,27 @@ def allow_login_as_specific_user?
helper_method :allow_login_as_specific_user?

def page_title(value=nil)
model_name = controller_name.singularize.underscore
model_name_singular_human = t("activerecord.models.#{model_name}.one", :default => controller_name.singularize.humanize).downcase

@page_title = value unless value.nil?

if @page_title.nil?
@page_title ||=
case action_name.to_sym
when :index
t("activerecord.models.#{controller_name.downcase}" , :default => controller_name.humanize.titleize)
t("activerecord.models.#{model_name}.other" , :default => controller_name.humanize.titleize)
when :new, :create
t("title.model.new",:modelname => t("activerecord.models.#{controller_name.singularize.downcase}" , :default => controller_name.singularize.humanize.downcase))
t("title.model.new", :modelname => model_name_singular_human)
when :edit, :update
t("title.model.edit",:modelname => t("activerecord.models.#{controller_name.singularize.downcase}", :default => controller_name.singularize.humanize.downcase))
t("title.model.edit",:modelname => model_name_singular_human)
when :destroy
t("title.model.destroy",:modelname => t("activerecord.models.#{controller_name.singularize.downcase}", :default => controller_name.singularize.humanize.downcase))
t("title.model.destroy",:modelname => model_name_singular_human)
else
begin
get_resource_ivar.name
rescue Exception => e
t("activerecord.models.#{controller_name.singularize.downcase}")
model_name_singular_human.titleize
end
end
else
Expand Down
4 changes: 2 additions & 2 deletions app/views/site/_custom_slug_field.html.haml
Expand Up @@ -2,9 +2,9 @@
-#
-# ARGUMENTS:
-# * form: The Rails form instance.
- hint = "%{example} %{url}/<em><b>%{my}-%{model}</b></em>" % { |
- hint = "%{example} %{url}/<em><b>%{my}_%{model}</b></em>" % { |
:example => t('field.example.fragment'), |
:url => "#{SETTINGS.organization.url}/#{controller_path}", |
:my => t('field.my.fragment'), |
:model => t("activerecord.models.#{form.send(:model_name).underscore}").underscore }
:model => form.object.class.model_name.human.underscore }
= form.input :custom_slug, :hint => hint.html_safe
23 changes: 22 additions & 1 deletion config/locales/en.yml
Expand Up @@ -25,7 +25,28 @@ en:
url: "Website (URL)"

activerecord:
# models: # Defaults are fine for English.
models:
authentication:
one: Authentication
other: Authentications
company:
one: Company
other: Companies
group:
one: Group
other: Groups
person:
one: Person
other: People
project:
one: Project
other: Projects
resource_link:
one: Ressource
other: Ressources
user:
one: User
other: Users
attributes:
# Common attribute names provided by `attributes`.
person:
Expand Down
33 changes: 21 additions & 12 deletions config/locales/fr.yml
Expand Up @@ -24,18 +24,27 @@ fr:

activerecord:
models:
group: Groupe
groups: Groupes
people: Personnes
person: Personne
company: Entreprise
companies: Entreprises
project: Projet
projects: Projets
resource_link: Ressource
resources_links: Ressources
user: Utilisateur
users: Utilisateurs
authentication:
one: Authentification
other: Authentifications
company:
one: Entreprise
other: Entreprises
group:
one: Groupe
other: Groupes
person:
one: Personne
other: Personnes
project:
one: Projet
other: Projets
resource_link:
one: Ressource
other: Ressources
user:
one: Utilisateur
other: Utilisateurs
attributes:
# Common attribute names provided by `attributes`.
person:
Expand Down

0 comments on commit 051d886

Please sign in to comment.