Permalink
Browse files

Removed commented-out code in patches initializer and renamed I18n#ha…

…rdtranslate into something a bit more explicit.
  • Loading branch information...
Aquaj committed Oct 23, 2018
1 parent ebaa75d commit e996c427939fef7226450b75eba41e41aebd6d85
Showing with 4 additions and 188 deletions.
  1. +1 −185 config/initializers/10-patches.rb
  2. +3 −3 lib/clean/locales.rb
@@ -5,24 +5,6 @@
Encoding.default_external = Encoding::UTF_8
end
# class ActiveRecord::ConnectionAdapters::PostGISAdapter::MainAdapter
# def quote(value, column = nil)
# if RGeo::Feature::Geometry.check_type(value)
# "'#{ RGeo::WKRep::WKBGenerator.new(hex_format: true, type_format: :ewkb, emit_ewkb_srid: true).generate(value) }'::geometry"
# elsif value.is_a?(RGeo::Cartesian::BoundingBox)
# "'#{ value.min_x },#{ value.min_y },#{ value.max_x },#{ value.max_y }'::box"
# elsif column and [:point].include?(column.type) and value =~ /\A\(.+\)\z/
# "'#{value}'::#{column.type}"
# elsif column and [:point, :linestring, :geometry].include?(column.type)
# "ST_GeomFromEWKT('#{Charta.new_geometry(value).to_ewkt}')::#{column.type}"
# else
# super
# end
# end
# end
class ::String
def tl(*args)
::I18n.translate('labels.' + self, *args)
@@ -141,54 +123,20 @@ def tc(*args)
args[0] = 'models.' + model_name.singular + '.' + args[0].to_s
::I18n.translate(*args)
end
# def tg(*args)
# args[0] = 'labels.'+args[0].to_s
# ::I18n.translate(*args)
# end
end
module ContextualModelInstanceHelpers
def tc(*args)
args[0] = 'models.' + self.class.model_name.singular + '.' + args[0].to_s
::I18n.translate(*args)
end
# def tg(*args)
# args[0] = 'labels.'+args[0].to_s
# ::I18n.translate(*args)
# end
end
module ContextualHelpers
# def tc(*args)
# args[0] = contextual_scope+'.'+args[0].to_s
# for i in 1..args.size
# args[i] = ::I18n.localize(args[i]) if args[i].is_a? Date
# end if args.size > 1
# ::I18n.translate(*args)
# end
# def tg(*args)
# args[0] = 'general.'+args[0].to_s
# ::I18n.translate(*args)
# end
def tl(*args)
args[0] = 'labels.' + args[0].to_s
::I18n.translate(*args)
end
# alias :tc :tl
# alias :tg :tl
private
def contextual_scope
app_dirs = '(helpers|controllers|views|models)'
latest_app_file = caller.detect { |level| level =~ /.*\/app\/#{app_dirs}\/[^\.\.]/ }
return 'eval' unless latest_app_file
latest_app_file.split(/\/app\//)[1].split(/\./)[0].tr('/', '.').gsub(/(_controller$|_helper$|_observer$)/, '')
end
end
end
end
@@ -199,8 +147,6 @@ def contextual_scope
ActiveRecord::Base.send :include, Ekylibre::I18n::ContextualModelInstanceHelpers
ActionView::Base.send :include, Ekylibre::I18n::ContextualHelpers
# Rails 4.1.0.rc1 and StateMachine don't play nice
require 'state_machine/version'
unless StateMachine::VERSION == '1.2.0'
@@ -217,26 +163,6 @@ def around_validation(*args, &block)
end
module ::I18n
# def self.valid_locales
# return [:fra, :eng, :spa, :jpn, :arb]
# end
# def self.valid_locales
# return [:fra, :eng, :spa, :jpn, :arb] # , :spa, :jpn, :arb
# # FIXME Call to active_locales fails during migrate
# self.available_locales.select{|x| x.to_s.size == 3}
# end
# def self.active_locales
# @@active_locales ||= self.valid_locales
# @@active_locales
# end
# def self.active_locales=(array=[])
# @@active_locales ||= self.valid_locales
# @@active_locales = array unless array.empty?
# end
def self.locale_label(locale = nil)
locale ||= self.locale
"#{locale} (" + locale_name + ')'
@@ -248,118 +174,8 @@ def self.locale_name(locale = nil)
end
# Returns translation if found else nil
def self.hardtranslate(*args)
def self.translate_or_nil(*args)
result = translate(*args)
(result.to_s =~ /(translation\ missing|\(\(\()/ ? nil : result)
end
# module Backend
# module Base
# def localize_with_numbers(locale, object, format = :default, options = {})
# options.symbolize_keys!
# if object.respond_to?(:abs)
# if currency = options[:currency]
# return currency.to_currency.localize(object, :locale=>locale)
# else
# formatter = I18n.translate('number.format'.to_sym, :locale => locale, :default => {})
# if formatter.is_a?(Proc)
# return formatter[object]
# elsif formatter.is_a?(Hash)
# formatter = {:format => "%n", :separator=>'.', :delimiter=>'', :precision=>3}.merge(formatter).merge(options)
# format = formatter[:format]
# negative_format = formatter[:negative_format] || "-" + format
# if object.to_f < 0
# format = negative_format
# object = object.abs
# end
# value = object.to_s.split(/\./)
# integrals, decimals = value[0].to_s, value[1].to_s
# decimals = decimals.gsub(/0+$/, '').ljust(formatter[:precision], '0').reverse.split(/(?=\d{3})/).reverse.collect{|x| x.reverse}.join(formatter[:delimiter])
# value = integrals.gsub(/^0+[1-9]+/, '').gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{formatter[:delimiter]}")
# value += formatter[:separator] + decimals unless decimals.blank?
# return format.gsub(/%n/, value).gsub(/%s/, "\u{00A0}").html_safe
# end
# end
# elsif object.respond_to?(:strftime)
# return localize_without_numbers(locale, object, format, options)
# else
# raise ArgumentError, "Object must be a Numeric, Date, DateTime or Time object. #{object.inspect} given."
# end
# end
# alias_method_chain :localize, :numbers
# end
# end
end
module ActiveModel
class Errors
# # allow a proc as a user defined message
# def add(attribute, message = nil, options = {})
# message ||= :invalid
# raise options.inspect if options.frozen?
# message = generate_message(attribute, message, options) # if message.is_a?(Symbol)
# self[attribute] ||= []
# self[attribute] << message
# end
# def add(attribute, message = nil, options = {})
# message ||= :invalid
# if message.is_a?(Symbol)
# message = generate_message(attribute, message, options.except(*CALLBACKS_OPTIONS))
# elsif message.is_a?(Proc)
# message = message.call
# elsif !options.delete(:forced)
# raise ArgumentError.new("Symbol or Proc expected, #{message.inspect} received.")
# end
# self[attribute] << message
# end
# def add_from_record(record)
# record.errors.each do |attribute, message|
# self[attribute] ||= []
# self[attribute] << message
# end
# end
# # Returns all the full error messages in an array.
# #
# # class Company
# # validates_presence_of :name, :address, :email
# # validates_length_of :name, :in => 5..30
# # end
# #
# # company = Company.create(:address => '123 First St.')
# # company.errors.full_messages # =>
# # ["Name is too short (minimum is 5 characters)", "Name can't be blank", "Address can't be blank"]
# def full_messages(options = {})
# full_messages = []
# each do |attribute, messages|
# messages = Array.wrap(messages)
# full_messages += messages
# end
# full_messages
# end
end
end
# ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
# msg = instance.error_message
# error_class = 'invalid'
# if html_tag =~ /<(input|textarea|select)[^>]+class=/
# class_attribute = html_tag =~ /class=['"]/
# html_tag.insert(class_attribute + 7, "#{error_class} ")
# elsif html_tag =~ /<(input|textarea|select)/
# first_whitespace = html_tag =~ /\s/
# html_tag[first_whitespace] = " class=\"#{error_class}\" "
# end
# html_tag
# end
View
@@ -131,7 +131,7 @@ def clean_action!
next unless translateable_actions.any?
translation << ' ' + controller_path + ":\n"
translateable_actions.each do |action_name|
name = ::I18n.hardtranslate("actions.#{controller_path}.#{action_name}")
name = ::I18n.translate_or_nil("actions.#{controller_path}.#{action_name}")
to_translate += 1
untranslated += 1 if actions.include?(action_name) && name.blank?
translation << " #{missing_prompt if name.blank?}#{action_name}: " + Clean::Support.yaml_value(name.blank? ? Clean::Support.default_action_title(controller_path, action_name) : name, 3)
@@ -313,7 +313,7 @@ def clean_aggregators!
to_translate += 1
if (name = ref[:aggregator_parameters][param_name]) && name.present?
translation << " #{param_name}: " + Clean::Support.yaml_value(name) + "\n"
elsif name = I18n.hardtranslate("labels.#{param_name}") || I18n.hardtranslate("attributes.#{param_name}")
elsif name = I18n.translate_or_nil("labels.#{param_name}") || I18n.hardtranslate("attributes.#{param_name}")
to_translate -= 1
translation << " #~ #{param_name}: " + Clean::Support.yaml_value(name) + "\n"
else
@@ -337,7 +337,7 @@ def clean_aggregators!
elsif property_name.to_s.underscore != property_name.to_s
to_translate -= 1
translation << " #~ #{property_name}: " + Clean::Support.yaml_value(property_name.to_s.underscore.humanize) + "\n"
elsif name = I18n.hardtranslate("attributes.#{property_name}") || I18n.hardtranslate("labels.#{property_name}") || I18n.hardtranslate("activerecord.models.#{property_name}")
elsif name = I18n.translate_or_nil("attributes.#{property_name}") || I18n.hardtranslate("labels.#{property_name}") || I18n.hardtranslate("activerecord.models.#{property_name}")
to_translate -= 1
translation << " #~ #{property_name}: " + Clean::Support.yaml_value(name) + "\n"
else

0 comments on commit e996c42

Please sign in to comment.