Permalink
Browse files

Convert error and exception strings to be internationalised

  • Loading branch information...
anthonybatchelor-examtime committed Aug 23, 2012
1 parent bd75694 commit ed69043eea924cd15ca09c282d553383abfef72d
View
@@ -1,8 +1,11 @@
-require File.join(File.expand_path(File.dirname(__FILE__)), 'ancestry/class_methods')
-require File.join(File.expand_path(File.dirname(__FILE__)), 'ancestry/instance_methods')
-require File.join(File.expand_path(File.dirname(__FILE__)), 'ancestry/exceptions')
-require File.join(File.expand_path(File.dirname(__FILE__)), 'ancestry/has_ancestry')
+require File.join(File.expand_path(File.dirname(__FILE__)), 'ancestry', 'class_methods')
+require File.join(File.expand_path(File.dirname(__FILE__)), 'ancestry', 'instance_methods')
+require File.join(File.expand_path(File.dirname(__FILE__)), 'ancestry', 'exceptions')
+require File.join(File.expand_path(File.dirname(__FILE__)), 'ancestry', 'has_ancestry')
+
+I18n.load_path += Dir[File.join(File.expand_path(File.dirname(__FILE__)),
+ 'ancestry', 'locales', '*.{rb,yml}').to_s]
module Ancestry
ANCESTRY_PATTERN = /\A[0-9]+(\/[0-9]+)*\Z/
-end
+end
@@ -12,7 +12,7 @@ def scope_depth depth_options, depth
if [:before_depth, :to_depth, :at_depth, :from_depth, :after_depth].include? scope_name
scope.send scope_name, depth + relative_depth
else
- raise Ancestry::AncestryException.new("Unknown depth option: #{scope_name}.")
+ raise Ancestry::AncestryException.new(I18n.t("ancestry.unkown_depth_option", {:scope_name => scope_name}))
end
end
end
@@ -23,7 +23,7 @@ def orphan_strategy= orphan_strategy
if [:rootify, :adopt, :restrict, :destroy].include? orphan_strategy
class_variable_set :@@orphan_strategy, orphan_strategy
else
- raise Ancestry::AncestryException.new("Invalid orphan strategy, valid ones are :rootify,:adopt, :restrict and :destroy.")
+ raise Ancestry::AncestryException.new(I18n.t("ancestry.invalid_orphan_strategy"))
end
end
@@ -80,19 +80,32 @@ def check_ancestry_integrity! options = {}
begin
# ... check validity of ancestry column
if !node.valid? and !node.errors[node.class.ancestry_column].blank?
- raise Ancestry::AncestryIntegrityException.new("Invalid format for ancestry column of node #{node.id}: #{node.read_attribute node.ancestry_column}.")
+ raise Ancestry::AncestryIntegrityException.new(I18n.t("ancestry.invalid_ancestry_column",
+ {
+ :node_id => node.id,
+ :ancestry_column => "#{node.read_attribute node.ancestry_column}"
+ }))
end
# ... check that all ancestors exist
node.ancestor_ids.each do |ancestor_id|
unless exists? ancestor_id
- raise Ancestry::AncestryIntegrityException.new("Reference to non-existent node in node #{node.id}: #{ancestor_id}.")
+ raise Ancestry::AncestryIntegrityException.new(I18n.t("ancestry.reference_nonexistent_node",
+ {
+ :node_id => node.id,
+ :ancestor_id => ancestor_id
+ }))
end
end
# ... check that all node parents are consistent with values observed earlier
node.path_ids.zip([nil] + node.path_ids).each do |node_id, parent_id|
parents[node_id] = parent_id unless parents.has_key? node_id
unless parents[node_id] == parent_id
- raise Ancestry::AncestryIntegrityException.new("Conflicting parent id found in node #{node.id}: #{parent_id || 'nil'} for node #{node_id} while expecting #{parents[node_id] || 'nil'}")
+ raise Ancestry::AncestryIntegrityException.new(I18n.t("ancestry.conflicting_parent_id",
+ {
+ :node_id => node_id,
+ :parent_id => parent_id || 'nil',
+ :expected => parents[node_id] || 'nil'
+ }))
end
end
rescue Ancestry::AncestryIntegrityException => integrity_exception
@@ -161,7 +174,7 @@ def build_ancestry_from_parent_ids! parent_id = nil, ancestry = nil
# Rebuild depth cache if it got corrupted or if depth caching was just turned on
def rebuild_depth_cache!
- raise Ancestry::AncestryException.new("Cannot rebuild depth cache for model without depth caching.") unless respond_to? :depth_cache_column
+ raise Ancestry::AncestryException.new(I18n.t("ancestry.cannot_rebuild_depth_cache")) unless respond_to? :depth_cache_column
self.base_class.unscoped do
self.base_class.find_each do |node|
@@ -1,10 +1,10 @@
class << ActiveRecord::Base
def has_ancestry options = {}
# Check options
- raise Ancestry::AncestryException.new("Options for has_ancestry must be in a hash.") unless options.is_a? Hash
+ raise Ancestry::AncestryException.new(I18n.t("ancestry.option_must_be_hash")) unless options.is_a? Hash
options.each do |key, value|
unless [:ancestry_column, :orphan_strategy, :cache_depth, :depth_cache_column].include? key
- raise Ancestry::AncestryException.new("Unknown option for has_ancestry: #{key.inspect} => #{value.inspect}.")
+ raise Ancestry::AncestryException.new(I18n.t("ancestry.unknown_option", {:key => key.inspect, :value => value.inspect}))
end
end
@@ -64,7 +64,10 @@ def has_ancestry options = {}
# Create named scopes for depth
{:before_depth => '<', :to_depth => '<=', :at_depth => '=', :from_depth => '>=', :after_depth => '>'}.each do |scope_name, operator|
scope scope_name, lambda { |depth|
- raise Ancestry::AncestryException.new("Named scope '#{scope_name}' is only available when depth caching is enabled.") unless options[:cache_depth]
+ raise Ancestry::AncestryException.new(I18n.t("ancestry.named_scope_depth_cache",
+ {
+ :scope_name => scope_name
+ })) unless options[:cache_depth]
{:conditions => ["#{depth_cache_column} #{operator} ?", depth]}
}
end
@@ -2,7 +2,7 @@ module Ancestry
module InstanceMethods
# Validate that the ancestors don't include itself
def ancestry_exclude_self
- errors.add(:base, "#{self.class.name.humanize} cannot be a descendant of itself.") if ancestor_ids.include? self.id
+ errors.add(:base, I18n.t("ancestry.exclude_self", {:class_name => self.class.name.humanize})) if ancestor_ids.include? self.id
end
# Update descendants with new ancestry
@@ -58,7 +58,7 @@ def apply_orphan_strategy
end
# ... throw an exception if it has children and orphan strategy is restrict
elsif self.base_class.orphan_strategy == :restrict
- raise Ancestry::AncestryException.new('Cannot delete record because it has descendants.') unless is_childless?
+ raise Ancestry::AncestryException.new(I18n.t("ancestry.cannot_delete_descendants")) unless is_childless?
end
end
end
@@ -67,7 +67,7 @@ def apply_orphan_strategy
# The ancestry value for this record's children
def child_ancestry
# New records cannot have children
- raise Ancestry::AncestryException.new('No child ancestry for new record. Save record before performing tree operations.') if new_record?
+ raise Ancestry::AncestryException.new(I18n.t("ancestry.no_child_for_new_record")) if new_record?
if self.send("#{self.base_class.ancestry_column}_was").blank? then id.to_s else "#{self.send "#{self.base_class.ancestry_column}_was"}/#{id}" end
end
@@ -0,0 +1,16 @@
+en:
+ ancestry:
+ unkown_depth_option: "Unknown depth option: %{scope_name}."
+ invalid_orphan_strategy: "Invalid orphan strategy, valid ones are :rootify, :adopt, :restrict and :destroy."
+ invalid_ancestry_column: "Invalid format for ancestry column of node %{node_id}: %{ancestry_column}."
+ reference_nonexistent_node: "Reference to nonexistent node in node %{node_id}: %{ancestor_id}."
+ conflicting_parent_id: "Conflicting parent id found in node %{node_id}: %{parent_id} for node %{node_id} while expecting %{expected}"
+ cannot_rebuild_depth_cache: "Cannot rebuild depth cache for model without depth caching."
+
+ option_must_be_hash: "Options for has_ancestry must be in a hash."
+ unknown_option: "Unknown option for has_ancestry: %{key} => %{value}."
+ named_scope_depth_cache: "Named scope '%{scope_name}' is only available when depth caching is enabled."
+
+ exclude_self: "%{class_name} cannot be a descendant of itself."
+ cannot_delete_descendants: "Cannot delete record because it has descendants."
+ no_child_for_new_record: "No child ancestry for new record. Save record before performing tree operations."

1 comment on commit ed69043

@denispeplin

This comment has been minimized.

Show comment Hide comment
@denispeplin

denispeplin Oct 24, 2012

+1, need I18n too.

+1, need I18n too.

Please sign in to comment.