numbers_and_words 0.3.2 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -8,6 +8,8 @@ Convert numbers to words using the I18n library.
8
8
 
9
9
  * English
10
10
  * Русский
11
+ * Українська
12
+ * Türkçe
11
13
 
12
14
  == Examples / Примеры
13
15
 
@@ -20,22 +22,32 @@ Convert numbers to words using the I18n library.
20
22
  21.to_words
21
23
  => "twenty-one"
22
24
  => "двадцать один"
25
+ => "двадцять один"
26
+ => "yirmi bir"
23
27
 
24
28
  231.to_words
25
29
  => "two hundred thirty-one"
26
30
  => "двести тридцать один"
31
+ => "двiстi тридцять один"
32
+ => "iki yüz otuz bir"
27
33
 
28
34
  4030.to_words
29
35
  => "four thousand thirty"
30
36
  => "четыре тысячи тридцать"
37
+ => "чотири тисячi тридцять"
38
+ => "dört bin otuz"
31
39
 
32
40
  1000100.to_words
33
41
  => "one million one hundred"
34
42
  => "один миллион сто"
43
+ => "один мiльйон сто"
44
+ => "bir milyon bir yüz"
35
45
 
36
46
  1000000000000000000000000000000000.to_words
37
47
  => "one decillion"
38
48
  => "один дециллион"
49
+ => "один децильйон"
50
+ => "bir desilyon"
39
51
 
40
52
  [1, 2, 3].to_words
41
53
  => ["one", "two", "three"]
@@ -44,7 +56,9 @@ Convert numbers to words using the I18n library.
44
56
  [11, 22, 133].to_words
45
57
  => ["eleven", "twenty-two", "one hundred thirty-three"]
46
58
  => ["одиннадцать", "двадцать два", "сто тридцать три"]
47
-
59
+ => ["одинадцять", "двадцять два", "сто тридцять три"]
60
+ => ["on bir", "yirmi iki", "bir yüz otuz üç"]
61
+
48
62
  == Requirements / Требования
49
63
 
50
64
  * 1.8.7 <= Ruby (compatible with/совместимость с Ruby 1.9, JRuby and/и Rubinius);
@@ -70,3 +84,4 @@ Send a pull request. Bonus points for topic branches.
70
84
 
71
85
  * Kirill Lazarev (mailto:k.s.lazarev@gmail.com)
