Skip to content

Commit

Permalink
I18n refactory [#856]
Browse files Browse the repository at this point in the history
- refactory of Hobo::Helper::Translations
- the use of ht method/tag is reserved for keys starting with a model name
- the first key passed to ht must be a model name, not a tableized name
- added show_translation_keys config option that allows to show the used keys right in the output
- added patches to I18n and TranslationHelper
- use of translate helper instead I18n.t where possible
  • Loading branch information
ddnexus committed Nov 22, 2010
1 parent deefcc5 commit eeddeb6
Show file tree
Hide file tree
Showing 26 changed files with 176 additions and 199 deletions.
2 changes: 1 addition & 1 deletion hobo/lib/generators/hobo/i18n/templates/hobo.it.yml
Expand Up @@ -52,7 +52,7 @@ it:
remember_me: "Ricorda i miei dati"
login: "Identificazione"
signup: "Iscrizione"
forgot_password: &forgot_password "Parola Chiave dimentivata?"
forgot_password: &forgot_password "Parola Chiave dimenticata?"

signup:
title: "Iscrizione"
Expand Down
Expand Up @@ -8,13 +8,13 @@
background-repeat: no-repeat;
}

.inplaceeditor-form input, .inplaceeditor-form textarea,
.inplaceeditor-form input, .inplaceeditor-form textarea,
table.new-record textarea, table.new-record input {
border: 1px dotted #666;
padding: 3px; width: 100%;
}
.inplaceeditor-form, .inplaceeditor-form input {
display: inline;
display: inline;
}

/**** Admin ****/
Expand Down Expand Up @@ -99,4 +99,4 @@ optgroup.disabled-option {
height: 1em;
}

input.nil-value { color:grey; }
input.nil-value { color:grey; }
Expand Up @@ -8,7 +8,7 @@ class <%= class_name %>Controller < ApplicationController
hobo_create do
if valid?
self.current_user = this
flash[:notice] = I18n.t("hobo.messages.you_are_site_admin", :default=>"You are now the site administrator")
flash[:notice] = t("hobo.messages.you_are_site_admin", :default=>"You are now the site administrator")
redirect_to home_page
end
end
Expand All @@ -18,7 +18,7 @@ class <%= class_name %>Controller < ApplicationController
def do_accept_invitation
do_transition_action :accept_invitation do
self.current_user = this
flash[:notice] = I18n.t("hobo.messages.you_signed_up", :default=>"You have signed up")
flash[:notice] = t("hobo.messages.you_signed_up", :default=>"You have signed up")
end
end
<% end -%>
Expand Down
1 change: 1 addition & 0 deletions hobo/lib/hobo.rb
Expand Up @@ -17,6 +17,7 @@ def self.root; @@root; end
class Error < RuntimeError; end
class PermissionDeniedError < RuntimeError; end
class UndefinedAccessError < RuntimeError; end
class I18nError < RuntimeError; end

# Empty class to represent the boolean type.
class Boolean; end
Expand Down
4 changes: 2 additions & 2 deletions hobo/lib/hobo/controller.rb
Expand Up @@ -131,7 +131,7 @@ def site_search(query)
results_hash = Hobo.find_by_search(query)
all_results = results_hash.values.flatten.select { |r| r.viewable_by?(current_user) }
if all_results.empty?
render :text => "<p>"+ I18n.t("hobo.live_search.no_results", :default=>["Your search returned no matches."]) + "</p>"
render :text => "<p>"+ t("hobo.live_search.no_results", :default=>["Your search returned no matches."]) + "</p>"
else
# TODO: call one tag that renders all the search results with headings for each model
render_tags(all_results, :search_card, :for_type => true)
Expand All @@ -156,7 +156,7 @@ def not_found(error)
elsif render :not_found, :status => 404
# cool
else
render(:text => I18n.t("hobo.messages.not_found", :default=>["The page you requested cannot be found."]) , :status => 404)
render(:text => t("hobo.messages.not_found", :default=>["The page you requested cannot be found."]) , :status => 404)
end
end

