From eeddeb6c44e7f005fd5f3ee7b363cd86cdef6539 Mon Sep 17 00:00:00 2001
From: Domizio Demichelis
Date: Mon, 22 Nov 2010 13:19:21 -0400
Subject: [PATCH] I18n refactory [#856]
- 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
---
.../hobo/i18n/templates/hobo.it.yml | 2 +-
.../clean/public/stylesheets/rapid-ui.css | 6 +-
.../templates/controller.rb.erb | 4 +-
hobo/lib/hobo.rb | 1 +
hobo/lib/hobo/controller.rb | 4 +-
.../hobo/controller/authentication_support.rb | 2 +-
hobo/lib/hobo/controller/model.rb | 20 +--
hobo/lib/hobo/controller/user.rb | 12 +-
hobo/lib/hobo/engine.rb | 6 +
.../action_view/translation_helper.rb | 20 +++
.../extensions/active_model/translation.rb | 2 +-
hobo/lib/hobo/extensions/i18n.rb | 16 +++
hobo/lib/hobo/helper.rb | 2 +-
hobo/lib/hobo/helper/translations.rb | 129 ++++++++----------
.../rapid/generators/rapid/cards.dryml.erb | 4 +-
.../rapid/generators/rapid/forms.dryml.erb | 4 +-
.../rapid/generators/rapid/pages.dryml.erb | 14 +-
hobo/lib/hobo/rapid/taglibs/rapid_core.dryml | 16 +--
hobo/lib/hobo/rapid/taglibs/rapid_forms.dryml | 16 +--
.../hobo/rapid/taglibs/rapid_generics.dryml | 2 +-
hobo/lib/hobo/rapid/taglibs/rapid_i18n.dryml | 17 ---
.../hobo/rapid/taglibs/rapid_lifecycles.dryml | 10 +-
.../hobo/rapid/taglibs/rapid_navigation.dryml | 2 +-
hobo/lib/hobo/rapid/taglibs/rapid_plus.dryml | 12 +-
.../rapid/taglibs/rapid_translations.dryml | 36 -----
.../hobo/rapid/taglibs/rapid_user_pages.dryml | 16 +--
26 files changed, 176 insertions(+), 199 deletions(-)
create mode 100644 hobo/lib/hobo/extensions/action_view/translation_helper.rb
create mode 100644 hobo/lib/hobo/extensions/i18n.rb
delete mode 100644 hobo/lib/hobo/rapid/taglibs/rapid_translations.dryml
diff --git a/hobo/lib/generators/hobo/i18n/templates/hobo.it.yml b/hobo/lib/generators/hobo/i18n/templates/hobo.it.yml
index 78b73bb24..060ecc69f 100644
--- a/hobo/lib/generators/hobo/i18n/templates/hobo.it.yml
+++ b/hobo/lib/generators/hobo/i18n/templates/hobo.it.yml
@@ -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"
diff --git a/hobo/lib/generators/hobo/rapid/templates/themes/clean/public/stylesheets/rapid-ui.css b/hobo/lib/generators/hobo/rapid/templates/themes/clean/public/stylesheets/rapid-ui.css
index 7fceec8db..f523d7f42 100755
--- a/hobo/lib/generators/hobo/rapid/templates/themes/clean/public/stylesheets/rapid-ui.css
+++ b/hobo/lib/generators/hobo/rapid/templates/themes/clean/public/stylesheets/rapid-ui.css
@@ -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 ****/
@@ -99,4 +99,4 @@ optgroup.disabled-option {
height: 1em;
}
-input.nil-value { color:grey; }
\ No newline at end of file
+input.nil-value { color:grey; }
diff --git a/hobo/lib/generators/hobo/user_controller/templates/controller.rb.erb b/hobo/lib/generators/hobo/user_controller/templates/controller.rb.erb
index e7dc329b4..ac3176e92 100644
--- a/hobo/lib/generators/hobo/user_controller/templates/controller.rb.erb
+++ b/hobo/lib/generators/hobo/user_controller/templates/controller.rb.erb
@@ -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
@@ -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 -%>
diff --git a/hobo/lib/hobo.rb b/hobo/lib/hobo.rb
index b4adc514a..e6124baf5 100644
--- a/hobo/lib/hobo.rb
+++ b/hobo/lib/hobo.rb
@@ -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
diff --git a/hobo/lib/hobo/controller.rb b/hobo/lib/hobo/controller.rb
index 252e4f420..bd838eaf0 100644
--- a/hobo/lib/hobo/controller.rb
+++ b/hobo/lib/hobo/controller.rb
@@ -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 => ""+ I18n.t("hobo.live_search.no_results", :default=>["Your search returned no matches."]) + "
"
+ render :text => ""+ t("hobo.live_search.no_results", :default=>["Your search returned no matches."]) + "
"
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)
@@ -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
diff --git a/hobo/lib/hobo/controller/authentication_support.rb b/hobo/lib/hobo/controller/authentication_support.rb
index ce7da92ef..f9de9101e 100644
--- a/hobo/lib/hobo/controller/authentication_support.rb
+++ b/hobo/lib/hobo/controller/authentication_support.rb
@@ -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
diff --git a/hobo/lib/hobo/controller/model.rb b/hobo/lib/hobo/controller/model.rb
index df8bacbb3..39fcaa3f0 100644
--- a/hobo/lib/hobo/controller/model.rb
+++ b/hobo/lib/hobo/controller/model.rb
@@ -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?
@@ -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
@@ -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
@@ -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?
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
diff --git a/hobo/lib/hobo/controller/user.rb b/hobo/lib/hobo/controller/user.rb
index 1c450f163..fa0785810 100644
--- a/hobo/lib/hobo/controller/user.rb
+++ b/hobo/lib/hobo/controller/user.rb
@@ -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])
@@ -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?
@@ -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
@@ -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 }
@@ -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
diff --git a/hobo/lib/hobo/engine.rb b/hobo/lib/hobo/engine.rb
index da95d5ea6..51de6d150 100644
--- a/hobo/lib/hobo/engine.rb
+++ b/hobo/lib/hobo/engine.rb
@@ -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
@@ -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
@@ -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
diff --git a/hobo/lib/hobo/extensions/action_view/translation_helper.rb b/hobo/lib/hobo/extensions/action_view/translation_helper.rb
new file mode 100644
index 000000000..4a65305e7
--- /dev/null
+++ b/hobo/lib/hobo/extensions/action_view/translation_helper.rb
@@ -0,0 +1,20 @@
+ActionView::Helpers::TranslationHelper.module_eval do
+
+ # we need to remove the 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
diff --git a/hobo/lib/hobo/extensions/active_model/translation.rb b/hobo/lib/hobo/extensions/active_model/translation.rb
index 57d9a77a4..698d2e2d8 100644
--- a/hobo/lib/hobo/extensions/active_model/translation.rb
+++ b/hobo/lib/hobo/extensions/active_model/translation.rb
@@ -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}"
diff --git a/hobo/lib/hobo/extensions/i18n.rb b/hobo/lib/hobo/extensions/i18n.rb
new file mode 100644
index 000000000..25aefafc9
--- /dev/null
+++ b/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
+
diff --git a/hobo/lib/hobo/helper.rb b/hobo/lib/hobo/helper.rb
index 3592b02f9..f968e9fc1 100644
--- a/hobo/lib/hobo/helper.rb
+++ b/hobo/lib/hobo/helper.rb
@@ -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
diff --git a/hobo/lib/hobo/helper/translations.rb b/hobo/lib/hobo/helper/translations.rb
index 27725e071..13fc1c0d3 100644
--- a/hobo/lib/hobo/helper/translations.rb
+++ b/hobo/lib/hobo/helper/translations.rb
@@ -2,86 +2,73 @@ module Hobo
module Helper
module Translations
- # --- Translation Helper --- #
- #
- # Uses RoR native I18n.translate.
- #
- # Adds some conventions for easier hobo translation.
- # 1. Assumes the first part of the key to be a model name (e.g.: users.index.title -> user)
- # 2. Tries to translate the model by lookup for: (e.g.: user-> activerecord.models.user)
- # 3. Adds a default fallback to the beginning of the fallback chain
- # by replacing the first part of the key with "hobo" and using the translated model name
- # as additional attribute. This allows us to have default translations
- # (e.g.: hobo.index.title: "{{model}} Index")
- #
- # Is also used as a tag in the dryml-view files. The syntax is:
- # My Application
- # --> Will lookup the "my.app"-key for your locale and replaces the "My Application" content
- # if found.
- #
- # My Application
- # --> Will look up both the "my"- and "app"-key for your locale, and replaces the
- # "My Application" with the "my"-key contents (interpolated using the "app"-key.
- # sample.en.yml-file:
- # "no":
- # my: "Mitt {{app}}"
- # The output should be: Mitt Program
- #
- # The "count" option is passed to the I18n.t method as is
- #
- def ht(key, options={})
-
- # Check if called as a tag, i.e. like this
- if (key.class == Hash)
+ # simple wrapper around the translate helper
+ # it implements a dryml and a tag
+
+ def translate(*args)
+ key, options = normalize_args(*args)
+ super key.to_sym, options
+ end
+ alias_method :t, :translate
+
+=begin
+
+hobo_translate / ht
+
+Wrapper around the Rails :translate helper with hobo added features.
+
+It can be used as a regular helper or as a dryml tag.
+
+Hobo Added Features
+
+The first part of the key must be a model name (e.g.: user.index.title -> user). This method will add a "model" interpolation variable set to the translated and pluralized Model.model_name.human. Besides, it will add a default 'hobo.' fallback, (e.g.: hobo.index.title) at the beginning of the fallback chain.
+
+You can also pass any other :translate option like for example :count.
+
+Example:
+
+<%= ht :key=>'user.index.title', :default=>'Index' %>
+Index
+#=> "Index" # if "user.index.title" or "hobo.index.title" is not found
+#=> "User Index" # with the below en.yml file
+
+=== en.yml ===
+en:
+ hobo:
+ index:
+ title: %{model} Index
+
+=end
+
+ def hobo_translate(*args)
+ key, options = normalize_args(*args)
+ keys = key.to_s.split(".")
+ model_name = keys.shift
+ model_class = begin model_name.camelize.constantize; rescue; end
+ unless model_class && model_class < ActiveRecord::Base
+ raise Hobo::I18nError, %(wrong model name: "#{model_name}" (extracted from translation key). You might want to use the translate/t tag/method instead.)
+ end
+ options[:default].unshift("hobo.#{keys.join(".")}".to_sym)
+ options[:model] = model_class.model_name.human(:count=>options[:count]||1)
+ translate key.to_sym, options
+ end
+ alias_method :ht, :hobo_translate
+
+ private
+
+ def normalize_args(key, options={})
+ if (key.class == Hash) # called as a tag
if key.has_key?(:default) && !key[:default].blank?
- Rails.logger.warn "hobo-i18n: 'default' should not be used as an attribute on the ht-tag. If used, then you need to make sure that the tags inner-contents are not used. These are normally treated as defaults automatically, but if there is a default attribute then that inner-content will be hidden from this method - and will not be replaced with the translation found."
+ Rails.logger.warn "hobo-i18n: 'default' should not be used as an attribute on *translate tags. If used, then you need to make sure that the tags inner-contents are not used. These are normally treated as defaults automatically, but if there is a default attribute then that inner-content will be hidden from this method - and will not be replaced with the translation found."
end
defaults = options[:default]
- # Swap key and options, remove options[:key]
options = key
key = options.delete(:key)
# Set options[:default] to complete the tag-argument-conversion process.
options[:default] = defaults.call(options) if defaults.class == Proc
end
options[:default] = Array.wrap options[:default]
-
- # assume the first part of the key to be the model
- keys = key.to_s.split(".")
- if keys.length > 1
- model = keys.shift()
- subkey = keys.join(".")
- else
- subkey = key
- end
-
- # will skip useless code in case the first part of the key is 'hobo'
- model = '' if model.eql?('hobo')
-
- unless model.blank?
- klass = begin
- model.singularize.camelize.constantize
- rescue NameError
- end
- # add :"hobo.#{key}" as the first fallback
- options[:default].unshift("hobo.#{subkey}".to_sym)
- # translate the model
- # the singularize method is used because Hobo does not keep the ActiveRecord convention in its tags
- # no default needed because human_name defaults to the model name
- # try because Hobo class is not an ActiveRecord::Base subclass
- translated_pluralized_model = klass.try.model_name.try.human(:count=>options[:count]||1)
- options[:model] = translated_pluralized_model
- end
-
- key_prefix = "#{key}" if defined?(HOBO_SHOW_LOCALE_KEYS) && HOBO_SHOW_LOCALE_KEYS
-
- Rails.logger.info "..translate(#{key}, #{options.inspect}) to #{I18n.locale}" if defined?(HOBO_VERBOSE_TRANSLATIONS)
-
- translation = I18n.translate(key.to_sym, options)
- if translation.respond_to? :to_str
- key_prefix ? translation.to_str+key_prefix : translation
- else
- "translation invalid: #{key}"
- end
+ [key, options]
end
end
diff --git a/hobo/lib/hobo/rapid/generators/rapid/cards.dryml.erb b/hobo/lib/hobo/rapid/generators/rapid/cards.dryml.erb
index 0102b74df..97f6f1677 100644
--- a/hobo/lib/hobo/rapid/generators/rapid/cards.dryml.erb
+++ b/hobo/lib/hobo/rapid/generators/rapid/cards.dryml.erb
@@ -19,7 +19,7 @@ has_body = (!show_link && description_attribute) || creator_link || creator_a
show_title = !show_link && (name_attribute || !has_body)
-model_key = model.name.pluralize.underscore
+model_key = model.to_s.underscore
-%>
@@ -53,7 +53,7 @@ model_key = model.name.pluralize.underscore
param="creator"/>
<% end -%>
<% if primary_collection -%>
-
+
param/>
<% end -%>
diff --git a/hobo/lib/hobo/rapid/generators/rapid/forms.dryml.erb b/hobo/lib/hobo/rapid/generators/rapid/forms.dryml.erb
index acd338912..67eed4656 100644
--- a/hobo/lib/hobo/rapid/generators/rapid/forms.dryml.erb
+++ b/hobo/lib/hobo/rapid/generators/rapid/forms.dryml.erb
@@ -4,7 +4,7 @@ form_fields = standard_fields :belongs_to, :has_many
cancel_to_show_page = linkable?(:show)
cancel_to_index_page = !cancel_to_index_page && linkable?(:index)
-model_key = model.name.tableize
+model_key = model.to_s.underscore
-%>
<% end -%>
-<% end # of each_model do -%>
\ No newline at end of file
+<% end # of each_model do -%>
diff --git a/hobo/lib/hobo/rapid/generators/rapid/pages.dryml.erb b/hobo/lib/hobo/rapid/generators/rapid/pages.dryml.erb
index d3bc42a83..62e67525d 100644
--- a/hobo/lib/hobo/rapid/generators/rapid/pages.dryml.erb
+++ b/hobo/lib/hobo/rapid/generators/rapid/pages.dryml.erb
@@ -4,7 +4,7 @@
Home
<% models.select { |m| linkable?(m, :index) }[0..4].each do |m| -%>
-
+
<% end -%>
@@ -17,7 +17,7 @@
<%
new_link = linkable?(:new)
new_form = !new_link && linkable?(model, :create, :method => :post)
-model_key = model.name.tableize
+model_key = model.to_s.underscore
-%>
@@ -177,7 +177,7 @@ end
<% if collection -%>
-
+
@@ -190,7 +190,7 @@ end
<% if add_link -%>
action="new" if="&can_create?(@<%= model.name.underscore %>.<%= collection %>)" param="new-link">
-
+
New <%= collection.to_s.singularize.titleize %>
>
@@ -198,13 +198,13 @@ end
-
+
Add <%= a_or_an collection.to_s.singularize.titleize %>
<% end -%>
@@ -223,7 +223,7 @@ end
<% else -%>
-
+
diff --git a/hobo/lib/hobo/rapid/taglibs/rapid_core.dryml b/hobo/lib/hobo/rapid/taglibs/rapid_core.dryml
index 23aca7bea..fc53e2d59 100644
--- a/hobo/lib/hobo/rapid/taglibs/rapid_core.dryml
+++ b/hobo/lib/hobo/rapid/taglibs/rapid_core.dryml
@@ -493,7 +493,7 @@ Assuming the context is a blog post...
%>
-<%= this ? I18n.t('hobo.boolean_yes', :default => 'Yes') : I18n.t('hobo.boolean_no', :default => 'No') %>
+<%= this ? t('hobo.boolean_yes', :default => 'Yes') : t('hobo.boolean_no', :default => 'No') %>
@@ -591,7 +591,7 @@ You can pass a summary attribute, which will generate a complete localized sente
else
key = summary.kind_of?(String) ? summary : "default"
default = c == 1 ? "There is 1 #{label}" : "There are #{c} #{label}"
- I18n.t "tags.count.#{key}", {:count=>c, :label=>label, :default=>default}.merge(attributes)
+ t "tags.count.#{key}", {:count=>c, :label=>label, :default=>default}.merge(attributes)
end
end
%>
@@ -661,12 +661,12 @@ The "titleize" attribute is deprecated: use "capitalize" instead.
when attributes[:do] then 'do'
end -%>
- <%= s = I18n.t("tags.you.current_user.#{k || attributes.keys.first || 'default'}",:default=>"you #{k}")
+ <%= s = t("tags.you.current_user.#{k || attributes.keys.first || 'default'}",:default=>"you #{k}")
(titleize||capitalize) ? s.sub(/^./){|c| c.upcase} : s %>
- <%= s = I18n.t("tags.you.other_user.#{k || attributes.keys.first || 'default'}", :name=>this.name,
+ <%= s = t("tags.you.other_user.#{k || attributes.keys.first || 'default'}", :name=>this.name,
:default=>"#{this.name} #{'has' if attributes[:have]}#{'is' if attributes[:are]}#{'does' if attributes[:do]}")
(titleize||capitalize) ? s.sub(/^./){|c| c.upcase} : s %>
@@ -738,14 +738,14 @@ Notes
options = {:default=>[d], :count=>(attrs[:count]||1), :name=>this.name}
your_key = key.split('.').last
unless key.eql?(your_key) || attrs.has_key?(your_key.to_sym)
- options[your_key.to_sym] = I18n.t(key, :count=>options[:count], :default=>your_key.titleize)
+ options[your_key.to_sym] = t(key, :count=>options[:count], :default=>your_key.titleize)
end
s = if this == current_user
options[:default].unshift :"tags.your.default.current_user"
- I18n.t("tags.your.#{your_key}.current_user", options.merge(attrs))
+ t("tags.your.#{your_key}.current_user", options.merge(attrs))
else
options[:default].unshift :"tags.your.default.other_user"
- I18n.t("tags.your.#{your_key}.other_user", options.merge(attrs))
+ t("tags.your.#{your_key}.other_user", options.merge(attrs))
end
capitalize ? s.sub(/^./){|c| c.upcase} : s
%>
@@ -774,7 +774,7 @@ Notes
"button" }
end)
- label ||= I18n.t("hobo.actions.remove", :default=>"Remove")
- confirm = I18n.t("hobo.messages.confirm", :default=>"Are you sure?") if confirm.nil?
+ label ||= t("hobo.actions.remove", :default=>"Remove")
+ confirm = t("hobo.messages.confirm", :default=>"Are you sure?") if confirm.nil?
add_classes!(attributes,
image ? "image-button" : "button",
@@ -623,7 +623,7 @@ All of the standard ajax attributes are supported (see the main taglib documenti
new = class_or_assoc.new(fields)
new.set_creator(current_user)
if can_create?(new)
- label ||= ht("#{new.class.name.tableize}.actions.new", :default=>"New #{new.class.model_name.human}")
+ label ||= ht("#{new.class.to_s.underscore}.actions.new", :default=>"New #{new.class.model_name.human}")
ajax_attributes = { :message => message }
class_name = new.class.name.underscore
ajax_attributes[:params] = { class_name => fields } unless fields.empty?
@@ -652,7 +652,7 @@ For situations where there are too many target records to practically include in
-->
<%
- raise Hobo::PermissionDeniedError.new("Not allowed to edit #{this_field}") if !attributes[:disabled] && !can_edit?
+ raise Hobo::PermissionDeniedError.new("Not allowed to edit #{this_field}") if !attributes[:disabled] && !can_edit?
blank_message ||= ht("#{this_type.name.tableize}.message.no", :default=>"(No #{this_type.model_name.human})")
limit ||= 100
@@ -797,12 +797,12 @@ An input for `has_many :through` associations that lets the user chose the items
To use this tag, the model of the items the user is chosing *must* have unique names, and the
-->
<%
- prompt ||= ht("#{this_field.tableize}.form.select_many.prompt", :default=>"Add #{this_field.titleize.singularize}")
+ prompt ||= ht("#{this_field.underscore}.form.select_many.prompt", :default=>"Add #{this_field.titleize.singularize}")
options ||= this_field_reflection.klass.all(:conditions =>this.send(:conditions)).select {|x| can_view?(x)}
name ||= param_name_for_this
values = this
- remove_label ||= I18n.t("hobo.actions.remove", :default=>'Remove')
+ remove_label ||= t("hobo.actions.remove", :default=>'Remove')
-%>
@@ -105,16 +105,16 @@ This tag assumes the controller has a `reorder` action and the model has a `posi
name ||= model_class.name.downcase.pluralize -%>
@@ -210,9 +210,9 @@ Besides the "tags.filter_menu.default.no_filter" key is used as default of the a
-->
- <% opt = I18n.t("tags.filter_menu.#{param_name}.options", :default=>'')
+ <% opt = t("tags.filter_menu.#{param_name}.options", :default=>'')
options = opt unless opt.blank?
- no_filter = I18n.t("tags.filter_menu.#{param_name}.no_filter", :default=>[:"tags.filter_menu.default.no_filter", no_filter, "All"]) %>
+ no_filter = t("tags.filter_menu.#{param_name}.no_filter", :default=>[:"tags.filter_menu.default.no_filter", no_filter, "All"]) %>