i18n-date 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +29 -0
- data/Rakefile +2 -0
- data/bin/release.sh +14 -0
- data/i18n-date.gemspec +17 -0
- data/lib/i18n-date/version.rb +5 -0
- data/lib/i18n-date.rb +70 -0
- data/spec/date_spec.rb +22 -0
- metadata +59 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Gonzalo Testa
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# I18n::Date
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'i18n-date'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install i18n-date
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/bin/release.sh
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
gem_version=`ruby -e "require './lib/i18n-date/version.rb'; puts I18n::Date::VERSION;"`
|
2
|
+
|
3
|
+
echo "Building version: $gem_version"
|
4
|
+
|
5
|
+
gem build i18n-date.gemspec
|
6
|
+
gem push i18n-date-${gem_version}.gem
|
7
|
+
gem inabox i18n-date-${gem_version}.gem
|
8
|
+
|
9
|
+
rm i18n-date-${gem_version}.gem
|
10
|
+
|
11
|
+
#git pull origin develop
|
12
|
+
#git push origin develop
|
13
|
+
git tag -a -m "i18n-date-${gem_version}" i18n-version-${gem_version}
|
14
|
+
git push --tags
|
data/i18n-date.gemspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/i18n-date/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Gonzalo Testa"]
|
6
|
+
gem.email = ["gonzalotesta@despegar.com"]
|
7
|
+
gem.description = %q{This gems is a monkey patch to Date class, that allows to take any Spanish/Portuguese date into a valid ruby date, to simplify operations between them.}
|
8
|
+
gem.summary = %q{Gem to convert intenational dates into a english date}
|
9
|
+
gem.homepage = "https://rubygems.org/gems/i18n-date"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "i18n-date"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = '0.0.1'
|
17
|
+
end
|
data/lib/i18n-date.rb
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
#encoding: UTF-8
|
2
|
+
require 'date'
|
3
|
+
require 'pry'
|
4
|
+
require "i18n-date/version"
|
5
|
+
|
6
|
+
class Date
|
7
|
+
|
8
|
+
# return a date from a non-english date
|
9
|
+
def self.parse_international(string)
|
10
|
+
parse(month_to_english(string))
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
PREPOSITIONS = (%w/de/)
|
16
|
+
|
17
|
+
# remove each preposition defined
|
18
|
+
def self.remove_prepositions(string)
|
19
|
+
PREPOSITIONS.each do | p |
|
20
|
+
string.gsub!(p, '')
|
21
|
+
end
|
22
|
+
string.gsub(' ', ' ')
|
23
|
+
end
|
24
|
+
|
25
|
+
# generate a new hash used to translate month names
|
26
|
+
def self.make_month_hash(names)
|
27
|
+
names.inject({}) {|result, name| result[name] = MONTHNAMES[result.count+1] ; result }
|
28
|
+
end
|
29
|
+
|
30
|
+
# useless code, will be used for day names
|
31
|
+
def self.make_day_hash(names)
|
32
|
+
names.inject({}) {|result, name| result[name] = DAYNAMES[result.count] ; result }
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
MONTH_TRANSLATIONS = {}
|
37
|
+
|
38
|
+
MONTH_TRANSLATIONS.merge! make_month_hash(%w/enero febrero marzo abril mayo junio julio agosto septiembre octubre noviembre diciembre/) # Spanish
|
39
|
+
|
40
|
+
MONTH_TRANSLATIONS.merge! make_month_hash(%w/ene feb mar abr may jun jul ago sep oct nov dic/) # Spanish short form
|
41
|
+
|
42
|
+
MONTH_TRANSLATIONS.merge! make_month_hash(%w/janeiro fevereiro março abril maio junho julho agosto setembro outubro novembro dezembro/) # Portuguese
|
43
|
+
|
44
|
+
MONTH_TRANSLATIONS.merge! make_month_hash(%w/jan Fev Mar Abr Mai Jun Jul Ago Set Out Nov Dez/) # Portuguese short form
|
45
|
+
|
46
|
+
DAY_TRANSLATIONS = {}
|
47
|
+
|
48
|
+
DAY_TRANSLATIONS.merge! make_day_hash(%w/domingo lunes martes miércoles jueves viernes sábado/) #Spanish
|
49
|
+
|
50
|
+
DAY_TRANSLATIONS.merge! make_day_hash(%w/dom lun mar mié jue vie sáb/) #Spanish short form
|
51
|
+
|
52
|
+
DAY_TRANSLATIONS.merge! make_day_hash(%w/domingo segunda-feira terça-feira quarta-feira quinta-feira sexta-feira sábado/)
|
53
|
+
|
54
|
+
DAY_TRANSLATIONS.merge! make_day_hash(%w/dom seg ter qua qui sex sáb/) #Portuguese short form
|
55
|
+
|
56
|
+
def self.month_to_english(string)
|
57
|
+
string = remove_prepositions(string) # remove prepositions
|
58
|
+
matching_month = string.match(/[\w]+[^\d],\s(?<date>.*)$/)
|
59
|
+
# just for dates that have day name and then current date
|
60
|
+
string = matching_month[:date] if !matching_month.nil? && matching_month.size == 2
|
61
|
+
month_from = string[/[^\s\d,]+/i] # Get month name
|
62
|
+
# if we have a match
|
63
|
+
if month_from
|
64
|
+
month_to = MONTH_TRANSLATIONS[month_from.downcase] # search for translation
|
65
|
+
return string.sub(month_from, month_to.to_s) if month_to
|
66
|
+
end
|
67
|
+
|
68
|
+
string
|
69
|
+
end
|
70
|
+
end
|
data/spec/date_spec.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require './lib/i18n-date.rb'
|
2
|
+
|
3
|
+
describe Date do
|
4
|
+
|
5
|
+
RESULT_DATE = "22 June 2010"
|
6
|
+
|
7
|
+
it "should respond to parse_international" do
|
8
|
+
Date.should be_respond_to(:parse_international)
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should return a date correctly" do
|
12
|
+
Date.month_to_english("22 jun 2010").should be_eql(RESULT_DATE)
|
13
|
+
Date.month_to_english("22 de jun 2010").should be_eql(RESULT_DATE)
|
14
|
+
Date.month_to_english("22 de jun de 2010").should be_eql(RESULT_DATE)
|
15
|
+
Date.month_to_english("22 junio 2010").should be_eql(RESULT_DATE)
|
16
|
+
Date.month_to_english("22 de junio 2010").should be_eql(RESULT_DATE)
|
17
|
+
Date.month_to_english("22 de junio de 2010").should be_eql(RESULT_DATE)
|
18
|
+
Date.month_to_english("jun 22, 2010").should be_eql("June 22, 2010")
|
19
|
+
Date.month_to_english("Martes, 22 de junio de 2010").should be_eql(RESULT_DATE)
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: i18n-date
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Gonzalo Testa
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-11-06 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: This gems is a monkey patch to Date class, that allows to take any Spanish/Portuguese
|
15
|
+
date into a valid ruby date, to simplify operations between them.
|
16
|
+
email:
|
17
|
+
- gonzalotesta@despegar.com
|
18
|
+
executables:
|
19
|
+
- release.sh
|
20
|
+
extensions: []
|
21
|
+
extra_rdoc_files: []
|
22
|
+
files:
|
23
|
+
- .gitignore
|
24
|
+
- Gemfile
|
25
|
+
- LICENSE
|
26
|
+
- README.md
|
27
|
+
- Rakefile
|
28
|
+
- bin/release.sh
|
29
|
+
- i18n-date.gemspec
|
30
|
+
- lib/i18n-date.rb
|
31
|
+
- lib/i18n-date/version.rb
|
32
|
+
- spec/date_spec.rb
|
33
|
+
homepage: https://rubygems.org/gems/i18n-date
|
34
|
+
licenses: []
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options: []
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ! '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
46
|
+
none: false
|
47
|
+
requirements:
|
48
|
+
- - ! '>='
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '0'
|
51
|
+
requirements: []
|
52
|
+
rubyforge_project:
|
53
|
+
rubygems_version: 1.8.24
|
54
|
+
signing_key:
|
55
|
+
specification_version: 3
|
56
|
+
summary: Gem to convert intenational dates into a english date
|
57
|
+
test_files:
|
58
|
+
- spec/date_spec.rb
|
59
|
+
has_rdoc:
|