Class: LocaleHelper

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::NumberHelper, DateTimeMaps
Defined in:
lib/helpers/locale_helper.rb

Constant Summary

Constants included from DateTimeMaps

DateTimeMaps::DAY_NAME_MAP, DateTimeMaps::DEFAULT_DAY_REGEX, DateTimeMaps::DEFAULT_MONTH_REGEX, DateTimeMaps::LONG_DAY_REGEX, DateTimeMaps::LONG_MONTH_REGEX, DateTimeMaps::MONTH_NAME_MAP, DateTimeMaps::SHORT_DAY_REGEX, DateTimeMaps::SHORT_MONTH_REGEX, DateTimeMaps::SHORT_SIZE

Instance Method Summary collapse

Constructor Details

#initialize(locale, options = {}) ⇒ LocaleHelper

Returns a new instance of LocaleHelper.



12
13
14
15
16
17
18
19
20
21
22
23
24
# 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

Instance Method Details

#currency_precisionObject



144
145
146
# File 'lib/helpers/locale_helper.rb', line 144

def currency_precision
  @format_data['number']['currency']['format']['precision']
end

#day_name(english_name) ⇒ Object



42
43
44
# File 'lib/helpers/locale_helper.rb', line 42

def day_name(english_name)
  get_day_name(english_name,'day_names')
end

#default_date(date) ⇒ Object



30
31
32
# File 'lib/helpers/locale_helper.rb', line 30

def default_date(date)
  date_to_string(date, 'default')
end

#default_time(time) ⇒ Object



59
60
61
# File 'lib/helpers/locale_helper.rb', line 59

def default_time(time)
  time_to_string(time, 'default')
end

#get_hst_formatObject



156
157
158
# File 'lib/helpers/locale_helper.rb', line 156

def get_hst_format
  @format_data['time']['formats']['history']
end

#history_time(time) ⇒ Object



63
64
65
# 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']) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# 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) ⇒ Object



115
116
117
# File 'lib/helpers/locale_helper.rb', line 115

def lc_currency_number_only(number)
  lc_currency(number,'')
end

#lc_label(label, *args) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# 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) ⇒ Object



79
80
81
82
83
84
# 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) ⇒ Object



86
87
88
89
90
91
92
93
# 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

#loggerObject



26
27
28
# File 'lib/helpers/locale_helper.rb', line 26

def logger
  @logger
end

#long_date(date) ⇒ Object



34
35
36
# File 'lib/helpers/locale_helper.rb', line 34

def long_date(date)
  date_to_string(date, 'long')
end

#long_time(time) ⇒ Object



67
68
69
# File 'lib/helpers/locale_helper.rb', line 67

def long_time(time)
  time_to_string(time,'long')
end

#month_name(english_name) ⇒ Object



50
51
52
# File 'lib/helpers/locale_helper.rb', line 50

def month_name(english_name)
  get_month_name(english_name, 'month_names')
end

#number_delimiterObject



152
153
154
# File 'lib/helpers/locale_helper.rb', line 152

def number_delimiter
  @format_data['number']['format']['delimiter']
end

#number_precisionObject



140
141
142
# File 'lib/helpers/locale_helper.rb', line 140

def number_precision
  @format_data['number']['format']['precision']
end

#number_separatorObject



148
149
150
# File 'lib/helpers/locale_helper.rb', line 148

def number_separator
  @format_data['number']['format']['separator']
end

#only_time(time) ⇒ Object



75
76
77
# File 'lib/helpers/locale_helper.rb', line 75

def only_time(time)
  time_to_string(time, 'time')
end

#short_date(date) ⇒ Object



38
39
40
# File 'lib/helpers/locale_helper.rb', line 38

def short_date(date)
  date_to_string(date, 'short')
end

#short_day_name(english_name) ⇒ Object



46
47
48
# 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) ⇒ Object



54
55
56
# 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) ⇒ Object



71
72
73
# File 'lib/helpers/locale_helper.rb', line 71

def short_time(time)
  time_to_string(time, 'short')
end