Expand Down
2 changes: 1 addition & 1 deletion hobo/lib/hobo/controller/authentication_support.rb
Expand Up @@ -67,7 +67,7 @@ def access_denied(user_model)
accepts.xml do
headers["Status"] = "Unauthorized"
headers["WWW-Authenticate"] = %(Basic realm="Web Password")
render :text => I18n.t("hobo.messages.unauthenticated", :default=>["Couldn't authenticate you"]), :status => '401 Unauthorized'
render :text => t("hobo.messages.unauthenticated", :default=>["Couldn't authenticate you"]), :status => '401 Unauthorized'
end
end
false
Expand Down
20 changes: 10 additions & 10 deletions hobo/lib/hobo/controller/model.rb
Expand Up @@ -552,7 +552,7 @@ def flash_notice(message)


def create_response(new_action, options={}, &b)
flash_notice (ht( :"#{@this.class.name.tableize}.messages.create.success", :default=>["The #{@this.class.model_name.human} was created successfully"])) if valid?
flash_notice (ht( :"#{@this.class.to_s.underscore}.messages.create.success", :default=>["The #{@this.class.model_name.human} was created successfully"])) if valid?

response_block(&b) or
if valid?
Expand All @@ -566,7 +566,7 @@ def create_response(new_action, options={}, &b)
errors = this.errors.full_messages.join("\n")
wants.html { re_render_form(new_action) }
wants.js { render(:status => 500,
:text => ht( :"#{this.class.name.pluralize.underscore}.messages.create.error", :errors=>errors,:default=>["Couldn't create the #{this.class.name.titleize.downcase}.\n #{errors}"])
:text => ht( :"#{this.class.to_s.underscore}.messages.create.error", :errors=>errors,:default=>["Couldn't create the #{this.class.name.titleize.downcase}.\n #{errors}"])
)}
end
end
Expand All @@ -577,7 +577,7 @@ def hobo_update(*args, &b)
options = args.extract_options!

self.this ||= args.first || find_instance
changes = options[:attributes] || attribute_parameters or raise RuntimeError, I18n.t("hobo.messages.update.no_attribute_error", :default=>["No update specified in params"])
changes = options[:attributes] || attribute_parameters or raise RuntimeError, t("hobo.messages.update.no_attribute_error", :default=>["No update specified in params"])
this.user_update_attributes(current_user, changes)

# Ensure current_user isn't out of date
Expand All @@ -590,7 +590,7 @@ def hobo_update(*args, &b)

def update_response(in_place_edit_field=nil, options={}, &b)

flash_notice (ht(:"#{@this.class.name.pluralize.underscore}.messages.update.success", :default=>["Changes to the #{@this.class.model_name.human} were saved"])) if valid?
flash_notice (ht(:"#{@this.class.name.to_s.underscore}.messages.update.success", :default=>["Changes to the #{@this.class.model_name.human} were saved"])) if valid?

response_block(&b) or
if valid?
Expand All @@ -617,7 +617,7 @@ def update_response(in_place_edit_field=nil, options={}, &b)
errors = @this.errors.full_messages.join("\n")
wants.html { re_render_form(:edit) }
wants.js { render(:status => 500,
:text => ht(:"#{@this.class.name.pluralize.underscore}.messages.update.error",:default=>["There was a problem with that change.\n#{errors}"], :errors=>errors)
:text => ht(:"#{@this.class.to_s.underscore}.messages.update.error",:default=>["There was a problem with that change.\n#{errors}"], :errors=>errors)
) }
end
end
Expand All @@ -628,7 +628,7 @@ def hobo_destroy(*args, &b)
options = args.extract_options!
self.this ||= args.first || find_instance
this.user_destroy(current_user)
flash_notice ht( :"#{model.name.pluralize.underscore}.messages.destroy.success", :default=>["The #{model.name.titleize.downcase} was deleted"])
flash_notice ht( :"#{model.to_s.underscore}.messages.destroy.success", :default=>["The #{model.name.titleize.downcase} was deleted"])
destroy_response(options, &b)
end

