class LocaleHelper

Public Class Methods

new(locale, options = {}) click to toggle source
# File lib/helpers/locale_helper.rb, line 12
def initialize(locale, options = {})
  @locale = locale
  @format_data = ConfigService.load_config("locales/#{locale}.yml")

  if options['logger']
    @logger = options['logger']
  else
    @logger = ::Logging::Logger.new("locale_helper_#{@locale}_#{self.object_id}")
    current_env = ENV['RACK_ENV'] || ENV['RAILS_ENV'] || options['env'] || 'development'
    @logger.add_appenders(Logging.appenders.stdout, Logging.appenders.file(File.expand_path("./log/#{current_env}.log")))
  end

end

Public Instance Methods

currency_precision() click to toggle source
# File lib/helpers/locale_helper.rb, line 144
def currency_precision
  @format_data['number']['currency']['format']['precision']
end
day_name(english_name) click to toggle source
# File lib/helpers/locale_helper.rb, line 42
def day_name(english_name)
  get_day_name(english_name,'day_names')
end
default_date(date) click to toggle source
# File lib/helpers/locale_helper.rb, line 30
def default_date(date)
  date_to_string(date, 'default')
end
default_time(time) click to toggle source
# File lib/helpers/locale_helper.rb, line 59
def default_time(time)
  time_to_string(time, 'default')
end
get_hst_format() click to toggle source
# File lib/helpers/locale_helper.rb, line 156
def get_hst_format
  @format_data['time']['formats']['history']
end
history_time(time) click to toggle source
# File lib/helpers/locale_helper.rb, line 63
def history_time(time)
  time.strftime(@format_data['time']['formats']['history'])
end
lc_currency(number, unit = @format_data['number']['currency']['format']['unit']) click to toggle source
# File lib/helpers/locale_helper.rb, line 95
def lc_currency(number, unit = @format_data['number']['currency']['format']['unit'])
   return number if number.to_s =~ /Infinity/ or number.to_s == 'NaN'
precision = @format_data['number']['currency']['format']['precision']
  separator = number_separator
  result = number_to_currency(number,:precision => precision,
                             :unit => unit,
                             :format => @format_data['number']['currency']['format']['format'],
                             :separator => separator,
                             :delimiter => number_delimiter
                            )
  #logger.error("separator --> #{separator} --- result --> #{result}")
  int, dec = result.split(separator)
  if (!dec.nil? && dec.length < precision)
    (precision - dec.length).times {
      result += '0'
    }
  end
  result
end
lc_currency_number_only(number) click to toggle source
# File lib/helpers/locale_helper.rb, line 115
def lc_currency_number_only(number)
  lc_currency(number,'')
end
lc_label(label, *args) click to toggle source
# File lib/helpers/locale_helper.rb, line 119
def lc_label(label, *args)
  st = @format_data['labels'][label]

  if st.nil?
    logger.error("\n***** Warning: label #{label} is not found in the locale #{@locale}.\n\n")
    return "#{lc_label('label_not_found')} #{label}"
  end

  if (!args.empty?)
    st2 = st.dup
    st.scan(/\{\{(\d)+\}\}/) { |word|
      st2.sub!(Regexp.compile("\\{\\{#{word[0]}\\}\\}"), args[word[0].to_i].to_s)

    }
    return st2
  end

  return st
end
lc_number(number) click to toggle source
# File lib/helpers/locale_helper.rb, line 79
def lc_number(number)

  return '' if (number.nil? || number == '')
  number_with_delimiter(number, number_delimiter, number_separator)

