i18n-complements 0.0.1 → 0.0.2

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/README.rdoc ADDED
@@ -0,0 +1,60 @@
1
+ = I18nComplements
2
+
3
+ I18n complements provides:
4
+ * Numbers localization
5
+ * Moneys localization (through ISO4217 codes)
6
+
7
+ == Localize numbers
8
+
9
+ I18n.localize(2015.2, :locale => :eng) # => "2,015.200"
10
+ I18n.localize(2015.2, :locale => :fra) # => "2 015,200"
11
+
12
+ With extensions:
13
+
14
+ 2015.2.l(:locale => :eng) # => "2,015.200"
15
+ 2015.2.l(:locale => :fra) # => "2 015,200"
16
+
17
+
18
+ == Localize money
19
+
20
+ I18n.localize(13500, :currency => :JPY, :locale => :eng) # => "¥13,500"
21
+ I18n.localize(13500, :currency => :JPY, :locale => :fra) # => "13 500 ¥"
22
+ I18n.localize(13500, :currency => :JPY, :locale => :jpn) # => "13,500円"
23
+
24
+ I18n.localize(13500, :currency => :EUR, :locale => :eng) # => "€13,500.00"
25
+ I18n.localize(13500, :currency => :EUR, :locale => :fra) # => "13 500.00 €"
26
+
27
+ With extensions:
28
+ 13500.l(:currency => :JPY, :locale => :jpn) # => "13,500円"
29
+
30
+ == Extensions
31
+
32
+ Moreover it extends String, and Symbol for translations:
33
+
34
+ I18n.t("my.example", options..)
35
+ # is equivalent to
36
+ "my.example".t(options...)
37
+
38
+ Or
39
+
40
+ I18n.t(:this_is_another_example, options..)
41
+ # is equivalent to
42
+ :this_is_another_example.t(options...)
43
+
44
+ And it extends Date, DateTime and Time for localization:
45
+
46
+ Date.civil(2012, 1, 1).l(:locale => :fra) # => "1 janvier 2012"
47
+
48
+ == Manipulating currencies
49
+
50
+ To find existing currencies:
51
+
52
+ I18n.active_currencies # returns the list of used currencies
53
+ I18n.available_currencies # returns all known currencies
54
+ I18n.currency(<currency_code>) # returns a Currency object with its properties
55
+
56
+ == To do
57
+
58
+ * Enhances numerals localization using bases (10, 20...) and characters
59
+ of the language. Ex: Roman (Ⅰ, Ⅱ, Ⅲ, Ⅳ...), traditionnal chinese/japanese
60
+ numbers (一, 二, 三, 四...)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.2
@@ -0,0 +1,55 @@
1
+ # TODO: Rewrite this "not DRY" code
2
+
3
+ # Translation
4
+
5
+ class ::String
6
+ def translate(options = {})
7
+ I18n.translate(self, options)
8
+ end
9
+ alias :t :translate
10
+
11
+ def to_currency
12
+ I18n.currencies(self)
13
+ end
14
+ end
15
+
16
+ class ::Symbol
17
+ def translate(options = {})
18
+ I18n.translate(self, options)
19
+ end
20
+ alias :t :translate
21
+
22
+ def to_currency
23
+ I18n.currencies(self.to_s)
24
+ end
25
+ end
26
+
27
+ # Localization
28
+
29
+ class ::Numeric
30
+ def localize(options = {})
31
+ I18n.localize(self, options)
32
+ end
33
+ alias :l :localize
34
+ end
35
+
36
+ class ::Date
37
+ def localize(options = {})
38
+ I18n.localize(self, options)
39
+ end
40
+ alias :l :localize
41
+ end
42
+
43
+ class ::DateTime
44
+ def localize(options = {})
45
+ I18n.localize(self, options)
46
+ end
47
+ alias :l :localize
48
+ end
49
+
50
+ class ::Time
51
+ def localize(options = {})
52
+ I18n.localize(self, options)
53
+ end
54
+ alias :l :localize
55
+ end
@@ -6,7 +6,7 @@ module I18n
6
6
  # options.symbolize_keys!
7
7
  if object.respond_to?(:abs)
8
8
  if currency = options[:currency]
9
- raise I18nComplements::InvalidCurrency.new("Currency #{currency.to_s} does not exist.") unless I18nComplements::Numisma[currency.to_s]
9
+ raise I18n::InvalidCurrency.new("Currency #{currency.to_s} does not exist.") unless I18nComplements::Numisma[currency.to_s]
10
10
  return I18nComplements::Numisma[currency.to_s].localize(object, :locale=>locale)
