Permalink
Browse files

Merging latest master

  • Loading branch information...
2 parents 1c1f8eb + 22a1910 commit f78beb0d5dcb5acb613f9ce92e211e336e704547 Cameron Dutro committed Nov 15, 2012
Showing with 30,261 additions and 94 deletions.
  1. +5 −0 Gemfile
  2. +35 −6 Rakefile
  3. +13 −14 lib/twitter_cldr/formatters/numbers/currency_formatter.rb
  4. +0 −4 lib/twitter_cldr/formatters/numbers/decimal_formatter.rb
  5. +14 −6 lib/twitter_cldr/formatters/numbers/number_formatter.rb
  6. +0 −4 lib/twitter_cldr/formatters/numbers/percent_formatter.rb
  7. +14 −12 lib/twitter_cldr/resources.rb
  8. +10 −6 lib/twitter_cldr/resources/locales_resources_importer.rb
  9. +20 −13 lib/twitter_cldr/shared/currencies.rb
  10. +351 −0 resources/locales/af/currencies.yml
  11. +1,463 −0 resources/locales/ar/currencies.yml
  12. +66 −0 resources/locales/be/currencies.yml
  13. +756 −0 resources/locales/bg/currencies.yml
  14. +561 −0 resources/locales/bn/currencies.yml
  15. +898 −0 resources/locales/ca/currencies.yml
  16. +1,053 −0 resources/locales/cs/currencies.yml
  17. +54 −0 resources/locales/cy/currencies.yml
  18. +810 −0 resources/locales/da/currencies.yml
  19. +835 −0 resources/locales/de/currencies.yml
  20. +863 −0 resources/locales/el/currencies.yml
  21. +905 −0 resources/locales/en/currencies.yml
  22. +817 −0 resources/locales/es/currencies.yml
  23. +336 −0 resources/locales/eu/currencies.yml
  24. +443 −0 resources/locales/fa/currencies.yml
  25. +900 −0 resources/locales/fi/currencies.yml
  26. +347 −0 resources/locales/fil/currencies.yml
  27. +903 −0 resources/locales/fr/currencies.yml
  28. +511 −0 resources/locales/ga/currencies.yml
  29. +467 −0 resources/locales/gl/currencies.yml
  30. +470 −0 resources/locales/he/currencies.yml
  31. +378 −0 resources/locales/hi/currencies.yml
  32. +637 −0 resources/locales/hu/currencies.yml
  33. +391 −0 resources/locales/id/currencies.yml
  34. +714 −0 resources/locales/it/currencies.yml
  35. +566 −0 resources/locales/ja/currencies.yml
  36. +562 −0 resources/locales/ko/currencies.yml
  37. +512 −0 resources/locales/lv/currencies.yml
  38. +335 −0 resources/locales/ms/currencies.yml
  39. +755 −0 resources/locales/nb/currencies.yml
  40. +583 −0 resources/locales/nl/currencies.yml
  41. +1,049 −0 resources/locales/pl/currencies.yml
  42. +901 −0 resources/locales/pt/currencies.yml
  43. +937 −0 resources/locales/ro/currencies.yml
  44. +1,044 −0 resources/locales/ru/currencies.yml
  45. +577 −0 resources/locales/sk/currencies.yml
  46. +56 −0 resources/locales/sq/currencies.yml
  47. +1,489 −0 resources/locales/sr/currencies.yml
  48. +903 −0 resources/locales/sv/currencies.yml
  49. +334 −0 resources/locales/ta/currencies.yml
  50. +566 −0 resources/locales/th/currencies.yml
  51. +569 −0 resources/locales/tr/currencies.yml
  52. +786 −0 resources/locales/uk/currencies.yml
  53. +349 −0 resources/locales/ur/currencies.yml
  54. +558 −0 resources/locales/zh-Hant/currencies.yml
  55. +558 −0 resources/locales/zh/currencies.yml
  56. +187 −0 resources/shared/currency_digits_and_rounding.yml
  57. +10 −1 spec/formatters/numbers/currency_formatter_spec.rb
  58. +3 −3 spec/readme_spec.rb
  59. +7 −9 spec/shared/currencies_spec.rb
  60. +7 −0 spec/spec_helper.rb
  61. +18 −16 spec/utils/yaml/yaml_spec.rb
