i18n-complements 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/i18n/complements/localize_extension.rb +1 -1
- data/lib/i18n/complements/numisma.rb +33 -6
- data/lib/i18n/complements/numisma/fixed_currency_rates.yml +20 -0
- data/lib/i18n/complements/version.rb +1 -1
- data/test/test_currencies.rb +4 -4
- data/test/test_currency_rates.rb +7 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7dda6ac868f33ce669414e1ccd72b713018a1540
|
4
|
+
data.tar.gz: 5dfb9a290c3b542c2d633b9e41f97e63a90a25ca
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 02421e944a7a2d5c1720b1f444b5fe42bd85befc811b2f089248e460bb9e9b08f2e3b83df9479a95651efb593d120ae22222c9e6deec58b3997e05802c2cb097
|
7
|
+
data.tar.gz: 3855cdc267a8cdfe9a6763b103e8f26cfdd733ab93e54f4df759e6bedfc29f143154ac23d15c8b793f7c3b57db9a2e204ab75d07fb2dd97b220bb1b032f68bb0
|
@@ -7,7 +7,7 @@ module I18n
|
|
7
7
|
return localize_without_complements(locale, object, format, options)
|
8
8
|
elsif object.respond_to?(:abs)
|
9
9
|
if currency = options[:currency]
|
10
|
-
raise I18n::InvalidCurrency
|
10
|
+
raise I18n::InvalidCurrency, "Currency #{currency} does not exist." unless I18n::Complements::Numisma[currency.to_s]
|
11
11
|
return I18n::Complements::Numisma[currency.to_s].localize(object, locale: locale)
|
12
12
|
else
|
13
13
|
formatter = I18n.translate('number.format'.to_sym, locale: locale, default: {})
|
@@ -22,7 +22,7 @@ module I18n
|
|
22
22
|
# Returns a hash with active currencies only
|
23
23
|
def active_currencies
|
24
24
|
x = {}
|
25
|
-
|
25
|
+
@@currencies.each do |code, currency|
|
26
26
|
x[code] = currency if currency.active
|
27
27
|
end
|
28
28
|
x
|
@@ -33,10 +33,21 @@ module I18n
|
|
33
33
|
@@currencies[currency_code]
|
34
34
|
end
|
35
35
|
|
36
|
+
def fixed_currency_rate(from, to)
|
37
|
+
rates = @@fixed_currency_rates[from]
|
38
|
+
return nil unless rates
|
39
|
+
rates[to]
|
40
|
+
end
|
41
|
+
|
36
42
|
def currency_rate(from, to)
|
37
|
-
|
38
|
-
|
39
|
-
|
43
|
+
if Numisma[from].nil?
|
44
|
+
raise InvalidCurrency, ":from currency is unknown (#{from.class}:#{from.inspect})"
|
45
|
+
end
|
46
|
+
if Numisma[to].nil?
|
47
|
+
raise InvalidCurrency, ":to currency is unknown (#{to.class}:#{to.inspect})"
|
48
|
+
end
|
49
|
+
rate = fixed_currency_rate(from, to)
|
50
|
+
return rate if rate
|
40
51
|
begin
|
41
52
|
uri = URI('http://www.webservicex.net/CurrencyConvertor.asmx/ConversionRate')
|
42
53
|
response = Net::HTTP.post_form(uri, 'FromCurrency' => from, 'ToCurrency' => to)
|
@@ -53,15 +64,31 @@ module I18n
|
|
53
64
|
# Load currencies
|
54
65
|
def load_currencies
|
55
66
|
@@currencies = {}
|
56
|
-
|
57
|
-
|
67
|
+
yaml = YAML.load_file(currencies_file)
|
68
|
+
yaml.each do |code, attributes|
|
69
|
+
currency = Currency.new(code, attributes.inject({}) { |h, p| h[p[0].to_sym] = p[1]; h })
|
58
70
|
@@currencies[currency.code] = currency
|
59
71
|
end
|
60
72
|
end
|
73
|
+
|
74
|
+
# Load fixed currency rates with reverse rates too.
|
75
|
+
def load_fixed_currency_rates
|
76
|
+
@@fixed_currency_rates = {}
|
77
|
+
yaml = YAML.load_file(File.join(File.dirname(__FILE__), 'numisma', 'fixed_currency_rates.yml'))
|
78
|
+
yaml.each do |from, rates|
|
79
|
+
@@fixed_currency_rates[from] ||= {}
|
80
|
+
rates.each do |to, rate|
|
81
|
+
@@fixed_currency_rates[from][to] = rate
|
82
|
+
@@fixed_currency_rates[to] ||= {}
|
83
|
+
@@fixed_currency_rates[to][from] = 1 / rate
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
61
87
|
end
|
62
88
|
|
63
89
|
# Finally load all currencies
|
64
90
|
load_currencies
|
91
|
+
load_fixed_currency_rates
|
65
92
|
end
|
66
93
|
end
|
67
94
|
|
@@ -0,0 +1,20 @@
|
|
1
|
+
EUR:
|
2
|
+
EEK: 15.6466
|
3
|
+
SKK: 30.126
|
4
|
+
GRD: 340.75
|
5
|
+
PTE: 200.482
|
6
|
+
NLG: 2.20371
|
7
|
+
BEF: 40.3399
|
8
|
+
FRF: 6.55957
|
9
|
+
LUF: 40.3399
|
10
|
+
LVL: 0.702804
|
11
|
+
ITL: 1936.27
|
12
|
+
MTL: 0.4293
|
13
|
+
LTL: 3.4528
|
14
|
+
CYP: 0.585274
|
15
|
+
IEP: 0.787564
|
16
|
+
DEM: 1.95583
|
17
|
+
FIM: 5.94573
|
18
|
+
ESP: 166.386
|
19
|
+
ATS: 13.7603
|
20
|
+
SIT: 239.64
|
data/test/test_currencies.rb
CHANGED
@@ -44,12 +44,12 @@ class TestCurrencies < I18n::Complements::TestCase
|
|
44
44
|
|
45
45
|
def test_number_formatting_with_currency
|
46
46
|
number = 413_500
|
47
|
-
assert_equal
|
47
|
+
assert_equal '¥413,500', ::I18n.localize(number, locale: :eng, currency: 'JPY')
|
48
48
|
assert_equal "413\u{00A0}500\u{00A0}¥", ::I18n.localize(number, locale: :fra, currency: 'JPY')
|
49
|
-
assert_equal
|
50
|
-
assert_equal
|
49
|
+
assert_equal '413,500円', ::I18n.localize(number, locale: :jpn, currency: 'JPY')
|
50
|
+
assert_equal '€0.325,1', ::I18n.localize(0.3251, locale: :eng, currency: 'EUR')
|
51
51
|
assert_equal "0,325\u{00A0}1\u{00A0}F", ::I18n.localize(0.3251, locale: :fra, currency: 'FRF')
|
52
|
-
assert_equal
|
52
|
+
assert_equal '€123,852.325,1', ::I18n.localize(123_852.3251, locale: :eng, currency: 'EUR')
|
53
53
|
end
|
54
54
|
|
55
55
|
def test_number_formatting_with_currency_with_core_extensions
|
data/test/test_currency_rates.rb
CHANGED
@@ -3,6 +3,11 @@ require 'helper'
|
|
3
3
|
|
4
4
|
class TestCurrencies < I18n::Complements::TestCase
|
5
5
|
def test_conversion_rate
|
6
|
+
rate = I18n::Complements::Numisma.currency_rate('EUR', 'FRF')
|
7
|
+
assert !rate.zero?, 'Rate should not be zero'
|
8
|
+
|
9
|
+
assert rate == 6.55957
|
10
|
+
|
6
11
|
assert_raise I18n::InvalidCurrency do
|
7
12
|
I18n::Complements::Numisma.currency_rate(:EUR, 'JPY')
|
8
13
|
end
|
@@ -17,7 +22,8 @@ class TestCurrencies < I18n::Complements::TestCase
|
|
17
22
|
|
18
23
|
r1 = I18n::Complements::Numisma.currency_rate('EUR', 'JPY')
|
19
24
|
r2 = I18n::Complements::Numisma.currency_rate('JPY', 'EUR')
|
20
|
-
|
25
|
+
rounded_one = (r1 * r2).round(2)
|
26
|
+
assert rounded_one >= 0.99, "EUR -> JPY: #{r1}, JPY -> EUR: #{r2}, #{r1 * r2}, #{rounded_one}"
|
21
27
|
|
22
28
|
rate = I18n::Complements::Numisma.currency_rate('EUR', 'JPY')
|
23
29
|
assert !rate.nil?, 'Rate cannot be nil'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: i18n-complements
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brice Texier
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-06-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: i18n
|
@@ -97,6 +97,7 @@ files:
|
|
97
97
|
- lib/i18n/complements/numisma.rb
|
98
98
|
- lib/i18n/complements/numisma/currencies.yml
|
99
99
|
- lib/i18n/complements/numisma/currency.rb
|
100
|
+
- lib/i18n/complements/numisma/fixed_currency_rates.yml
|
100
101
|
- lib/i18n/complements/version.rb
|
101
102
|
- test/helper.rb
|
102
103
|
- test/locales/eng.yml
|