11
11
  else
12
12
  formatter = I18n.translate('number.format'.to_sym, :locale => locale, :default => {})
@@ -61,5 +61,3 @@ module I18n
61
61
  end
62
62
  end
63
63
  end
64
-
65
- # I18n::Backend::Base.send(:include, I18nComplements::Backend::Base)
@@ -5,7 +5,7 @@ module I18nComplements
5
5
  attr_reader :code, :active, :cash, :countries, :number, :precision, :unit
6
6
 
7
7
  def initialize(code, options = {})
8
- @code = code.strip.upcase
8
+ @code = code.strip # .upcase
9
9
  @active = (options[:active] ? true : false)
10
10
  @cash = options[:cash].to_a.collect{|x| x.to_f}.sort
11
11
  @countries = options[:countries].to_a.collect{|x| x.to_s}.sort.collect{|x| x.to_sym}
@@ -59,3 +59,24 @@ module I18nComplements
59
59
  load_currencies
60
60
  end
61
61
  end
62
+
63
+
64
+ module I18n
65
+
66
+ class << self
67
+
68
+ def currencies(currency_code)
69
+ I18nComplements::Numisma.currencies[currency_code.to_s]
70
+ end
71
+
72
+ def active_currencies
73
+ I18nComplements::Numisma.active_currencies
74
+ end
75
+
76
+ def available_currencies
77
+ I18nComplements::Numisma.currencies
78
+ end
79
+
80
+ end
81
+
82
+ end
@@ -1,10 +1,14 @@
1
1
  require 'i18n'
2
2
  require 'i18n-complements/numisma'
3
- require 'i18n-complements/localize_numbers'
3
+ require 'i18n-complements/localize_extension'
4
+ require 'i18n-complements/core_extension'
4
5
 
5
- module I18nComplements
6
+ module I18n
6
7
 
7
8
  class InvalidCurrency < ArgumentError
8
9
  end
9
10
 
10
11
  end
12
+
13
+ module I18nComplements
14
+ end
data/test/locales/eng.yml CHANGED
@@ -1,4 +1,10 @@
1
1
  eng:
2
+ date:
3
+ formats:
4
+ default: '%m %d, %Y'
5
+ time:
6
+ formats:
7
+ default: '%b. %d, %Y at %H:%M:%S'
2
8
  number:
3
9
  format:
4
10
  separator: '.'
@@ -8,3 +14,6 @@ eng:
8
14
  format:
9
15
  format: "%u%n"
10
16
  precision: 2
17
+ my:
18
+ example: My example is short
19
+ my_example: My example is short
@@ -1,12 +1,95 @@
1
+ # encoding: utf-8
1
2
  require 'helper'
2
3
 
3
4
  class TestCurrencies < Test::Unit::TestCase
4
5
 
5
- def test_listing_of_currencies
6
+ def test_localization_with_currency
7
+ assert_nothing_raised do
8
+ I18n.localize(14.5, :currency => :JPY)
9
+ end
10
+
11
+ assert_nothing_raised do
12
+ I18n.localize(14.5, :currency => "JPY")
13
+ end
14
+
15
+ assert_raise(I18n::InvalidCurrency) do
16
+ I18n.localize(14.5, :currency => "jPY")
17
+ end
6
18
  end
7
19
 
20
+ def test_localization_with_currency_with_core_extensions
21
+ assert_nothing_raised do
22
+ 14.5.localize(:currency => :JPY)
23
+ end
24
+
25
+ assert_nothing_raised do
26
+ 14.5.l(:currency => :JPY)
27
+ end
8
28
 
9
- def test_currency_rates
29
+ assert_nothing_raised do
30
+ 13546.localize(:currency => :JPY)
31
+ end
32
+
33
+ assert_nothing_raised do
34
+ 13546.l(:currency => :JPY)
35
+ end
36
+
37
+ assert_raise(I18n::InvalidCurrency) do
38
+ 14.5.l(:currency => "jPY")
39
+ end
40
+
41
+ assert_raise(I18n::InvalidCurrency) do
42
+ 14.5.l(:currency => "JPYXX")
43
+ end
44
+ end
45
+
46
+ def test_number_formatting_with_currency
47
+ number = 413500
48
+ assert_equal "¥413,500", I18n.localize(number, :locale => :eng, :currency=>"JPY")
49
+ assert_equal "413\u{00A0}500\u{00A0}¥", I18n.localize(number, :locale => :fra, :currency=>"JPY")
50
+ assert_equal "413,500円", I18n.localize(number, :locale => :jpn, :currency=>"JPY")
10
51
  end
