papla 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,7 @@
1
+ ### 0.0.3 (2012-02-16)
2
+
3
+ * provide localization using I18n
4
+
1
5
  ### 0.0.2 (2011-01-09)
2
6
 
3
7
  * display decimal part of floats as cents/100
data/README.md CHANGED
@@ -46,7 +46,13 @@ Papla[2.999] # => "Trzy 00/100"
46
46
 
47
47
  This feature is planned for future releases.
48
48
 
49
- ### Integration with Money, I18n and Rails
49
+ ## Integration with Money, I18n and Rails
50
+
51
+ ### I18n
52
+
53
+ Localization is provided by I18n. See `lib/papla/backend.rb` for details of keys used.
54
+
55
+ ### Money, Rails
50
56
 
51
57
  This feature is planned for future releases.
52
58
 
@@ -1,5 +1,5 @@
1
- # encoding: utf-8
2
- require "papla/version"
1
+ require 'papla/backend'
2
+ require 'papla/version'
3
3
 
4
4
  module Papla
5
5
  # Converts a number to Polish words, capitalizing
@@ -49,31 +49,6 @@ module Papla
49
49
  end
50
50
 
51
51
  private
52
- ZERO = 'zero'
53
-
54
- ONES = [nil] + %w[
55
- jeden dwa trzy cztery pięć
56
- sześć siedem osiem dziewięć dziesięć
57
- jedenaście dwanaście trzynaście czternaście piętnaście
58
- szesnaście siedemnaście osiemnaście dziewiętnaście
59
- ].freeze
60
-
61
- TENS = [nil, nil] + %w[
62
- dwadzieścia trzydzieści czterdzieści pięćdziesiąt
63
- sześćdziesiąt siedemdziesiąt osiemdziesiąt dziewięćdziesiąt
64
- ].freeze
65
-
66
- HUNDREDS = [nil] + %w[
67
- sto dwieście trzysta czterysta pięćset
68
- sześćset siedemset osiemset dziewięćset
69
- ].freeze
70
-
71
- RANKS = [
72
- [],
73
- ['tysięcy', 'tysiąc', 'tysiące'].freeze,
74
- ['milionów', 'milion', 'miliony'].freeze,
75
- ['miliardów', 'miliard', 'miliardy'].freeze
76
- ].freeze
77
52
 
78
53
  def self.prepare(number)
79
54
  case number
@@ -84,7 +59,7 @@ module Papla
84
59
 
85
60
  def self.build_basic_phrase(basic_number)
86
61
  if basic_number.zero?
87
- ZERO
62
+ spell_zero
88
63
  else
89
64
  groups = group(basic_number)
90
65
  groups_as_words = convert_groups(groups)
@@ -110,7 +85,7 @@ module Papla
110
85
  groups.each_with_index do |group, i|
111
86
  if group > 0
112
87
  result << convert_small_number(group)
113
- result << rank(bound - i, group) if i < bound
88
+ result << spell_rank(bound - i, group) if i < bound
114
89
  end
115
90
  end
116
91
 
@@ -121,34 +96,13 @@ module Papla
121
96
  if number.zero?
122
97
  []
123
98
  elsif number < 20
124
- [ONES[number]]
99
+ [spell_ones(number)]
125
100
  elsif number < 100
126
101
  tens, remainder = number.divmod(10)
127
- [TENS[tens], convert_small_number(remainder)]
102
+ [spell_tens(tens), convert_small_number(remainder)]
128
103
  else
129
104
  hundreds, remainder = number.divmod(100)
130
- [HUNDREDS[hundreds], convert_small_number(remainder)]
131
- end
132
- end
133
-
134
- def self.rank(rank, number)
135
- RANKS[rank][declination_index(number)]
136
- end
137
-
138
- def self.declination_index(number)
139
- return 1 if number == 1
140
-
141
- remainder = number - number / 100 * 100
142
-
143
- case remainder
144
- when 12..14 then 0
145
- else
146
- ones = number - number / 10 * 10
147
-
148
- case ones
149
- when 2..4 then 2
150
- else 0
151
- end
105
+ [spell_hundreds(hundreds), convert_small_number(remainder)]
152
106
  end
153
107
  end
154
108
 
@@ -159,6 +113,23 @@ module Papla
159
113
 
160
114
  def self.append_cents(basic_phrase, number)
161
115
  cents = 100 * (number - number.to_i)
162
- "%s %02d/100" % [basic_phrase, cents]
116
+ spell_cents(basic_phrase, cents)
117
+ end
118
+
119
+ @backends = {}
120
+
121
+ def self.backend
122
+ @backends[locale] ||= Backend.new(locale)
123
+ end
124
+
125
+ def self.locale
126
+ :pl
163
127
  end
128
+
129
+ def self.spell_zero; backend.zero; end
130
+ def self.spell_ones(index); backend.ones(index); end
131
+ def self.spell_tens(index); backend.tens(index); end
132
+ def self.spell_hundreds(index); backend.hundreds(index); end
133
+ def self.spell_rank(index, number); backend.rank(index, number); end
134
+ def self.spell_cents(basic_phrase, cents); backend.cents(basic_phrase, cents); end
164
135
  end