Expand Down Expand Up @@ -669,7 +669,7 @@ def do_creator_action(name, options={}, &b)
errors = this.errors.full_messages.join("\n")
wants.html { re_render_form(name) }
wants.js { render(:status => 500,
:text => ht(:"#{@this.class.name.pluralize.underscore}.messages.creator.error", :default=>["Couldn't do creator #{name}.\n#{errors}"], :name=>name, :errors=>errors)
:text => ht(:"#{@this.class.to_s.underscore}.messages.creator.error", :default=>["Couldn't do creator #{name}.\n#{errors}"], :name=>name, :errors=>errors)
)}
end
end
Expand Down Expand Up @@ -711,7 +711,7 @@ def do_transition_action(name, *args, &b)
errors = this.errors.full_messages.join("\n")
wants.html { re_render_form(name) }
wants.js { render(:status => 500,
:text => ht(:"#{@this.class.name.pluralize.underscore}.messages.transition.error", :default=>["Couldn't do transition #{name}.\n#{errors}"], :name=>name, :errors=>errors)
:text => ht(:"#{@this.class.to_s.underscore}.messages.transition.error", :default=>["Couldn't do transition #{name}.\n#{errors}"], :name=>name, :errors=>errors)
)}
end
end
Expand Down Expand Up @@ -767,11 +767,11 @@ def permission_denied(error)
if render :permission_denied, :status => 403
# job done
else
render :text => I18n.t("hobo.messages.permission_denied", :default=>["Permission Denied"]), :status => 403
render :text => t("hobo.messages.permission_denied", :default=>["Permission Denied"]), :status => 403
end
end
wants.js do
render :text => I18n.t("hobo.messages.permission_denied", :default=>["Permission Denied"]), :status => 403
render :text => t("hobo.messages.permission_denied", :default=>["Permission Denied"]), :status => 403
end
end
end
Expand Down
12 changes: 6 additions & 6 deletions hobo/lib/hobo/controller/user.rb
Expand Up @@ -62,8 +62,8 @@ def hobo_login(options={})
end

login_attr = model.human_attribute_name(model.login_attribute)
options.reverse_merge!(:success_notice => ht(:"users.messages.login.success", :default=>["You have logged in."]),
:failure_notice => ht(:"users.messages.login.error", :login=>login_attr, :default=>["You did not provide a valid #{login_attr} and password."]))
options.reverse_merge!(:success_notice => ht(:"#{model.to_s.underscore}.messages.login.success", :default=>["You have logged in."]),
:failure_notice => ht(:"#{model.to_s.underscore}.messages.login.error", :login=>login_attr, :default=>["You did not provide a valid #{login_attr} and password."]))

if request.post?
user = model.authenticate(params[:login], params[:password])
Expand Down Expand Up @@ -113,7 +113,7 @@ def hobo_signup(&b)
def hobo_do_signup(&b)
do_creator_action(:signup) do
if valid?
flash[:notice] = ht(:"users.messages.signup.success", :default=>["Thanks for signing up!"])
flash[:notice] = ht(:"#{model.to_s.underscore}.messages.signup.success", :default=>["Thanks for signing up!"])
end
response_block(&b) or if valid?
self.current_user = this if this.account_active?
Expand All @@ -127,7 +127,7 @@ def hobo_do_signup(&b)


