ruby_l10n 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/helpers/date_time_maps.rb +23 -0
- data/lib/helpers/locale_helper.rb +215 -0
- data/ruby_l10n.gemspec +3 -2
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c9a7f623be56a1bdf69e8e6bf25cb0291e371eac
|
4
|
+
data.tar.gz: 682713561f1cfe651730c2c57309de6b5a515e21
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e7a348165a48ecd5debf06a5d4d71f612787f027e21529d929f8191e2931e7e946cfd290fd00cf1d3cb11442f33b7fd624f5d310d4ffd8f9c76a5972c1cf350e
|
7
|
+
data.tar.gz: 49ac9b001facdc4a60e434165245f8705e5103e9c3ef7c18739bfe29a41944669211d3db1e0b619b75dc68a778796ab066b58341fa0951311d38c9661385e33e
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module DateTimeMaps
|
2
|
+
|
3
|
+
SHORT_SIZE = 3
|
4
|
+
|
5
|
+
DAY_NAME_MAP = {'Sun' => 0, 'Mon' => 1, 'Tue' => 2,
|
6
|
+
'Wed' => 3, 'Thu' => 4, 'Fri' => 5, 'Sat' => 6}
|
7
|
+
|
8
|
+
MONTH_NAME_MAP = {'Jan' => 0, 'Feb' => 1, 'Mar' => 2, 'Apr' => 3,
|
9
|
+
'May' => 4, 'Jun' => 5, 'Jul' => 6, 'Aug' => 7,
|
10
|
+
'Sep' => 8, 'Oct' => 9, 'Nov' => 10, 'Dec' => 11}
|
11
|
+
|
12
|
+
SHORT_DAY_REGEX = /(Sun)|(Mon)|(Tue)|(Wed)|(Thu)|(Fri)|(Sat)/
|
13
|
+
LONG_DAY_REGEX = /(Sunday)|(Monday)|(Tuesday)|(Wednesday)|(Thursday)|(Friday)|(Saturday)/
|
14
|
+
DEFAULT_DAY_REGEX = LONG_DAY_REGEX
|
15
|
+
|
16
|
+
LONG_MONTH_REGEX = /(January)|(February)|(March)|(April)|(May)|(June)|(July)|(August)|(September)|(October)|(November)|(December)/
|
17
|
+
SHORT_MONTH_REGEX = /(Jan)|(Feb)|(Mar)|(Apr)|(May)|(Jun)|(Jul)|(Aug)|(Sep)|(Oct)|(Nov)|(Dec)/
|
18
|
+
DEFAULT_MONTH_REGEX = LONG_MONTH_REGEX
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
|
23
|
+
|
@@ -0,0 +1,215 @@
|
|
1
|
+
require 'action_view'
|
2
|
+
require 'config_service'
|
3
|
+
require 'logging'
|
4
|
+
|
5
|
+
require_relative './date_time_maps'
|
6
|
+
|
7
|
+
class LocaleHelper
|
8
|
+
include ActionView::Helpers::NumberHelper
|
9
|
+
include DateTimeMaps
|
10
|
+
|
11
|
+
|
12
|
+
def initialize(locale, options = {})
|
13
|
+
@locale = locale
|
14
|
+
config = ConfigService.load_config("locales/#{locale}.yml")
|
15
|
+
@format_data = config[locale]
|
16
|
+
|
17
|
+
if options['logger']
|
18
|
+
@logger = options['logger']
|
19
|
+
else
|
20
|
+
@logger = ::Logging::Logger.new("locale_helper_#{@locale}_#{self.object_id}")
|
21
|
+
current_env = ENV['RACK_ENV'] || ENV['RAILS_ENV'] || options['env'] || 'development'
|
22
|
+
#raise File.expand_path("../../log/#{current_env}.log")
|
23
|
+
@logger.add_appenders(Logging.appenders.stdout, Logging.appenders.file(File.expand_path("./log/#{current_env}.log")))
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
def logger
|
29
|
+
@logger
|
30
|
+
end
|
31
|
+
|
32
|
+
def default_date(date)
|
33
|
+
date_to_string(date, 'default')
|
34
|
+
end
|
35
|
+
|
36
|
+
def long_date(date)
|
37
|
+
date_to_string(date, 'long')
|
38
|
+
end
|
39
|
+
|
40
|
+
def short_date(date)
|
41
|
+
date_to_string(date, 'short')
|
42
|
+
end
|
43
|
+
|
44
|
+
def day_name(english_name)
|
45
|
+
get_day_name(english_name,'day_names')
|
46
|
+
end
|
47
|
+
|
48
|
+
def short_day_name(english_name)
|
49
|
+
get_day_name(english_name,'abbr_day_names')
|
50
|
+
end
|
51
|
+
|
52
|
+
def month_name(english_name)
|
53
|
+
get_month_name(english_name, 'month_names')
|
54
|
+
end
|
55
|
+
|
56
|
+
def short_month_name(english_name)
|
57
|
+
get_month_name(english_name, 'abbr_month_names')
|
58
|
+
end
|
59
|
+
|
60
|
+
|
61
|
+
def default_time(time)
|
62
|
+
time_to_string(time, 'default')
|
63
|
+
end
|
64
|
+
|
65
|
+
def history_time(time)
|
66
|
+
time.strftime(@format_data['time']['formats']['history'])
|
67
|
+
end
|
68
|
+
|
69
|
+
def long_time(time)
|
70
|
+
time_to_string(time,'long')
|
71
|
+
end
|
72
|
+
|
73
|
+
def short_time(time)
|
74
|
+
time_to_string(time, 'short')
|
75
|
+
end
|
76
|
+
|
77
|
+
def only_time(time)
|
78
|
+
time_to_string(time, 'time')
|
79
|
+
end
|
80
|
+
|
81
|
+
def lc_number(number)
|
82
|
+
|
83
|
+
return '' if (number.nil? || number == '')
|
84
|
+
number_with_delimiter(number, number_delimiter, number_separator)
|
85
|
+
|
86
|
+
end
|
87
|
+
|
88
|
+
def lc_number_with_precision(number, precision = number_precision)
|
89
|
+
st = localize_number(number)
|
90
|
+
if st =~ Regexp.compile(number_separator)
|
91
|
+
return ($` + $& + $'[0,precision])
|
92
|
+
end
|
93
|
+
|
94
|
+
return st
|
95
|
+
end
|
96
|
+
|
97
|
+
def lc_currency(number, unit = @format_data['number']['currency']['format']['unit'])
|
98
|
+
return number if number.to_s =~ /Infinity/ or number.to_s == 'NaN'
|
99
|
+
precision = @format_data['number']['currency']['format']['precision']
|
100
|
+
separator = number_separator
|
101
|
+
result = number_to_currency(number,:precision => precision,
|
102
|
+
:unit => unit,
|
103
|
+
:format => @format_data['number']['currency']['format']['format'],
|
104
|
+
:separator => separator,
|
105
|
+
:delimiter => number_delimiter
|
106
|
+
)
|
107
|
+
#logger.error("separator --> #{separator} --- result --> #{result}")
|
108
|
+
int, dec = result.split(separator)
|
109
|
+
if (!dec.nil? && dec.length < precision)
|
110
|
+
(precision - dec.length).times {
|
111
|
+
result += '0'
|
112
|
+
}
|
113
|
+
end
|
114
|
+
result
|
115
|
+
end
|
116
|
+
|
117
|
+
def lc_currency_number_only(number)
|
118
|
+
lc_currency(number,'')
|
119
|
+
end
|
120
|
+
|
121
|
+
def lc_label(label, *args)
|
122
|
+
st = @format_data['labels'][label]
|
123
|
+
|
124
|
+
if st.nil?
|
125
|
+
logger.error("\n***** Warning: label #{label} is not found in the locale #{@locale}.\n\n")
|
126
|
+
# st = LOCALES[US_LOCALE].lc_label(label, args)
|
127
|
+
return "#{lc_label('label_not_found')} #{label}"
|
128
|
+
end
|
129
|
+
|
130
|
+
if (!args.empty?)
|
131
|
+
st2 = st.dup
|
132
|
+
st.scan(/\{\{(\d)+\}\}/) { |word|
|
133
|
+
st2.sub!(Regexp.compile("\\{\\{#{word[0]}\\}\\}"), args[word[0].to_i].to_s)
|
134
|
+
|
135
|
+
}
|
136
|
+
return st2
|
137
|
+
end
|
138
|
+
|
139
|
+
return st
|
140
|
+
end
|
141
|
+
|
142
|
+
|
143
|
+
def number_precision
|
144
|
+
@format_data['number']['format']['precision']
|
145
|
+
end
|
146
|
+
|
147
|
+
def currency_precision
|
148
|
+
@format_data['number']['currency']['format']['precision']
|
149
|
+
end
|
150
|
+
|
151
|
+
def number_separator
|
152
|
+
@format_data['number']['format']['separator']
|
153
|
+
end
|
154
|
+
|
155
|
+
def number_delimiter
|
156
|
+
@format_data['number']['format']['delimiter']
|
157
|
+
end
|
158
|
+
|
159
|
+
def get_hst_format
|
160
|
+
@format_data['time']['formats']['history']
|
161
|
+
end
|
162
|
+
|
163
|
+
private
|
164
|
+
|
165
|
+
def date_to_string(date, format)
|
166
|
+
return '' if date.nil?
|
167
|
+
if date.is_a? ActiveSupport::TimeWithZone
|
168
|
+
date = date.to_date
|
169
|
+
end
|
170
|
+
|
171
|
+
raise "Invalid type #{date} of class #{date.class}" unless [Date, Time, DateTime].include? date.class
|
172
|
+
result = date.strftime(@format_data['date']['formats'][format])
|
173
|
+
sub_month(result, format)
|
174
|
+
end
|
175
|
+
|
176
|
+
def time_to_string(time, format)
|
177
|
+
raise "Invalid type #{time} of class #{time.class}" unless [Time, DateTime].include? time.class
|
178
|
+
result = time.strftime(@format_data['time']['formats'][format])
|
179
|
+
return result if format == 'time'
|
180
|
+
result = sub_month(result, format)
|
181
|
+
sub_day(result, format)
|
182
|
+
end
|
183
|
+
|
184
|
+
def get_day_name(english_name, format)
|
185
|
+
english_name = "#{english_name[0,3]}" if (english_name.size >= SHORT_SIZE)
|
186
|
+
@format_data['date'][format][DAY_NAME_MAP[english_name]]
|
187
|
+
end
|
188
|
+
|
189
|
+
def get_month_name(english_name, format)
|
190
|
+
if (english_name.size >= SHORT_SIZE )
|
191
|
+
english_name = "#{english_name[0,3]}"
|
192
|
+
end
|
193
|
+
@format_data['date'][format][MONTH_NAME_MAP[english_name]]
|
194
|
+
end
|
195
|
+
|
196
|
+
def sub_month(str_date, format)
|
197
|
+
idx = (str_date =~ eval("#{format.upcase}_MONTH_REGEX"))
|
198
|
+
return str_date unless idx
|
199
|
+
english_name = $&
|
200
|
+
month = (format == 'short')? short_month_name(english_name) :
|
201
|
+
month_name(english_name)
|
202
|
+
$` + month + $'
|
203
|
+
end
|
204
|
+
|
205
|
+
def sub_day(str_time, format)
|
206
|
+
idx = (str_time =~ eval("#{format.upcase}_DAY_REGEX"))
|
207
|
+
return str_time unless idx
|
208
|
+
|
209
|
+
english_name = $&
|
210
|
+
day = (format == 'short')? short_day_name(english_name) :
|
211
|
+
day_name(english_name)
|
212
|
+
$` + day + $'
|
213
|
+
end
|
214
|
+
|
215
|
+
end
|
data/ruby_l10n.gemspec
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'ruby_l10n'
|
3
|
-
s.version = '0.0.
|
3
|
+
s.version = '0.0.3'
|
4
4
|
s.summary = "A gem that provide L10n functionalities"
|
5
5
|
s.description = "A gem that provide L10n functionalities"
|
6
6
|
s.authors = ['Linh Chau']
|
7
7
|
s.email = 'chauhonglinh@gmail.com'
|
8
8
|
s.files = [
|
9
|
-
'./ruby_l10n.gemspec', 'lib/ruby_l10n.rb',
|
9
|
+
'./ruby_l10n.gemspec', 'lib/ruby_l10n.rb',
|
10
|
+
'lib/helpers/locale_helper.rb', 'lib/helpers/date_time_maps.rb'
|
10
11
|
]
|
11
12
|
s.homepage = 'https://github.com/linhchauatl/ruby_l10n'
|
12
13
|
s.license = 'MIT'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby_l10n
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Linh Chau
|
@@ -87,6 +87,8 @@ extensions: []
|
|
87
87
|
extra_rdoc_files: []
|
88
88
|
files:
|
89
89
|
- "./ruby_l10n.gemspec"
|
90
|
+
- lib/helpers/date_time_maps.rb
|
91
|
+
- lib/helpers/locale_helper.rb
|
90
92
|
- lib/ruby_l10n.rb
|
91
93
|
homepage: https://github.com/linhchauatl/ruby_l10n
|
92
94
|
licenses:
|