72
86
  * Daniel Doubrovkine (link:http://github.com/dblock)
87
+ * Sergey Shkirando (mailto:shkirando.s@yandex.ru)
@@ -1,30 +1,14 @@
1
1
  require 'rubygems'
2
2
  require 'i18n'
3
- require 'initializers/i18n'
3
+ require 'i18n/backend/pluralization'
4
4
  require 'active_support/inflector'
5
5
 
6
- require 'numbers_and_words/version'
7
-
8
6
  require 'numbers_and_words/translations_helpers'
9
7
  require 'numbers_and_words/strategies'
10
8
  require 'numbers_and_words/array_additions'
11
9
  require 'numbers_and_words/figures_array'
10
+ require 'numbers_and_words/core_ext'
11
+ require 'numbers_and_words/i18n'
12
+ require 'numbers_and_words/version'
12
13
 
13
- require 'numbers_and_words/core_ext/integer'
14
- require 'numbers_and_words/core_ext/array'
15
-
16
- module NumbersAndWords
17
- module I18nInitialization
18
- extend self
19
-
20
- def init
21
- locale_files.each { |locale_file| I18n.load_path << locale_file }
22
- end
23
-
24
- def locale_files
25
- Dir[File.join(File.dirname(__FILE__), 'locales', '**/*')]
26
- end
27
- end
28
- end
29
-
30
- NumbersAndWords::I18nInitialization.init
14
+ NumbersAndWords::I18n::Initialization.init
@@ -0,0 +1,2 @@
1
+ require 'numbers_and_words/core_ext/integer'
2
+ require 'numbers_and_words/core_ext/array'
@@ -0,0 +1,12 @@
1
+ require 'numbers_and_words/i18n/pluralization'
2
+ require 'numbers_and_words/i18n/initialization'
3
+
4
+ module NumbersAndWords
5
+ module I18n
6
+ extend self
7
+
8
+ def files directory, ext
9
+ Dir[File.join File.dirname(__FILE__), "i18n/#{directory}", "**/#{ext}"]
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,22 @@
1
+ module NumbersAndWords
2
+ module I18n
3
+ module Initialization
4
+ extend self
5
+
6
+ def init
7
+ locale_files.each { |locale_file| ::I18n.load_path << locale_file}
8
+ NumbersAndWords::I18n::Pluralization.init
9
+ end
10
+
11
+ private
12
+
13
+ def locale_files
14
+ I18n.files 'locales', '*.*'
15
+ end
16
+
17
+ def languages
18
+ locale_files.map{|path| path.split /[\/.]/}[-2]
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,7 @@
1
+ tr:
2
+ numbers:
3
+ ones: [sıfır, bir, iki, üç, dört, beş, altı, yedi, sekiz, dokuz]
4
+ teens: ['on', on bir, on iki, on üç, on dört, on beş, on altı, on yedi, on sekiz, on dokuz]
5
+ tens: [sıfır, 'on', yirmi, otuz, kırk, elli, altmış, yetmiş, seksen, doksan]
6
+ hundreds: yüz
7
+ mega: [bir, bin, milyon, milyar, trilyon, katrilyon, kentilyon, seksilyon, septilyon, oktilyon, nonilyon, desilyon]
@@ -0,0 +1,52 @@
1
+ ua:
2
+ numbers:
3
+ ones_male: [нуль, один, два, три, чотири, п’ять, шiсть, сiм, вiсiм, дев’ять]
4
+ ones_female: [нуль, однa, двi, три, чотири, п’ять, шiсть, сiм, вiсiм, дев’ять]
5
+ teens: [десять, одинадцять, дванадцять, тринадцять, чотирнадцять, п’ятнадцять, шiстнадцять, сiмнадцять, вiсiмнадцять, дев’ятнадцять]
6
+ tens: [нуль, десять, двадцять, тридцять, сорок, п’ятдесят, шiстдесят, сiмдесят, вiсiмдесят, дев’яносто]
7
+ hundreds: [нуль, сто, двiстi, триста, чотириста, п’ятсот, шiстсот, сiмсот, вiсiмсот, дев’ятсот]
8
+ thousands:
9
+ one: тисяча
10
+ few: тисячi
11
+ many: тисяч
12
+ millions:
13
+ one: мiльйон
14
+ few: мiльйона
15
+ many: мiльйонiв
16
+ billions:
17
+ one: мiльярд
18
+ few: мiльярда
19
+ many: мiльярдiв
20
+ trillions:
21
+ one: трильйон
22
+ few: трильйона
23
+ many: трильйонiв
24
+ quadrillions:
25
+ one: квадрильйон
26
+ few: квадрильйона
27
+ many: квадрильйонiв
28
+ quintillions:
29
+ one: квiнтильйон
30
+ few: квiнтильйона
31
+ many: квiнтильйонiв
32
+ sextillions:
33
+ one: секстильйон
34
+ few: секстильйона
35
+ many: секстильйонiв
36
+ septillions:
37
+ one: септильйон
38
+ few: септильйона
39
+ many: септильйонiв
40
+ octillions:
41
+ one: октильйон
42
+ few: октильйона
43
+ many: октильйонiв
44
+ nonillions:
45
+ one: нонильйон
46
+ few: нонильйона
47
+ many: нонильйонiв
48
+ decillions:
49
+ one: децильйон
50
+ few: децильйона
51
+ many: децильйонiв
52
+ mega: [ones, thousands, millions, billions, trillions, quadrillions, quintillion, sextillions, septillions, octillions, nonillions, decillions]
@@ -0,0 +1,31 @@
1
+ require 'numbers_and_words/i18n/plurals/ru'
2
+ require 'numbers_and_words/i18n/plurals/ua'
3
+
4
+ module NumbersAndWords
5
+ module I18n
6
+ module Pluralization
7
+ extend self
8
+
9
+ def init
10
+ ::I18n.load_path << config_file
11
+ ::I18n::Backend::Simple.send :include, ::I18n::Backend::Pluralization
12
+ end
13
+
14
+ def files
15
+ I18n.files 'plurals', '*.*'
16
+ end
17
+
18
+ def config_file
19
+ I18n.files('plurals', 'plurals.rb').first
20
+ end
21
+
22
+ def plurals_files
23
+ files - [config_file]
24
+ end
25
+
26
+ def languages
27
+ plurals_files.map{|path| path.split(/[\/.]/)[-2]}
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,7 @@
1
+ Hash[
2
+ NumbersAndWords::I18n::Pluralization.languages.map { |language|
3
+ [language.to_sym, {:i18n => {:plural => {
4
+ :rule => "NumbersAndWords::I18n::Plurals::#{language.titleize}::RULE".constantize
5
+ }}}]
6
+ }
7
+ ]
@@ -0,0 +1,28 @@
1
+ module NumbersAndWords
2
+ module I18n
3
+ module Plurals
4
+ module Ru
5
+ RULE = lambda do |n|
6
+ one_conditions(n) ?
7
+ :one : few_conditions(n) ?
8
+ :few : many_conditions(n) ?
9
+ :many : :other
10
+ end
11
+
12
+ extend self
13
+
14
+ def one_conditions n
15
+ n % 10 == 1 && n % 100 != 11
16
+ end
17
+
18
+ def few_conditions n
19
+ [2, 3, 4].include?(n % 10) && ![12, 13, 14].include?(n % 100)
20
+ end
21
+
22
+ def many_conditions n
23
+ n % 10 == 0 || [5, 6, 7, 8, 9].include?(n % 10) || [11, 12, 13, 14].include?(n % 100)
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,9 @@
1
+ module NumbersAndWords
2
+ module I18n
3
+ module Plurals
4
+ module Ua
5
+ RULE = Ru::RULE
6
+ end
7
+ end
8
+ end
9
+ end
@@ -1,3 +1,5 @@
1
1
  require 'numbers_and_words/strategies/base'
2
2
  require 'numbers_and_words/strategies/ru'
3
3
  require 'numbers_and_words/strategies/en'
4
+ require 'numbers_and_words/strategies/ua'
5
+ require 'numbers_and_words/strategies/tr'
@@ -4,7 +4,7 @@ module NumbersAndWords
4
4
  attr_accessor :figures, :words
5
5
 
6
6
  def self.factory
7
- "NumbersAndWords::Strategies::#{I18n.locale.to_s.titleize}".constantize.new
7
+ "NumbersAndWords::Strategies::#{::I18n.locale.to_s.titleize}".constantize.new
8
8
  end
9
9
  end
10
10
  end
@@ -0,0 +1,71 @@
1
+ module NumbersAndWords
2
+ module Strategies
3
+ class Tr < Base
4
+ include NumbersAndWords::TranslationsHelpers::Tr
5
+
6
+ attr_accessor :figures_in_previous_capacity
7
+
8
+ def convert figures
9
+ @figures = figures.reverse
10
+
11
+ @words = strings
12
+ @words.empty? ? zero : @words.reverse.join(' ')
13
+ end
14
+
15
+ private
16
+
17
+ def strings
18
+ if figures.capacity_count
19
+ complex_to_words
20
+ elsif figures.hundreds
21
+ simple_hundreds_to_words
22
+ elsif figures.tens or figures.ones
23
+ simple_to_words
24
+ else
25
+ []
26
+ end
27
+ end
28
+
29
+ def complex_to_words
30
+ words = []
31
+ figures.capacity_count.times do |capacity|
32
+ number_in_capacity_by_words = save_parent_figures do |parent_figures|
33
+ @figures = parent_figures.figures_array_in_capacity(capacity)
34
+ strings
35
+ end
36
+
37
+ unless number_in_capacity_by_words.empty?
38
+ words.push translation_megs(capacity) if 0 < capacity
39
+ words += number_in_capacity_by_words
40
+ end
41
+ end
42
+ words
43
+ end
44
+
45
+ def simple_hundreds_to_words
46
+ simple_to_words + [translation_hundreds(figures.hundreds)]
47
+ end
48
+
49
+ def simple_to_words
50
+ if figures.teens
51
+ [translation_teens(figures.ones)]
52
+ elsif figures.tens
53
+ figures.ones ?
54
+ [translation_tens_with_ones(figures.tens_with_ones)] :
55
+ [translation_tens(figures.tens)]
56
+ elsif figures.ones
57
+ [translation_ones(figures.ones)]
58
+ else
59
+ []
60
+ end
61
+ end
62
+
63
+ def save_parent_figures
64
+ parent_figures = @figures
65
+ result = yield(parent_figures)
66
+ @figures = parent_figures
67
+ result
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,6 @@
1
+ module NumbersAndWords
2
+ module Strategies
3
+ class Ua < Ru
4
+ end
5
+ end
6
+ end
@@ -1,3 +1,4 @@
1
1
  require 'numbers_and_words/translations_helpers/base'
2
2
  require 'numbers_and_words/translations_helpers/ru'
3
3
  require 'numbers_and_words/translations_helpers/en'
4
+ require 'numbers_and_words/translations_helpers/tr'
@@ -4,7 +4,7 @@ module NumbersAndWords
4
4
  I18N_NAMESPACE = :numbers
5
5
 
6
6
  def t attribute, options = {}
7
- I18n.t attribute, options.merge(:scope => I18N_NAMESPACE)
7
+ ::I18n.t attribute, options.merge(:scope => I18N_NAMESPACE)
8
8
  end
9
9
  end
10
10
  end
@@ -0,0 +1,41 @@
1
+ module NumbersAndWords
2
+ module TranslationsHelpers
3
+ module Tr
4
+ include NumbersAndWords::TranslationsHelpers::Base
5
+
6
+ private
7
+
8
+ def translation_teens number
9
+ t(:teens)[number]
10
+ end
11
+
12
+ def translation_tens number
13
+ t(:tens)[number]
14
+ end
15
+
16
+ def translation_ones number
17
+ t(:ones)[number]
18
+ end
19
+
20
+ def translation_tens_with_ones numbers
21
+ [translation_tens(numbers[1]), translation_ones(numbers[0])].join ' '
22
+ end
23
+
24
+ def translation_union
25
+ t :union
26
+ end
27
+
28
+ def translation_hundreds number
29
+ [t(:ones)[number], t(:hundreds)].join ' '
30
+ end
31
+
32
+ def translation_megs capacity
33
+ t(:mega)[capacity]
34
+ end
35
+
36
+ def zero
37
+ t(:ones)[0]
38
+ end
39
+ end
40
+ end
41
+ end
@@ -1,3 +1,3 @@
1
1
  module NumbersAndWords
2
- VERSION = '0.3.2'
2
+ VERSION = '0.4.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: numbers_and_words
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -83,26 +83,35 @@ extra_rdoc_files:
83
83
  - LICENSE.txt
84
84
  - README.rdoc
85
85
  files:
86
- - lib/initializers/i18n.rb
87
- - lib/locales/numbers.en.yml
88
- - lib/locales/numbers.ru.yml
89
- - lib/locales/plurals.rb
90
86
  - lib/numbers_and_words.rb
91
87
  - lib/numbers_and_words/array_additions.rb
92
88
  - lib/numbers_and_words/array_additions/helpers.rb
93
89
  - lib/numbers_and_words/array_additions/validations.rb
90
+ - lib/numbers_and_words/core_ext.rb
94
91
  - lib/numbers_and_words/core_ext/array.rb
95
92
  - lib/numbers_and_words/core_ext/integer.rb
96
93
  - lib/numbers_and_words/figures_array.rb
97
- - lib/numbers_and_words/pluralization.rb
94
+ - lib/numbers_and_words/i18n.rb
95
+ - lib/numbers_and_words/i18n/initialization.rb
96
+ - lib/numbers_and_words/i18n/locales/numbers.en.yml
97
+ - lib/numbers_and_words/i18n/locales/numbers.ru.yml
98
+ - lib/numbers_and_words/i18n/locales/numbers.tr.yml
99
+ - lib/numbers_and_words/i18n/locales/numbers.ua.yml
100
+ - lib/numbers_and_words/i18n/pluralization.rb
101
+ - lib/numbers_and_words/i18n/plurals/plurals.rb
102
+ - lib/numbers_and_words/i18n/plurals/ru.rb
103
+ - lib/numbers_and_words/i18n/plurals/ua.rb
98
104
  - lib/numbers_and_words/strategies.rb
99
105
  - lib/numbers_and_words/strategies/base.rb
100
106
  - lib/numbers_and_words/strategies/en.rb
101
107
  - lib/numbers_and_words/strategies/ru.rb
108
+ - lib/numbers_and_words/strategies/tr.rb
109
+ - lib/numbers_and_words/strategies/ua.rb
102
110
  - lib/numbers_and_words/translations_helpers.rb
103
111
  - lib/numbers_and_words/translations_helpers/base.rb
104
112
  - lib/numbers_and_words/translations_helpers/en.rb
105
113
  - lib/numbers_and_words/translations_helpers/ru.rb
114
+ - lib/numbers_and_words/translations_helpers/tr.rb
106
115
  - lib/numbers_and_words/version.rb
107
116
  - LICENSE.txt
108
117
  - README.rdoc
@@ -1,2 +0,0 @@
1
- require "i18n/backend/pluralization"
2
- I18n::Backend::Simple.send(:include, I18n::Backend::Pluralization)
@@ -1,23 +0,0 @@
1
- module NumbersAndWords
2
- module Locales
3
- module Plurals
4
-
5
- RU = lambda do |n|
6
- n % 10 == 1 && n % 100 != 11 ?
7
- :one :
8
- [2, 3, 4].include?(n % 10) && ![12, 13, 14].include?(n % 100) ?
9
- :few :
10
- n % 10 == 0 || [5, 6, 7, 8, 9].include?(n % 10) || [11, 12, 13, 14].include?(n % 100) ?
11
- :many : :other
12
- end
13
-
14
- EN = lambda { |count| 1 == count ? :one : :other }
15
-
16
- end
17
- end
18
- end
19
-
20
- {
21
- :ru => {:i18n => {:plural => {:rule => NumbersAndWords::Locales::Plurals::RU}}},
22
- :en => {:i18n => {:plural => {:rule => NumbersAndWords::Locales::Plurals::EN}}}
23
- }
@@ -1,2 +0,0 @@
1
- require "i18n/backend/pluralization"
2
- I18n::Backend::Simple.send(:include, I18n::Backend::Pluralization)