Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for Italian language (:it locale) with tests #69

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ Currently supported locales:
* Thai: `:th`
* Russian: `:ru` (except for decimals)
* Portuguese: `:pt`
* Italian: `:it`

## Testing

Expand Down
2 changes: 1 addition & 1 deletion lib/humanize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def humanize(locale: Humanize.config.default_locale,
def self.for_locale(locale)
case locale.to_sym
# NOTE: add locales here in ealphabetical order
when :az, :de, :en, :es, :fr, :id, :pt, :ru
when :az, :de, :en, :es, :fr, :id, :pt, :ru, :it
[Object.const_get("Humanize::#{locale.capitalize}"), SPACE]
when :th
[Humanize::Th, EMPTY]
Expand Down
2 changes: 1 addition & 1 deletion lib/humanize/locales.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Humanize
%w[az de en es fr id pt ru th tr].each do |locale|
%w[az de en es fr id pt ru th tr it].each do |locale|
autoload locale.capitalize.to_sym, "humanize/locales/#{locale}.rb"
end
end
10 changes: 10 additions & 0 deletions lib/humanize/locales/constants/it.rb

Large diffs are not rendered by default.

53 changes: 53 additions & 0 deletions lib/humanize/locales/it.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
require_relative 'constants/it'

module Humanize
class It
def humanize(number)
iteration = 0
parts = []
use_and = false
until number.zero?
number, remainder = number.divmod(1000)
if remainder > 0
add_grouping(parts, use_and, iteration)
parts << SUB_ONE_GROUPING[remainder]
end

iteration += 1
end

used_lots = LOTS & parts.map {|part| part.delete(',')}.compact
used_lots.each do |lot|
i = (parts.index("#{lot},") || parts.index("#{lot}"))
prefix = parts[i+1]
parts -= [prefix]

parts[i] = if lot == LOTS[1]
prefix == SUB_ONE_GROUPING[1] ? "mille" : "#{prefix}#{lot}"
else
plural = lot[0...-1] + 'i'
prefix == SUB_ONE_GROUPING[1] ? "un #{lot}" : "#{prefix} #{plural}"
end

parts[i] += ',' if i + 1 != parts.size
end

parts
end

private

def conjunction(parts, use_and)
return '' if parts.empty?
use_and ? ' e' : ','
end

def add_grouping(parts, use_and, iteration)
grouping = LOTS[iteration]
return unless grouping

parts << "#{grouping}#{conjunction(parts, use_and)}"
end

end
end
64 changes: 64 additions & 0 deletions spec/locales/it_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
require 'spec_helper'

RSpec.describe Humanize, "it locale" do
# rubocop:disable Metrics/LineLength

before do
Humanize.configure do |config|
config.default_locale = :it
end
end

it_tests = [
[-1, "meno uno"],
[-0.042, "meno zero punto zero quattro due"],
[-0.00003345, "meno zero punto zero zero zero zero tre tre quattro cinque"],
[-0.0000017854, "meno zero punto zero zero zero zero zero uno sette otto cinque quattro"],
[0.000000123, "zero punto zero zero zero zero zero zero uno due tre"],
[0, "zero"],
[8.15, "otto punto uno cinque"],
[8.000015, "otto punto zero zero zero zero uno cinque"],
[8, "otto"],
[11, "undici"],
[21, "ventuno"],
[61, "sessantuno"],
[71, "settantuno"],
[99, "novantanove"],
[100, "cento"],
[101, "centouno"],
[111, "centoundici"],
[121, "centoventuno"],
[167, "centosessantasette"],
[1000, "mille"],
[1079, "mille settantanove"],
[1577, "mille cinquecentosettantasette"],
[2000, "duemila"],
[10_000, "diecimila"],
[10_079, "diecimila settantanove"],
[50_877, "cinquantamila ottocentosettantasette"],
[100_444, "centomila quattrocentoquarantaquattro"],
[235_409, "duecentotrentacinquemila quattrocentonove"],
[1_007_310, "un milione settemila, trecentodieci"],
[8_017_878, "otto milioni diciassettemila, ottocentosettantotto"],
[21_007_310, "ventuno milioni settemila, trecentodieci"],
[321_007_310, "trecentoventuno milioni settemila, trecentodieci"],
[4_321_007_310, "quattro miliardi trecentoventuno milioni, settemila, trecentodieci"],
[14_321_007_310, "quattordici miliardi trecentoventuno milioni, settemila, trecentodieci"],
[1_112_176_514_321_007_310, "un quintilione centododici quadrilioni, centosettantasei trilioni, cinquecentoquattordici miliardi, trecentoventuno milioni, settemila, trecentodieci"],
[15_000_000_000_000_000_333, "quindici quintilioni trecentotrentatre"],
[111_000_000_000_000_000_000_000, "centoundici sestilioni"],
]
# rubocop:enable Metrics/LineLength

it_tests.each do |num, output|
it "#{num} is equal to #{output}" do
expect(num.humanize).to eq(output)
end
end

describe 'when called on bigdecimal' do
it 'reads correctly' do
expect(BigDecimal(it_tests.last.first).humanize).to eql(it_tests.last.last)
end
end
end