@@ -0,0 +1,51 @@
1
+ require 'i18n'
2
+
3
+ I18n.load_path << File.expand_path('../locale/pl.yml', __FILE__)
4
+ I18n.load_path << File.expand_path('../locale/pl.rb', __FILE__)
5
+
6
+ module Papla
7
+ class Backend
8
+ def initialize(locale)
9
+ @locale = locale
10
+ end
11
+
12
+ def zero
13
+ translate(:zero)
14
+ end
15
+
16
+ def ones(index)
17
+ translate(:ones)[index]
18
+ end
19
+
20
+ def tens(index)
21
+ translate(:tens)[index]
22
+ end
23
+
24
+ def hundreds(index)
25
+ translate(:hundreds)[index]
26
+ end
27
+
28
+ def rank(index, number)
29
+ rule = pluralization_rule(number)
30
+ rules = translate(:ranks)[index]
31
+ rules[rule.to_s]
32
+ end
33
+
34
+ def cents(basic_phrase, cents)
35
+ cents = "%02d" % cents
36
+ translate(:cents, basic_phrase: basic_phrase, cents: cents)
37
+ end
38
+
39
+ private
40
+
41
+ def translate(message, options = {})
42
+ options[:locale] = @locale
43
+ options[:scope] = :papla
44
+ I18n.translate(message, options)
45
+ end
46
+
47
+ def pluralization_rule(number)
48
+ I18n.translate(:papla, locale: @locale)[:pluralization][number]
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,18 @@
1
+ {
2
+ pl: {
3
+ papla: {
4
+ pluralization: lambda do |n|
5
+ return :one if n == 1
6
+
7
+ case n % 100
8
+ when 12..14 then :other
9
+ else
10
+ case n % 10
11
+ when 2..4 then :few
12
+ else :other
13
+ end
14
+ end
15
+ end
16
+ }
17
+ }
18
+ }
@@ -0,0 +1,61 @@
1
+ pl:
2
+ papla:
3
+ zero: zero
4
+ ones:
5
+ - ~
6
+ - jeden
7
+ - dwa
8
+ - trzy
9
+ - cztery
10
+ - pięć
11
+ - sześć
12
+ - siedem
13
+ - osiem
14
+ - dziewięć
15
+ - dziesięć
16
+ - jedenaście
17
+ - dwanaście
18
+ - trzynaście
19
+ - czternaście
20
+ - piętnaście
21
+ - szesnaście
22
+ - siedemnaście
23
+ - osiemnaście
24
+ - dziewiętnaście
25
+ tens:
26
+ - ~
27
+ - ~
28
+ - dwadzieścia
29
+ - trzydzieści
30
+ - czterdzieści
31
+ - pięćdziesiąt
32
+ - sześćdziesiąt
33
+ - siedemdziesiąt
34
+ - osiemdziesiąt
35
+ - dziewięćdziesiąt
36
+ hundreds:
37
+ - ~
38
+ - sto
39
+ - dwieście
40
+ - trzysta
41
+ - czterysta
42
+ - pięćset
43
+ - sześćset
44
+ - siedemset
45
+ - osiemset
46
+ - dziewięćset
47
+ ranks:
48
+ - ~
49
+ -
50
+ one: tysiąc
51
+ few: tysiące
52
+ other: tysięcy
53
+ -
54
+ one: milion
55
+ few: miliony
56
+ other: milionów
57
+ -
58
+ one: miliard
59
+ few: miliardy
60
+ other: miliardów
61
+ cents: "%{basic_phrase} %{cents}/100"
@@ -1,3 +1,3 @@
1
1
  module Papla
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
@@ -18,6 +18,8 @@ Gem::Specification.new do |s|
18
18
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
19
  s.require_paths = ["lib"]
20
20
 
21
+ s.add_dependency 'i18n'
22
+
21
23
  s.add_development_dependency 'rspec', '>= 2.8.0'
22
24
  s.add_development_dependency 'rake'
23
25
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: papla
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,22 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-09 00:00:00.000000000 Z
12
+ date: 2012-01-16 00:00:00.000000000 Z
13
13
  dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: i18n
16
+ requirement: &6025320 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *6025320
14
25
  - !ruby/object:Gem::Dependency
15
26
  name: rspec
16
- requirement: &9069800 !ruby/object:Gem::Requirement
27
+ requirement: &6023880 !ruby/object:Gem::Requirement
17
28
  none: false
18
29
  requirements:
19
30
  - - ! '>='
@@ -21,10 +32,10 @@ dependencies:
21
32
  version: 2.8.0
22
33
  type: :development
23
34
  prerelease: false
24
- version_requirements: *9069800
35
+ version_requirements: *6023880
25
36
  - !ruby/object:Gem::Dependency
26
37
  name: rake
27
- requirement: &9069220 !ruby/object:Gem::Requirement
38
+ requirement: &6021540 !ruby/object:Gem::Requirement
28
39
  none: false
29
40
  requirements:
30
41
  - - ! '>='
@@ -32,7 +43,7 @@ dependencies:
32
43
  version: '0'
33
44
  type: :development
34
45
  prerelease: false
35
- version_requirements: *9069220
46
+ version_requirements: *6021540
36
47
  description: Papla is a Ruby gem that allows you to convert numbers into Polish words
37
48
  (e.g. 153 into "sto pięćdziesiąt trzy"), including the decimal part as cents and
38
49
  currency symbol. Its primary use case are invoices, where the total amount has to
@@ -52,6 +63,9 @@ files:
52
63
  - README.md
53
64
  - Rakefile
54
65
  - lib/papla.rb
66
+ - lib/papla/backend.rb
67
+ - lib/papla/locale/pl.rb
68
+ - lib/papla/locale/pl.yml
55
69
  - lib/papla/version.rb
56
70
  - papla.gemspec
57
71
  - spec/papla_spec.rb