View
@@ -21,5 +21,10 @@ group :test do
platform :mri_18 do
gem 'rcov'
end
+
+ platform :mri_19 do
+ gem 'simplecov'
+ gem 'launchy'
+ end
end
View
@@ -32,11 +32,32 @@ namespace :spec do
end
if RUBY_VERSION < '1.9.0'
- desc 'Run all examples with RCov'
- RSpec::Core::RakeTask.new('spec:rcov') do |t|
- t.rcov = true
- t.pattern = './spec/**/*_spec.rb'
- t.rcov_opts = %w(-T --sort coverage --exclude gems/,spec/)
+ desc 'Run specs with RCov'
+ RSpec::Core::RakeTask.new('spec:cov') do |t|
+ t.rcov = true
+ t.pattern = './spec/**/*_spec.rb'
+ t.rcov_opts = '-T --sort coverage --exclude gems/,spec/'
+ end
+
+ desc 'Run full specs suit with RCov'
+ task 'spec:cov:full' => %w[spec:full_spec_env spec:cov]
+else
+ namespace :spec do
+ desc 'Run specs with SimpleCov'
+ task :cov => ['spec:simplecov_env', :spec] do
+ require 'launchy'
+ Launchy.open 'coverage/index.html'
+ end
+
+ desc 'Run full specs suit with SimpleCov'
+ task 'cov:full' => %w[spec:full_spec_env spec:cov]
+
+ task :simplecov_env do
+ puts 'Cleaning up coverage reports'
+ rm_rf 'coverage'
+
+ ENV['SCOV'] = 'true'
+ end
end
end
@@ -45,7 +66,7 @@ namespace :update do
task :locales_resources, :cldr_path do |_, args|
TwitterCldr::Resources::LocalesResourcesImporter.new(
args[:cldr_path] || './vendor/cldr',
- './resources/locales'
+ './resources'
).import
end
@@ -79,6 +100,14 @@ namespace :update do
).import
end
+ desc 'Import currency digits and rounding resource'
+ task :currency_digits_and_rounding, :cldr_path do |_, args|
+ TwitterCldr::Resources::CurrencyDigitsAndRoundingImporter.new(
+ args[:cldr_path] || './vendor/cldr',
+ './resources/shared'
+ ).import
+ end
+
desc 'Import postal codes resource'
task :postal_codes, :cldr_path do |_, args|
TwitterCldr::Resources::PostalCodesImporter.new(
@@ -6,29 +6,28 @@
module TwitterCldr
module Formatters
class CurrencyFormatter < NumberFormatter
- DEFAULT_CURRENCY_SYMBOL = "$"
- DEFAULT_PRECISION = 2
-
def initialize(options = {})
@tokenizer = TwitterCldr::Tokenizers::NumberTokenizer.new(:locale => self.extract_locale(options), :type => :currency)
super
end
def format(number, options = {})
- if options[:currency]
- currency ||= TwitterCldr::Shared::Currencies.for_code(options[:currency])
- currency ||= TwitterCldr::Shared::Currencies.for_country(options[:currency])
- currency ||= { :symbol => options[:currency] }
- else
- currency = { :symbol => DEFAULT_CURRENCY_SYMBOL }
- end
+ 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] }
+
+ digits_and_rounding = resource(options[:currency])
+ options[:precision] ||= digits_and_rounding[:digits]
+ options[:rounding] ||= digits_and_rounding[:rounding]
- super(number, options).gsub('¤', currency[:symbol])
+ super(number, options).gsub('¤', (currency[:symbol] || currency[:currency].to_s))
end
- def default_format_options_for(number)
- precision = precision_from(number)
- { :precision => precision == 0 ? DEFAULT_PRECISION : precision }
+ private
+ def resource(code)
+ @resource ||= TwitterCldr.get_resource(:shared, :currency_digits_and_rounding)
+ @resource[code.to_sym] || @resource[:DEFAULT]
end
end
end
@@ -16,10 +16,6 @@ def format(number, options = {})
rescue TypeError, ArgumentError
number
end
-
- def default_format_options_for(number)
- { :precision => precision_from(number) }
- end
end
end
end
@@ -14,10 +14,10 @@ def initialize(options = {})
@symbols = DEFAULT_SYMBOLS.merge(tokenizer.symbols)
end
- def format(number, options = {})
- opts = self.default_format_options_for(number).merge(options)
+ def format(number, opts = {})
+ opts[:precision] ||= precision_from(number)
prefix, suffix, integer_format, fraction_format = *partition_tokens(self.get_tokens(number, opts))
-
+
int, fraction = parse_number(number, opts)
result = integer_format.apply(int, opts)
result << fraction_format.apply(fraction, opts) if fraction
@@ -35,13 +35,21 @@ def partition_tokens(tokens)
def parse_number(number, options = {})
precision = options[:precision] || self.precision_from(number)
- number = "%.#{precision}f" % round_to(number, precision).abs
+ rounding = options[:rounding] || 0
+
+ number = "%.#{precision}f" % round_to(number, precision, rounding).abs
number.split(".")
end
- def round_to(number, precision)
+ def round_to(number, precision, rounding = 0)
factor = 10 ** precision
- (number * factor).round.to_f / factor
+ result = (number * factor).round.to_f / factor
+ if rounding > 0
+ rounding = rounding.to_f / factor
+ result = (result * (1.0 / rounding)).round.to_f / (1.0 / rounding)
+ end
+
+ result
end
def precision_from(num)
@@ -16,10 +16,6 @@ def initialize(options = {})
def format(number, options = {})
super(number, options).gsub('¤', @tokenizer.symbols[:percent_sign] || DEFAULT_PERCENT_SIGN)
end
-
- def default_format_options_for(number)
- { :precision => 0 }
- end
end
end
end
@@ -5,17 +5,19 @@
module TwitterCldr
module Resources
- autoload :CanonicalCompositionsUpdater, 'twitter_cldr/resources/canonical_compositions_updater'
- autoload :CollationTriesDumper, 'twitter_cldr/resources/collation_tries_dumper'
- autoload :CompositionExclusionsImporter, 'twitter_cldr/resources/composition_exclusions_importer'
- autoload :CustomLocalesResourcesImporter, 'twitter_cldr/resources/custom_locales_resources_importer'
- autoload :LanguageCodesImporter, 'twitter_cldr/resources/language_codes_importer'
- autoload :Loader, 'twitter_cldr/resources/loader'
- autoload :LocalesResourcesImporter, 'twitter_cldr/resources/locales_resources_importer'
- autoload :PhoneCodesImporter, 'twitter_cldr/resources/phone_codes_importer'
- autoload :PostalCodesImporter, 'twitter_cldr/resources/postal_codes_importer'
- autoload :TailoringImporter, 'twitter_cldr/resources/tailoring_importer'
- autoload :UnicodeDataImporter, 'twitter_cldr/resources/unicode_data_importer'
- autoload :BidiTestImporter, 'twitter_cldr/resources/bidi_test_importer'
+ autoload :CanonicalCompositionsUpdater, 'twitter_cldr/resources/canonical_compositions_updater'
+ autoload :CollationTriesDumper, 'twitter_cldr/resources/collation_tries_dumper'
+ autoload :CompositionExclusionsImporter, 'twitter_cldr/resources/composition_exclusions_importer'
+ autoload :CurrenciesImporter, 'twitter_cldr/resources/currencies_importer'
+ autoload :CurrencyDigitsAndRoundingImporter, 'twitter_cldr/resources/currency_digits_and_rounding_importer'
+ autoload :CustomLocalesResourcesImporter, 'twitter_cldr/resources/custom_locales_resources_importer'
+ autoload :LanguageCodesImporter, 'twitter_cldr/resources/language_codes_importer'
+ autoload :Loader, 'twitter_cldr/resources/loader'
+ autoload :LocalesResourcesImporter, 'twitter_cldr/resources/locales_resources_importer'
+ autoload :PhoneCodesImporter, 'twitter_cldr/resources/phone_codes_importer'
+ autoload :PostalCodesImporter, 'twitter_cldr/resources/postal_codes_importer'
+ autoload :TailoringImporter, 'twitter_cldr/resources/tailoring_importer'
+ autoload :UnicodeDataImporter, 'twitter_cldr/resources/unicode_data_importer'
+ autoload :BidiTestImporter, 'twitter_cldr/resources/bidi_test_importer'
end
end
@@ -12,7 +12,7 @@ module Resources
class LocalesResourcesImporter
- COMPONENTS = %w[calendars languages numbers units plurals lists layout]
+ COMPONENTS = %w[calendars languages numbers units plurals lists layout currencies]
# Arguments:
#
@@ -39,19 +39,23 @@ def prepare_ruby_cldr
# Copies zh plurals to zh-Hant (they can share, but locale code needs to be different).
#
def copy_zh_hant_plurals
- File.open(File.join(@output_path, 'zh-Hant', 'plurals.yml'), 'w:utf-8') do |output|
- data = YAML.load(File.read(File.join(@output_path, 'zh', 'plurals.yml')))
+ File.open(File.join(@output_path, 'locales', 'zh-Hant', 'plurals.yml'), 'w:utf-8') do |output|
+ data = YAML.load(File.read(File.join(@output_path, 'locales', 'zh', 'plurals.yml')))
output.write(YAML.dump(:'zh-Hant' => data[:zh].gsub(':zh', ":'zh-Hant'")))
end
end
def import_components
- Cldr::Export.export(:locales => TwitterCldr.supported_locales, :components => COMPONENTS, :target => @output_path) do |component, locale, path|
+ Cldr::Export.export(:locales => TwitterCldr.supported_locales, :components => COMPONENTS, :target => File.join(@output_path, 'locales')) do |component, locale, path|
add_buddhist_calendar(component, locale, path)
process_plurals(component, locale, path)
deep_symbolize(component, locale, path)
end
+ Cldr::Export.export(:components => ["currency_digits_and_rounding"], :target => File.join(@output_path, 'shared')) do |component, locale, path|
+ deep_symbolize(component, locale, path)
+ end
+
copy_zh_hant_plurals
end
@@ -65,7 +69,7 @@ def deep_symbolize(component, locale, path)
end
def process_plurals(component, locale, path)
- return unless component == 'plurals'
+ return unless component == 'Plurals'
plural_rules = File.read(path)
@@ -79,7 +83,7 @@ def process_plurals(component, locale, path)
# TODO: export buddhist calendar from CLDR data instead of using BUDDHIST_CALENDAR constant.
#
def add_buddhist_calendar(component, locale, path)
- return unless component == 'calendars' && locale == :th
+ return unless component == 'Calendars' && locale == :th
data = YAML.load(File.read(path))
data['th']['calendars']['buddhist'] = BUDDHIST_CALENDAR
@@ -6,34 +6,41 @@
module TwitterCldr
module Shared
module Currencies
-
class << self
-
def countries
- resource.keys.map(&:to_s)
+ Kernel.warn("Currencies#countries will be deprecated. Please use Currencies#for_code.")
+ resource_countries.keys.map(&:to_s)
end
- def currency_codes
- resource.values.map { |data| data[:code] }
+ def currency_codes(locale = :en)
+ resource(locale).keys.map { |c| c.to_s }
end
- def for_country(country_name)
- resource[country_name.to_sym]
+ 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)
- country_name, data = resource.detect { |_, data| data[:code] == currency_code }
- { :country => country_name.to_s, :currency => data[:currency], :symbol => data[:symbol] } if data
+ 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
end
private
- def resource
- @resource ||= TwitterCldr.get_resource(:shared, :currencies)
+ def resource_countries
+ @resource_countries ||= TwitterCldr.get_resource(:shared, :currencies)
end
+ def resource(locale)
+ locale = locale.to_sym
+ @resource ||= {}
+ @resource[locale] ||= TwitterCldr.get_resource(:locales, locale, :currencies)[locale][:currencies]
+ end
end
-
end
end
end
Oops, something went wrong.

0 comments on commit f78beb0

Please sign in to comment.