Permalink
Browse files

Adapting currency data from the currencies gem to supplement the CLDR…

… data
  • Loading branch information...
1 parent dbf4535 commit ee8d5b2f023cfc811699be78af17821a162453ac Cameron Dutro committed Dec 12, 2012
View
30 NOTICE
@@ -151,4 +151,32 @@ OR PERFORMANCE OF THIS SOFTWARE.
Except as contained in this notice, the name of a copyright holder shall not be
used in advertising or otherwise to promote the sale, use or other dealings in
-this Software without prior written authorization of the copyright holder.
+this Software without prior written authorization of the copyright holder.
+
+
+
+
+
+The currencies data in twitter-cldr-rb has been adapted from the currencies gem.
+Here's the license that accompanied that code:
+
+Copyright (c) 2009 hexorx
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
@@ -42,7 +42,6 @@ TwitterCldr patches core Ruby objects like `Fixnum` and `Date` to make localizat
# currencies, default USD
1337.localize(:es).to_currency.to_s # 1.337,00 $
1337.localize(:es).to_currency.to_s(:currency => "EUR") # 1.337,00 €
-1337.localize(:es).to_currency.to_s(:currency => "Peru") # 1.337,00 S/.
# percentages
1337.localize(:es).to_percent.to_s # 1.337%
@@ -63,20 +62,14 @@ num.to_currency.to_s # ...etc
#### More on Currencies
-If you're looking for a list of supported countries and currencies, use the `TwitterCldr::Shared::Currencies` class:
+If you're looking for a list of supported currencies, use the `TwitterCldr::Shared::Currencies` class:
```ruby
-# all supported countries
-TwitterCldr::Shared::Currencies.countries # ["Lithuania", "Philippines", ... ]
-
# all supported currency codes
TwitterCldr::Shared::Currencies.currency_codes # ["LTL", "PHP" ... ]
-# data for a specific country
-TwitterCldr::Shared::Currencies.for_country("Canada") # { :currency => "Dollar", :symbol => "$", :code => "CAD" }
-
# data for a specific currency code
-TwitterCldr::Shared::Currencies.for_code("CAD") # { :currency => "Dollar", :symbol => "$", :country => "Canada"}
+TwitterCldr::Shared::Currencies.for_code("CAD") # { :currency => "Canadian dollar", :symbol => "$", :cldr_symbol => "CA$", :country => "Canada" }
```
#### Short / Long Decimals
@@ -12,17 +12,26 @@ class CurrencyFormatter < NumberFormatter
def format(number, options = {})
options[:currency] ||= "USD"
currency = TwitterCldr::Shared::Currencies.for_code(options[:currency])
- currency ||= TwitterCldr::Shared::Currencies.for_country(options[:currency])
- currency ||= { :symbol => options[:currency], :currency => options[:currency] }
+ currency ||= {
+ :currency => options[:currency],
+ :symbol => options[:currency],
+ :cldr_symbol => options[:currency]
+ }
+
+ # overwrite with explicit symbol if given
+ currency[:symbol] = options[:symbol] if options[:symbol]
digits_and_rounding = resource(options[:currency])
options[:precision] ||= digits_and_rounding[:digits]
options[:rounding] ||= digits_and_rounding[:rounding]
- super(number, options).gsub('¤', (currency[:symbol] || currency[:currency].to_s))
+ symbol = options[:use_cldr_symbol] ? currency[:cldr_symbol] : currency[:symbol]
+ symbol ||= currency[:currency].to_s
+ super(number, options).gsub('¤', symbol)
end
private
+
def resource(code)
@resource ||= TwitterCldr.get_resource(:shared, :currency_digits_and_rounding)
@resource[code.to_sym] || @resource[:DEFAULT]
@@ -7,32 +7,34 @@ module TwitterCldr
module Shared
module Currencies
class << self
- def countries
- Kernel.warn("Currencies#countries will be deprecated. Please use Currencies#for_code.")
- resource_countries.keys.map(&:to_s)
- end
-
def currency_codes(locale = :en)
resource(locale).keys.map { |c| c.to_s }
end
- def for_country(country_name, locale = :en)
- Kernel.warn("Currencies#for_country will be deprecated. Please use Currencies#for_code.")
- for_code(resource_countries[country_name.to_sym][:code], locale) if resource_countries[country_name.to_sym]
- end
-
def for_code(currency_code, locale = :en)
currency_code = currency_code.to_sym
data = resource(locale)[currency_code]
- { :currency => currency_code,
- :name => data[:one],
- :symbol => data[:symbol] } if data
+ symbol_data = iso_currency_symbols[currency_code]
+
+ if data
+ result = {
+ :currency => currency_code,
+ :name => data[:one],
+ :cldr_symbol => data[:symbol],
+ :symbol => data[:symbol]
+ }
+
+ result.merge!(symbol_data) if symbol_data
+ end
+
+ result
end
private
- def resource_countries
- @resource_countries ||= TwitterCldr.get_resource(:shared, :currencies)
+ # ISO 4217 to be precise
+ def iso_currency_symbols
+ @iso_currency_symbols ||= TwitterCldr.get_resource(:shared, :iso_currency_symbols)
end
def resource(locale)
Oops, something went wrong.

0 comments on commit ee8d5b2

Please sign in to comment.