def hobo_logout(options={})
options = options.reverse_merge(:notice => ht(:"users.messages.logout", :default=>["You have logged out."]),
options = options.reverse_merge(:notice => ht(:"#{model.to_s.underscore}.messages.logout", :default=>["You have logged out."]),
:redirect_to => base_url)

logout_current_user
Expand Down Expand Up @@ -155,7 +155,7 @@ def hobo_do_reset_password(&b)
do_transition_action :reset_password do
response_block(&b) or if valid?
self.current_user = this
flash[:notice] = ht(:"users.messages.reset_password", :default=>["Your password has been reset"])
flash[:notice] = ht(:"#{model.to_s.underscore}.messages.reset_password", :default=>["Your password has been reset"])
respond_to do |wants|
wants.html { redirect_to(home_page) }
wants.js { hobo_ajax_response }
Expand All @@ -167,7 +167,7 @@ def hobo_do_reset_password(&b)

def hobo_update_with_account_flash(*args)
hobo_update_without_account_flash(*args) do
flash[:notice] = ht(:"users.messages.update.success", :default=>["Changes to your account were saved"]) if valid? && @this == current_user
flash[:notice] = ht(:"#{model.to_s.underscore}.messages.update.success", :default=>["Changes to your account were saved"]) if valid? && @this == current_user
yield if block_given?
end
end
Expand Down
6 changes: 6 additions & 0 deletions hobo/lib/hobo/engine.rb
Expand Up @@ -14,6 +14,7 @@ class Engine < Rails::Engine
h.rapid_generators_path = Pathname.new File.expand_path('lib/hobo/rapid/generators', Hobo.root)
h.auto_taglibs_path = Pathname.new File.expand_path('app/views/taglibs/auto', Rails.root)
h.read_only_file_system = !!ENV['HEROKU_TYPE']
h.show_translation_keys = false
end

ActiveSupport.on_load(:action_controller) do
Expand All @@ -35,6 +36,7 @@ class Engine < Rails::Engine

ActiveSupport.on_load(:action_view) do
require 'hobo/extensions/action_view/tag_helper'
require 'hobo/extensions/action_view/translation_helper'
end

ActiveSupport.on_load(:before_initialize) do
Expand All @@ -44,6 +46,10 @@ class Engine < Rails::Engine
Dryml::DrymlGenerator.enable([h.rapid_generators_path], h.auto_taglibs_path)
end

initializer 'hobo.i18n' do |app|
require 'hobo/extensions/i18n' if app.config.hobo.show_translation_keys
end

initializer 'hobo.routes' do |app|
h = app.config.hobo
# generate at first boot, so no manual generation is required
Expand Down
20 changes: 20 additions & 0 deletions hobo/lib/hobo/extensions/action_view/translation_helper.rb
@@ -0,0 +1,20 @@
ActionView::Helpers::TranslationHelper.module_eval do

# we need to remove the <span> tag because it will mess up
# the dryml tags when ht is used in some place
# we redefine the method since we cannot catch the rescued exception
# although the only difference is the rescue block
def translate(key, options = {})
translation = I18n.translate(scope_key_by_partial(key), options.merge!(:raise => true))
if html_safe_translation_key?(key) && translation.respond_to?(:html_safe)
translation.html_safe
else
translation
end
rescue I18n::MissingTranslationData => e
keys = I18n.normalize_keys(I18n.locale, key, options[:scope]).join('.')
"[MISSING: #{keys}]"
end
alias_method :t, :translate

end
2 changes: 1 addition & 1 deletion hobo/lib/hobo/extensions/active_model/translation.rb
Expand Up @@ -21,7 +21,7 @@ def human_attribute_name_with_en_pluralization_default(attribute, options={})
# otherwise it returns "".
def attribute_help(attribute, options = {})
defaults = lookup_ancestors.map do |klass|
:"#{self.i18n_scope}.attribute_help.#{klass.model_name.underscore}.#{attribute}"
:"#{self.i18n_scope}.attribute_help.#{klass.to_s.underscore}.#{attribute}"
end

defaults << :"attribute_help.#{attribute}"
Expand Down
16 changes: 16 additions & 0 deletions hobo/lib/hobo/extensions/i18n.rb
@@ -0,0 +1,16 @@
I18n.module_eval do
class << self

def translate_with_show_keys(key, options = {})
translation = translate_without_show_keys(key, options)
keys = normalize_keys(locale, key, options[:scope]).join('.')
"[#{keys}]" + translation
end
alias_method_chain :translate, :show_keys

alias_method :t_without_show_keys, :t
alias_method :t, :translate_with_show_keys

end
end

2 changes: 1 addition & 1 deletion hobo/lib/hobo/helper.rb
Expand Up @@ -113,7 +113,7 @@ def object_url(obj, *args)
def app_name(add_subsite=true)
an = Rails.application.config.hobo.app_name
if add_subsite && subsite
subsite_name = I18n.t 'hobo.admin.subsite_name', :default => subsite.titleize
subsite_name = t 'hobo.admin.subsite_name', :default => subsite.titleize
an = an + " - #{subsite_name}"
end
an
Expand Down

0 comments on commit eeddeb6

Please sign in to comment.