end
lc_number_with_precision(number, precision = number_precision) click to toggle source
# File lib/helpers/locale_helper.rb, line 86
def lc_number_with_precision(number, precision = number_precision)
  st = localize_number(number)
  if st =~  Regexp.compile(number_separator)
    return ($` + $& + $'[0,precision])
  end

  return st
end
logger() click to toggle source
# File lib/helpers/locale_helper.rb, line 26
def logger
  @logger
end
long_date(date) click to toggle source
# File lib/helpers/locale_helper.rb, line 34
def long_date(date)
  date_to_string(date, 'long')
end
long_time(time) click to toggle source
# File lib/helpers/locale_helper.rb, line 67
def long_time(time)
  time_to_string(time,'long')
end
month_name(english_name) click to toggle source
# File lib/helpers/locale_helper.rb, line 50
def month_name(english_name)
  get_month_name(english_name, 'month_names')
end
number_delimiter() click to toggle source
# File lib/helpers/locale_helper.rb, line 152
def number_delimiter
  @format_data['number']['format']['delimiter']
end
number_precision() click to toggle source
# File lib/helpers/locale_helper.rb, line 140
def number_precision
  @format_data['number']['format']['precision']
end
number_separator() click to toggle source
# File lib/helpers/locale_helper.rb, line 148
def number_separator
  @format_data['number']['format']['separator']
end
only_time(time) click to toggle source
# File lib/helpers/locale_helper.rb, line 75
def only_time(time)
  time_to_string(time, 'time')
end
short_date(date) click to toggle source
# File lib/helpers/locale_helper.rb, line 38
def short_date(date)
  date_to_string(date, 'short')
end
short_day_name(english_name) click to toggle source
# File lib/helpers/locale_helper.rb, line 46
def short_day_name(english_name)
  get_day_name(english_name,'abbr_day_names')
end
short_month_name(english_name) click to toggle source
# File lib/helpers/locale_helper.rb, line 54
def short_month_name(english_name)
  get_month_name(english_name, 'abbr_month_names')
end
short_time(time) click to toggle source
# File lib/helpers/locale_helper.rb, line 71
def short_time(time)
  time_to_string(time, 'short')
end

Private Instance Methods

date_to_string(date, format) click to toggle source
# File lib/helpers/locale_helper.rb, line 162
def date_to_string(date, format)
  return '' if date.nil?
if date.is_a? ActiveSupport::TimeWithZone
    date = date.to_date
  end

  raise "Invalid type #{date} of class #{date.class}" unless [Date, Time, DateTime].include? date.class
  result = date.strftime(@format_data['date']['formats'][format])
  sub_month(result, format)
end
get_day_name(english_name, format) click to toggle source
# File lib/helpers/locale_helper.rb, line 181
def get_day_name(english_name, format)
  english_name = "#{english_name[0,3]}" if (english_name.size >= SHORT_SIZE)
  @format_data['date'][format][DAY_NAME_MAP[english_name]]
end
get_month_name(english_name, format) click to toggle source
# File lib/helpers/locale_helper.rb, line 186
def get_month_name(english_name, format)
  if (english_name.size >= SHORT_SIZE )
    english_name = "#{english_name[0,3]}"
  end
  @format_data['date'][format][MONTH_NAME_MAP[english_name]]
end
sub_day(str_time, format) click to toggle source
# File lib/helpers/locale_helper.rb, line 202
def sub_day(str_time, format)
  idx = (str_time =~ eval("#{format.upcase}_DAY_REGEX"))
  return str_time unless idx

  english_name = $&
  day = (format == 'short')? short_day_name(english_name) :
                               day_name(english_name)
  $` + day + $'
end
sub_month(str_date, format) click to toggle source
# File lib/helpers/locale_helper.rb, line 193
def sub_month(str_date, format)
  idx = (str_date =~ eval("#{format.upcase}_MONTH_REGEX"))
  return str_date unless idx
  english_name = $&
  month = (format == 'short')? short_month_name(english_name) :
                               month_name(english_name)
  $` + month + $'
end
time_to_string(time, format) click to toggle source
# File lib/helpers/locale_helper.rb, line 173
def time_to_string(time, format)
  raise "Invalid type #{time} of class #{time.class}" unless [Time, DateTime].include? time.class
  result = time.strftime(@format_data['time']['formats'][format])
  return result if format == 'time'
  result = sub_month(result, format)
  sub_day(result, format)
end