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



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/helpers/locale_helper.rb', line 12

def initialize(locale, options = {})
  @locale = locale
  config = ConfigService.load_config("locales/#{locale}.yml")
  @format_data = config[locale]

  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'
    #raise File.expand_path("../../log/#{current_env}.log")
    @logger.add_appenders(Logging.appenders.stdout, Logging.appenders.file(File.expand_path("./log/#{current_env}.log")))
  end

end

Instance Method Details

#currency_precisionObject



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

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

#day_name(english_name) ⇒ Object



44
45
46
# File 'lib/helpers/locale_helper.rb', line 44

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

#default_date(date) ⇒ Object



32
33
34
# File 'lib/helpers/locale_helper.rb', line 32

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

#default_time(time) ⇒ Object



61
62
63
# File 'lib/helpers/locale_helper.rb', line 61

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

#get_hst_formatObject



159
160
161
# File 'lib/helpers/locale_helper.rb', line 159

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

#history_time(time) ⇒ Object



65
66
67
# File 'lib/helpers/locale_helper.rb', line 65

def history_time(time)
  time.strftime(@format_data['time']['formats']['history'])
end

#lc_currency(number, unit = @format_data['number']['currency']['format']['unit']) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/helpers/locale_helper.rb', line 97

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



117
118
119
# File 'lib/helpers/locale_helper.rb', line 117

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

#lc_label(label, *args) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/helpers/locale_helper.rb', line 121

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")
    # st = LOCALES[US_LOCALE].lc_label(label, args)
    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



81
82
83
84
85
86
# File 'lib/helpers/locale_helper.rb', line 81

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



88
89
90
91
92
93
94
95
# File 'lib/helpers/locale_helper.rb', line 88

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



28
29
30
# File 'lib/helpers/locale_helper.rb', line 28

def logger
  @logger
end

#long_date(date) ⇒ Object



36
37
38
# File 'lib/helpers/locale_helper.rb', line 36

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

#long_time(time) ⇒ Object



69
70
71
# File 'lib/helpers/locale_helper.rb', line 69

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

#month_name(english_name) ⇒ Object



52
53
54
# File 'lib/helpers/locale_helper.rb', line 52

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

#number_delimiterObject



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

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

#number_precisionObject



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

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

#number_separatorObject



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

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

#only_time(time) ⇒ Object



77
78
79
# File 'lib/helpers/locale_helper.rb', line 77

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

#short_date(date) ⇒ Object



40
41
42
# File 'lib/helpers/locale_helper.rb', line 40

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

#short_day_name(english_name) ⇒ Object



48
49
50
# File 'lib/helpers/locale_helper.rb', line 48

def short_day_name(english_name)
  get_day_name(english_name,'abbr_day_names')
end

#short_month_name(english_name) ⇒ Object



56
57
58
# File 'lib/helpers/locale_helper.rb', line 56

def short_month_name(english_name)
  get_month_name(english_name, 'abbr_month_names')
end

#short_time(time) ⇒ Object



73
74
75
# File 'lib/helpers/locale_helper.rb', line 73

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