Skip to content

Commit

Permalink
Merge pull request #86 from skyporter/swiss-french-support
Browse files Browse the repository at this point in the history
Add Swiss French support
  • Loading branch information
radar authored Apr 14, 2024
2 parents 84ee123 + dfc8531 commit c21a72e
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ Currently supported locales:
* English: `:en`
* Spanish: `:es`
* French: `:fr`
* Swiss French: `:'fr-CH'`
* Turkish: `:tr`
* Azerbaijani `:az`
* German: `:de`
Expand Down
2 changes: 2 additions & 0 deletions lib/humanize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ def self.for_locale(locale)
[Humanize::Tr, SPACE]
when :jp
[Humanize::Jp, EMPTY]
when :'fr-CH'
[Humanize::FrCh, SPACE]
else
raise "Unsupported humanize locale: #{locale}"
end
Expand Down
4 changes: 2 additions & 2 deletions 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 ms pt ru th tr jp vi].each do |locale|
autoload locale.capitalize.to_sym, "humanize/locales/#{locale}.rb"
%w[az de en es fr fr_ch id ms pt ru th tr jp vi].each do |locale|
autoload locale.split('_').map(&:capitalize).join.to_sym, "humanize/locales/#{locale}.rb"
end
end
10 changes: 10 additions & 0 deletions lib/humanize/locales/constants/fr_ch.rb

Large diffs are not rendered by default.

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

module Humanize
class FrCh
def humanize(number)
iteration = 0
parts = []
until number.zero?
number, remainder = number.divmod(1000)
unless remainder.zero?
add_grouping(parts, iteration, remainder)

parts << SUB_ONE_GROUPING[remainder] unless exactly_one_thousand?(remainder, parts)
end

iteration += 1
end

parts
end

private

def exactly_one_thousand?(remainder, parts)
remainder == 1 && parts.last.to_s.strip == 'mille'
end

def plural_for_lots(remainder, word, iteration)
if remainder > 1 && iteration >= 2
"#{word}s"
else
word
end
end

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

parts << grouping
end
end
end
48 changes: 48 additions & 0 deletions spec/locales/fr_ch_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require 'spec_helper'

RSpec.describe Humanize, "fr CH locale" do
before do
Humanize.configure do |config|
config.default_locale = :'fr-CH'
end
end

tests = [
[71, 'septante-et-un'],
[80.15, 'huitante virgule un cinq'],
[1090, 'mille nonante'],
[2095, 'deux mille nonante-cinq'],
[10_000, 'dix mille'],
[1_000_000, "un million"],
[2_000_000, "deux millions"],
[5_000_000, "cinq millions"],
[1_000_000_000, "un milliard"],
[2_000_000_000, "deux milliards"],
[5_000_000_000, "cinq milliards"]
]

tests.each do |num, output|
it "#{num} equals #{output}" do
expect(num.humanize).to eql(output)
end
end

context 'decimals: number' do
it 'returns the decimals as whole numbers' do
num = 8.15
expect(num.humanize(decimals_as: :number)).to eq('huit virgule quinze')
end
end

describe 'when called on conceptual number' do
it 'reads correctly' do
inf = Float::INFINITY
neg_inf = -inf
nan = inf + neg_inf

expect(inf.humanize).to eql('infini')
expect(neg_inf.humanize).to eql('négatif infini')
expect(nan.humanize).to eql('indéfini')
end
end
end

0 comments on commit c21a72e

Please sign in to comment.