11
52
 
53
+ def test_number_formatting_with_currency_with_core_extensions
54
+ for locale in [:eng, :fra, :jpn]
55
+ for money in [[413500, "JPY"], [1425.23, "USD"], [0.96, "EUR"]]
56
+ assert_equal(money[0].l(:locale => money[1]), I18n.localize(money[0], :locale => money[1]))
57
+ end
58
+ end
59
+ end
60
+
61
+
62
+ def test_listing_of_currencies
63
+ assert_nothing_raised do
64
+ I18n.active_currencies
65
+ end
66
+
67
+ assert_nothing_raised do
68
+ I18n.available_currencies
69
+ end
70
+
71
+ assert I18n.active_currencies.size <= I18n.available_currencies.size, "Available currencies cannot be less numerous than active currencies"
72
+ end
73
+
74
+ def test_currency_properties
75
+ assert !I18n.currencies(:JPY).nil?
76
+ assert !I18n.currencies("JPY").nil?
77
+ assert I18n.currencies("JPy").nil?
78
+
79
+ assert !I18n.currencies("USD").nil?
80
+ assert !I18n.currencies("EUR").nil?
81
+
82
+ assert I18n.currencies("JPYXX").nil?
83
+ end
84
+
85
+ def test_currency_properties_with_core_extensions
86
+ assert_nothing_raised do
87
+ :JPY.to_currency.name
88
+ end
89
+
90
+ assert_nothing_raised do
91
+ "USD".to_currency.name
92
+ end
93
+ end
94
+
12
95
  end
@@ -0,0 +1,69 @@
1
+ # encoding: utf-8
2
+ require 'helper'
3
+
4
+ class TestExtensions < Test::Unit::TestCase
5
+
6
+ def setup
7
+ I18n.locale = :eng
8
+ end
9
+
10
+ def test_string_translation
11
+ assert_equal "My example is short", I18n.translate("my_example")
12
+ assert_equal I18n.translate("my_example"), "my_example".translate
13
+ assert_equal I18n.translate("my_example"), "my_example".t
14
+
15
+ assert_equal "My example is short", I18n.translate("my.example")
16
+ assert_equal I18n.translate("my.example"), "my.example".translate
17
+ assert_equal I18n.translate("my.example"), "my.example".t
18
+ end
19
+
20
+ def test_symbol_translation
21
+ assert_equal "My example is short", I18n.translate(:my_example)
22
+ assert_equal I18n.translate(:my_example), :my_example.translate
23
+ assert_equal I18n.translate(:my_example), :my_example.t
24
+ end
25
+
26
+
27
+ def test_date_localization
28
+ date = Date.civil(1999, 12, 31)
29
+
30
+ assert_nothing_raised do
31
+ date.localize
32
+ end
33
+
34
+ assert_nothing_raised do
35
+ date.l
36
+ end
37
+
38
+ assert_equal I18n.localize(date), date.l
39
+ end
40
+
41
+ def test_datetime_localization
42
+ datetime = DateTime.civil(1999, 12, 31, 23, 59, 59)
43
+
44
+ assert_nothing_raised do
45
+ datetime.localize
46
+ end
47
+
48
+ assert_nothing_raised do
49
+ datetime.l
50
+ end
51
+
52
+ assert_equal I18n.localize(datetime), datetime.l
53
+ end
54
+
55
+ def test_time_localization
56
+ time = DateTime.civil(1999, 12, 31, 23, 59, 59).to_time
57
+
58
+ assert_nothing_raised do
59
+ time.localize
60
+ end
61
+
62
+ assert_nothing_raised do
63
+ time.l
64
+ end
65
+
66
+ assert_equal I18n.localize(time), time.l
67
+ end
68
+
69
+ end
data/test/test_numbers.rb CHANGED
@@ -7,6 +7,28 @@ class TestNumbers < Test::Unit::TestCase
7
7
  assert_nothing_raised do
8
8
  I18n.localize(14.5)
9
9
  end
10
+
11
+ assert_nothing_raised do
12
+ I18n.localize(413500)
13
+ end
14
+ end
15
+
16
+ def test_localization_with_core_extensions
17
+ assert_nothing_raised do
18
+ 14.5.localize
19
+ end
20
+
21
+ assert_nothing_raised do
22
+ 413500.localize
23
+ end
24
+
25
+ assert_nothing_raised do
26
+ 14.5.l
27
+ end
28
+
29
+ assert_nothing_raised do
30
+ 413500.l
31
+ end
10
32
  end
