Permalink
Browse files

Add I18n support

ActiveRecord object names are taken from ActiveRecord's internal method (ActiveRecord#human_name)
ActiveRecord object attribute names are taken from ActiveRecord's internal method (ActiveRecord#human_attribute_name(attr))
There is a list of translatable names that can be found in the README and in lib/locale/en.yml
Also a new option was added (Formtastic::SemanticFormBuilder.label_str_method) which is called to format default label text

Signed-off-by: Justin French <justin@indent.com.au>
  • Loading branch information...
1 parent 085e22f commit 282b28c2cf423d473900d777e935960ee4449531 @sprsquish sprsquish committed with Feb 16, 2009
Showing with 233 additions and 20 deletions.
  1. +24 −0 README.textile
  2. +13 −9 lib/formtastic.rb
  3. +12 −0 lib/locale/en.yml
  4. +184 −11 spec/formtastic_spec.rb
View
@@ -95,6 +95,9 @@ If you wish, put something like this in config/initializers/formtastic_config.rb
# Set the way inline errors will be displayed. Valid options are :sentence, :list and :none
Formtastic::SemanticFormBuilder.inline_errors = :sentence
+
+ # Set the method called on label text to determine its format (:titleize, :humanize, :to_s, etc)
+ Formtastic::SemanticFormBuilder.label_str_method = :titleize
</pre>
@@ -207,6 +210,27 @@ Nested forms are supported:
<% end %>
</pre>
+h2. Internationalization (I18n)
+
+Supports I18n! ActiveRecord object names and attributes are, by default, taken from calling @object.human_name and @object.human_attribute_name(attr) respectively. There are a few words specific to Formtastic that can be translated.
+
+Here is an example locale file:
+
+<pre>
+en:
+ formtastic:
+ yes: 'Yes'
+ no: 'No'
+ create: 'Create'
+ save: 'Save'
+ year: 'Year'
+ month: 'Month'
+ day: 'Day'
+ hour: 'Hour'
+ minute: 'Minute'
+ second: 'Second'
+</pre>
+
h2. Extending Formtastic
Adding functionality to Formtastic can be done by extending SemanticFormBuilder and configuring formtastic's builder.
View
@@ -22,8 +22,9 @@ class SemanticFormBuilder < ActionView::Helpers::FormBuilder
@@required_string = %{<abbr title="required">*</abbr>}
@@optional_string = ''
@@inline_errors = :sentence
+ @@label_str_method = :titleize
- cattr_accessor :all_fields_required_by_default, :required_string, :optional_string, :inline_errors
+ cattr_accessor :all_fields_required_by_default, :required_string, :optional_string, :inline_errors, :label_str_method
attr_accessor :template
@@ -71,7 +72,7 @@ def input(method, options = {})
options[:required] = method_required?(method, options[:required])
- options[:label] ||= method.to_s.titleize
+ options[:label] ||= @object.human_attribute_name(method).send(@@label_str_method)
options[:as] ||= default_input_type(@object, method)
input_method = "#{options[:as]}_input"
@@ -266,8 +267,10 @@ def set_options(options)
end
def save_or_create_commit_button_text #:nodoc:
- prefix = @object.new_record? ? "Create" : "Save"
- "#{prefix} #{@object_name.to_s.humanize}"
+ prefix = @object.new_record? ? 'create' : 'save'
+ [ I18n.t(prefix, :default => prefix, :scope => [:formtastic]),
+ @object.human_name
+ ].join(' ').send(@@label_str_method)
end
# Determins if the attribute (eg :title) should be considered required or not.
@@ -523,8 +526,9 @@ def date_or_datetime_input(method, options)
list_items_capture << template.hidden_field_tag("#{@object_name}[#{method}(#{position[input]}i)]", @object.send(method), :id => "#{@object_name}_#{method}_#{position[input]}i")
else
opts = set_options(options).merge({:prefix => @object_name, :field_name => "#{method}(#{position[input]}i)", :include_blank => options[:include_blank]})
+ item_label_text = I18n.t(input.to_s, :default => input.to_s, :scope => [:formtastic]).send(@@label_str_method)
list_items_capture << template.content_tag(:li,
- template.content_tag(:label, input.to_s.humanize, :for => "#{@object_name}_#{method}_#{position[input]}i") +
+ template.content_tag(:label, item_label_text, :for => "#{@object_name}_#{method}_#{position[input]}i") +
template.send("select_#{input}".intern, @object.send(method), opts)
)
end
@@ -566,8 +570,8 @@ def boolean_input(method, options)
#
# TODO: Doesn't handle :include_blank => true, but then again, neither do most of the inputs.
def boolean_select_input(method, options)
- options[:true] ||= "Yes"
- options[:false] ||= "No"
+ options[:true] ||= I18n.t('yes', :default => 'yes', :scope => [:formtastic]).send(@@label_str_method)
+ options[:false] ||= I18n.t('no', :default => 'no', :scope => [:formtastic]).send(@@label_str_method)
choices = [ [options[:true],true], [options[:false],false] ]
input_label(method, options) + template.select(@object_name, method, choices, set_options(options))
@@ -599,8 +603,8 @@ def boolean_select_input(method, options)
# </fieldset>
# </li>
def boolean_radio_input(method, options)
- options[:true] ||= "Yes"
- options[:false] ||= "No"
+ options[:true] ||= I18n.t('yes', :default => 'yes', :scope => [:formtastic]).send(@@label_str_method)
+ options[:false] ||= I18n.t('no', :default => 'no', :scope => [:formtastic]).send(@@label_str_method)
choices = [ {:label => options[:true], :value => true}, {:label => options[:false], :value => false} ]
View
@@ -0,0 +1,12 @@
+en:
+ formtastic:
+ yes: 'Yes'
+ no: 'No'
+ create: 'Create'
+ save: 'Save'
+ year: 'Year'
+ month: 'Month'
+ day: 'Day'
+ hour: 'Hour'
+ minute: 'Minute'
+ second: 'Second'
Oops, something went wrong.

0 comments on commit 282b28c

Please sign in to comment.