diff --git a/lib/initializers/i18n.rb b/lib/initializers/i18n.rb index d061b78f..0a567d44 100644 --- a/lib/initializers/i18n.rb +++ b/lib/initializers/i18n.rb @@ -1,2 +1,2 @@ require "i18n/backend/pluralization" -I18n::Backend::Simple.send(:include, I18n::Backend::Pluralization) \ No newline at end of file +I18n::Backend::Simple.send(:include, I18n::Backend::Pluralization) diff --git a/lib/locales/numbers.en.yml b/lib/locales/numbers.en.yml index f2a56e54..15b879b6 100644 --- a/lib/locales/numbers.en.yml +++ b/lib/locales/numbers.en.yml @@ -1,8 +1,7 @@ en: numbers: - ones: [nought, one, two, three, four, five, six, seven, eight, nine] + ones: [zero, one, two, three, four, five, six, seven, eight, nine] teens: [ten, eleven, twelve, thirteen, fourteen, fifteen, sixteen, seventeen, eighteen, nineteen] - tens: [nought, ten, twenty, thirty, forty, fifty, sixty, seventy, nought, ninety] - union: and + tens: [zero, ten, twenty, thirty, forty, fifty, sixty, seventy, nought, ninety] hundreds: hundred mega: [ones, thousand, million, billion, trillion, quadrillion, quintillion, sextillion, septillion, octillion, nonillion, decillion] diff --git a/lib/numbers_and_words/strategies/en.rb b/lib/numbers_and_words/strategies/en.rb index beab10c5..073aebea 100644 --- a/lib/numbers_and_words/strategies/en.rb +++ b/lib/numbers_and_words/strategies/en.rb @@ -2,18 +2,18 @@ module NumbersAndWords module Strategies class En < Base include NumbersAndWords::TranslationsHelpers::En - + attr_accessor :figures_in_previous_capacity - + def convert figures @figures = figures.reverse - + @words = strings @words.empty? ? zero : @words.reverse.join(' ') end - + private - + def strings if figures.capacity_count complex_to_words @@ -25,7 +25,7 @@ def strings [] end end - + def complex_to_words words = [] figures.capacity_count.times do |capacity| @@ -33,7 +33,7 @@ def complex_to_words @figures = parent_figures.figures_array_in_capacity(capacity) strings end - + unless number_in_capacity_by_words.empty? words.push translation_megs(capacity) if 0 < capacity words += number_in_capacity_by_words @@ -41,11 +41,11 @@ def complex_to_words end words end - + def simple_hundreds_to_words simple_to_words + [translation_hundreds(figures.hundreds)] end - + def simple_to_words if figures.teens [translation_teens(figures.ones)] @@ -59,7 +59,7 @@ def simple_to_words [] end end - + def save_parent_figures parent_figures = @figures result = yield(parent_figures) diff --git a/lib/numbers_and_words/translations_helpers/en.rb b/lib/numbers_and_words/translations_helpers/en.rb index bea1739c..71ddb19f 100644 --- a/lib/numbers_and_words/translations_helpers/en.rb +++ b/lib/numbers_and_words/translations_helpers/en.rb @@ -5,10 +5,6 @@ module En private - def translation_megs capacity - t(:mega)[capacity] - end - def translation_teens number t(:teens)[number] end @@ -33,8 +29,12 @@ def translation_hundreds number [t(:ones)[number], t(:hundreds)].join ' ' end + def translation_megs capacity + t(:mega)[capacity] + end + def zero - t(:ones_male)[0] + t(:ones)[0] end end end diff --git a/spec/numbers_and_words/array/en_spec.rb b/spec/numbers_and_words/array/en_spec.rb new file mode 100644 index 00000000..41262958 --- /dev/null +++ b/spec/numbers_and_words/array/en_spec.rb @@ -0,0 +1,26 @@ +require 'spec_helper' + +describe Array do + around :each do |example| + I18n.with_locale(:en) do + example.run + end + end + + describe 'to_words' do + context 'simple example' do + subject { [1, 2, 3] } + its(:to_words) { should == 3.times.map { |number| t(:ones)[number + 1] } } + end + + context 'complex example' do + subject { [101, 21, 13] } + + its(:to_words) { should == [ + [t(:ones)[1], t(:hundreds), t(:ones)[1]].join(' '), + [t(:tens)[2], t(:ones)[1]].join('-'), + t(:teens)[3] + ] } + end + end +end diff --git a/spec/numbers_and_words/array/ru_spec.rb b/spec/numbers_and_words/array/ru_spec.rb index a862db54..400052cd 100644 --- a/spec/numbers_and_words/array/ru_spec.rb +++ b/spec/numbers_and_words/array/ru_spec.rb @@ -1,13 +1,13 @@ -require './spec/spec_helper' +require 'spec_helper' describe Array do + around :each do |example| I18n.with_locale(:ru) do example.run end end - describe 'to_words' do context 'simple example' do subject { [1, 2, 3] } @@ -24,4 +24,5 @@ ] } end end + end diff --git a/spec/numbers_and_words/integer/en_spec.rb b/spec/numbers_and_words/integer/en_spec.rb new file mode 100644 index 00000000..bbf118d3 --- /dev/null +++ b/spec/numbers_and_words/integer/en_spec.rb @@ -0,0 +1,338 @@ +require 'spec_helper' + +describe Integer do + + around :each do |example| + I18n.with_locale(:en) do + example.run + end + end + + describe '#to_words' do + describe 'ones' do + context '0 as uniq example' do + subject { 0 } + its(:to_words) { should == t(:ones)[0] } + end + + context '1 as lower example' do + subject { 1 } + its(:to_words) { should == t(:ones)[1] } + end + + context '9 as upper example' do + subject { 9 } + its(:to_words) { should == t(:ones)[9] } + end + end + + describe 'teens' do + context '10 as uniq example' do + subject { 10 } + its(:to_words) { should == t(:teens)[0] } + end + + context '11 as lower example' do + subject { 11 } + its(:to_words) { should == t(:teens)[1] } + end + + context '19 as upper example' do + subject { 19 } + its(:to_words) { should == t(:teens)[9] } + end + end + + describe 'tens' do + context '20 as lower example' do + subject { 20 } + its(:to_words) { should == t(:tens)[2] } + end + + context '21 as lower example with ones' do + subject { 21 } + its(:to_words) { should == [t(:tens)[2], t(:ones)[1]].join('-') } + end + + context '90 as upper example' do + subject { 90 } + its(:to_words) { should == t(:tens)[9] } + end + + context '99 as upper example with ones' do + subject { 99 } + its(:to_words) { should == [t(:tens)[9], t(:ones)[9]].join('-') } + end + end + + describe 'hundreds' do + context '100 as lower example' do + subject { 100 } + its(:to_words) { should == [ + t(:ones)[1], + t(:hundreds) + ].join(' ') } + end + + context '101 as lower example with ones' do + subject { 101 } + its(:to_words) { should == [ + t(:ones)[1], + t(:hundreds), + t(:ones)[1] + ].join(' ') } + end + + context '111 as lower example with teens' do + subject { 111 } + its(:to_words) { should == [ + t(:ones)[1], + t(:hundreds), + t(:teens)[1] + ].join(' ') } + end + + context '120 as lower example with tens' do + subject { 120 } + its(:to_words) { should == [ + t(:ones)[1], + t(:hundreds), + t(:tens)[2] + ].join(' ') } + end + + context '121 as lower example with tens and ones' do + subject { 121 } + + its(:to_words) { should == [ + t(:ones)[1], + t(:hundreds), + [ t(:tens)[2], t(:ones)[1] ].join('-') + ].flatten.join(' ') } + end + + context '900 as upper example' do + subject { 900 } + its(:to_words) { should == [ + t(:ones)[9], + t(:hundreds) + ].join(' ') } + end + + context '909 as upper example with ones' do + subject { 909 } + its(:to_words) { should == [ + t(:ones)[9], + t(:hundreds), + t(:ones)[9] + ].join(' ') } + end + + context '919 as upper example with teens' do + subject { 919 } + its(:to_words) { should == [ + t(:ones)[9], + t(:hundreds), + t(:teens)[9] + ].join(' ') } + end + + context '990 as upper example with tens' do + subject { 990 } + its(:to_words) { should == [ + t(:ones)[9], + t(:hundreds), + t(:tens)[9] + ].join(' ') } + end + + context '999 as upper example with tens and ones' do + subject { 999 } + + its(:to_words) { should == [ + t(:ones)[9], + t(:hundreds), + [t(:tens)[9], t(:ones)[9]].join('-'), + ].join(' ') } + end + end + + describe 'thousands' do + describe 'one' do + context '1000 as uniq example' do + subject { 1000 } + + its(:to_words) { should == [t(:ones)[1], t(:mega)[1]]. + join(' ') } + end + + context '21000 as uniq example' do + subject { 21000 } + + its(:to_words) { should == [ + [ t(:tens)[2], t(:ones)[1] ].join('-'), t(:mega)[1] + ].flatten.join(' ') } + end + end + + describe 'few' do + context '2000 as lower example' do + subject { 2000 } + + its(:to_words) { should == [t(:ones)[2], t(:mega)[1]].join(' ') } + end + + context '4000 as upper example' do + subject { 4000 } + + its(:to_words) { should == [t(:ones)[4], t(:mega)[1]].join(' ') } + end + end + + describe 'many' do + context '5000 as lower example' do + subject { 5000 } + + its(:to_words) { should == [t(:ones)[5], t(:mega)[1]].join(' ') } + end + + context '11000 as uniq example' do + subject { 11000 } + + its(:to_words) { should == [t(:teens)[1], t(:mega)[1]].join(' ') } + end + + context '999000 as upper example' do + subject { 999000 } + + its(:to_words) { should == [ + t(:ones)[9], + t(:hundreds), + [t(:tens)[9], t(:ones)[9]].join('-'), + t(:mega)[1], + ].flatten.join(' ') } + end + end + + describe 'custom' do + context '999999 as example with first capacity' do + subject { 999999 } + + its(:to_words) { should == [ + t(:ones)[9], + t(:hundreds), + [t(:tens)[9], t(:ones)[9]].join('-'), + t(:mega)[1], + t(:ones)[9], + t(:hundreds), + [t(:tens)[9], t(:ones)[9]].join('-'), + ].flatten.join(' ') } + end + end + end + end + + describe 'millions' do + describe 'one' do + context '1000000 as uniq example' do + subject { 1000000 } + + its(:to_words) { should == [ + t(:ones)[1], + t(:mega)[2] + ].join(' ') } + end + end + + context 'few' do + context '2000000 as lower example in few' do + subject { 2000000 } + + its(:to_words) { should == [ + t(:ones)[2], + t(:mega)[2] + ].join(' ') } + end + + context '4000000 as upper example in few' do + subject { 4000000 } + + its(:to_words) { should == [ + t(:ones)[4], + t(:mega)[2] + ].join(' ') } + end + end + + context 'many' do + context '5000000 as lower example in many' do + subject { 5000000 } + + its(:to_words) { should == [ + t(:ones)[5], + t(:mega)[2] + ].join(' ') } + end + + context '99900000 as upper example' do + subject { 999000000 } + + its(:to_words) { should == [ + t(:ones)[9], + t(:hundreds), + [t(:tens)[9], t(:ones)[9]].join('-'), + t(:mega)[2], + ].flatten.join(' ') } + end + end + + context 'custom' do + context '999000999 as example with first capacity' do + subject { 999000999 } + + its(:to_words) { should == [ + t(:ones)[9], + t(:hundreds), + [t(:tens)[9], t(:ones)[9]].join('-'), + t(:mega)[2], + t(:ones)[9], + t(:hundreds), + [t(:tens)[9], t(:ones)[9]].join('-'), + ].flatten.join(' ') } + end + + context '999999000 as example with second capacity' do + subject { 999999000 } + + its(:to_words) { should == [ + t(:ones)[9], + t(:hundreds), + [t(:tens)[9], t(:ones)[9]].join('-'), + t(:mega)[2], + t(:ones)[9], + t(:hundreds), + [t(:tens)[9], t(:ones)[9]].join('-'), + t(:mega)[1], + ].flatten.join(' ') } + end + + context '999999999 as example with first and second capacity' do + subject { 999999999 } + + its(:to_words) { should == [ + t(:ones)[9], + t(:hundreds), + [t(:tens)[9], t(:ones)[9]].join('-'), + t(:mega)[2], + t(:ones)[9], + t(:hundreds), + [t(:tens)[9], t(:ones)[9]].join('-'), + t(:mega)[1], + t(:ones)[9], + t(:hundreds), + [t(:tens)[9], t(:ones)[9]].join('-'), + ].flatten.join(' ') } + end + end + end +end