11
33
 
12
34
  def test_number_formatting
@@ -56,29 +78,13 @@ class TestNumbers < Test::Unit::TestCase
56
78
  assert_equal "62\u{00A0}951\u{00A0}413,141\u{00A0}592\u{00A0}6", I18n.localize(number, :locale => :fra)
57
79
  end
58
80
 
59
-
60
- def test_localization_with_currency
61
- I18n.localize(14.5, :currency => :JPY)
62
- assert_nothing_raised do
63
- I18n.localize(14.5, :currency => :JPY)
64
- end
65
-
66
- assert_nothing_raised do
67
- I18n.localize(14.5, :currency => "JPY")
68
- end
69
-
70
- assert_raise(I18nComplements::InvalidCurrency) do
71
- I18n.localize(14.5, :currency => "jPY")
81
+ def test_number_formatting_with_core_extensions
82
+ for locale in [:eng, :fra, :jpn]
83
+ for number in [3, 21145, 0.5, 3.0, 14.53, 2144.5, 0.41421356, 3.1415926, 62951413.1415926]
84
+ assert_equal(number.l(:locale => locale), I18n.localize(number, :locale => locale))
85
+ end
72
86
  end
73
- end
74
87
 
75
- def test_formatting_with_currency
76
- number = 413500
77
- assert_equal "¥413,500", I18n.localize(number, :locale => :eng, :currency=>"JPY")
78
- assert_equal "413\u{00A0}500\u{00A0}¥", I18n.localize(number, :locale => :fra, :currency=>"JPY")
79
- assert_equal "413,500円", I18n.localize(number, :locale => :jpn, :currency=>"JPY")
80
88
  end
81
89
 
82
-
83
-
84
90
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: i18n-complements
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-09 00:00:00.000000000Z
12
+ date: 2012-05-11 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: i18n
16
- requirement: &15085180 !ruby/object:Gem::Requirement
16
+ requirement: &9099980 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,31 +21,21 @@ dependencies:
21
21
  version: '0.6'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *15085180
25
- - !ruby/object:Gem::Dependency
26
- name: rake
27
- requirement: &15084120 !ruby/object:Gem::Requirement
28
- none: false
29
- requirements:
30
- - - ! '>='
31
- - !ruby/object:Gem::Version
32
- version: 0.8.7
33
- type: :development
34
- prerelease: false
35
- version_requirements: *15084120
24
+ version_requirements: *9099980
36
25
  description:
37
26
  email: burisu@oneiros.fr
38
27
  executables: []
39
28
  extensions: []
40
29
  extra_rdoc_files:
41
30
  - LICENSE
42
- - README
31
+ - README.rdoc
43
32
  files:
44
33
  - LICENSE
45
- - README
34
+ - README.rdoc
46
35
  - VERSION
47
36
  - lib/i18n-complements.rb
48
- - lib/i18n-complements/localize_numbers.rb
37
+ - lib/i18n-complements/core_extension.rb
38
+ - lib/i18n-complements/localize_extension.rb
49
39
  - lib/i18n-complements/numisma.rb
50
40
  - lib/i18n-complements/numisma/currencies.yml
51
41
  - lib/i18n-complements/numisma/currency.rb
@@ -54,6 +44,7 @@ files:
54
44
  - test/locales/fra.yml
55
45
  - test/locales/jpn.yml
56
46
  - test/test_currencies.rb
47
+ - test/test_extensions.rb
57
48
  - test/test_numbers.rb
58
49
  homepage: http://github.com/burisu/i18n-complements
59
50
  licenses:
@@ -82,5 +73,6 @@ specification_version: 3
82
73
  summary: I18n missing functionnalities
83
74
  test_files:
84
75
  - test/test_currencies.rb
76
+ - test/test_extensions.rb
85
77
  - test/test_numbers.rb
86
78
  has_rdoc:
data/README DELETED
@@ -1,15 +0,0 @@
1
- I18nComplements
2
- ===============
3
-
4
- I18n complements provides:
5
- * Numbers localization
6
- * Moneys localization (through ISO4217 codes)
7
-
8
- TODO
9
- ----
10
-
11
- * Add String/Symbol :t methods
12
- * Add Date/Datetime/Time/Number :l methods
13
- * Enhances number localization using bases (10, 20...) and characters
14
- of the language. Ex: Roman (Ⅰ, Ⅱ, Ⅲ, Ⅳ...), traditionnal chinese/japanese
15
- numbers (一, 二, 三, 四...)