Skip to content
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
3 changes: 2 additions & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ Humanize.format(0.001) => "zero point zero zero one"

```ruby
Humanize.configure do |config|
config.locale = :en # [:en, :es, :fr, :'fr-CH', :tr, :az, :de, :id, :th, :ru, :pt, :ms, :jp, :vi, :'zh-tw'], default: :en
config.locale = :en # [:en, :es, :fr, :'fr-CH', :tr, :az, :de, :id, :th, :ru, :pt, :ms, :jp, :vi, :'zh-tw', :ar], default: :en
config.decimals_as = :digits # [:digits, :number], default: :digits
end
```
Expand Down Expand Up @@ -145,6 +145,7 @@ Currently supported locales:
* Japanese `:jp`
* Vietnamese `:vi`
* Traditional Chinese `:zh-tw`
* Arabic `:ar`

## Testing

Expand Down
2 changes: 1 addition & 1 deletion lib/humanize/locales.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

module Humanize
%w[az de en es fr fr_ch id ms pt ru th tr jp vi zh_tw].each do |locale|
%w[ar az de en es fr fr_ch id ms pt ru th tr jp vi zh_tw].each do |locale|
autoload locale.split('_').map(&:capitalize).join.to_sym, "humanize/locales/#{locale}.rb"
end
end
55 changes: 55 additions & 0 deletions lib/humanize/locales/ar.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# frozen_string_literal: true

require_relative 'constants/ar'

module Humanize
class Ar
def humanize(number)
iteration = 0
parts = []
use_and = false
until number.zero?
number, remainder = number.divmod(1000)
unless remainder.zero?
if iteration.zero? && remainder < 100
use_and = true
else
add_grouping(parts, use_and, iteration)
end

parts << SUB_ONE_GROUPING[remainder]
end

iteration += 1
end

# parts
correct_lots(parts)
end

private

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

use_and ? ' و' : ' و'
end

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

return unless grouping

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

def correct_lots(parts)
parts = parts.reverse.join(' ').squeeze(' ')
SPECIFIC_LOTS.each do |wrong, right|
parts = parts.sub(wrong, right) if parts.include?(wrong)
end

parts.split.reverse
end
end
end
17 changes: 17 additions & 0 deletions lib/humanize/locales/constants/ar.rb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/humanize/module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def format(number,
def for_locale(locale)
case locale.to_sym
# NOTE: add locales here in alphabetical order
when :az, :de, :en, :es, :fr, :id, :ms, :pt, :ru, :vi
when :ar, :az, :de, :en, :es, :fr, :id, :ms, :pt, :ru, :vi
[Object.const_get("Humanize::#{locale.capitalize}"), ' ']
when :th
[Humanize::Th, '']
Expand Down
54 changes: 54 additions & 0 deletions spec/locales/ar_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Humanize, 'ar locale' do
before do
Humanize.configure do |config|
config.locale = :ar
end
end

tests = [
[1, 'واحد'],
[11, 'أحد عشر'],
[23, 'ثلاثة وعشرون'],
[102, 'مائة واثنان'],
[233, 'مائتان وثلاثة وثلاثون'],
[678, 'ستمائة وثمانية وسبعون'],
[876, 'ثمانمائة وستة وسبعون'],
[1000, 'ألف'],
[1756, 'ألف و سبعمائة وستة وخمسون'],
[2000, 'ألفان'],
[5000, 'خمسة آلاف'],
[10_000, 'عشرة آلاف'],
[20_000, 'عشرون ألف'],
[202_000, "مائتان وألفان"],
[1_000_000, 'مليون'],
[2_000_000, 'مليونان'],
[3_000_000, 'ثلاثة ملايين'],
[5_000_000, 'خمسة ملايين'],
[9_000_000, 'تسعة ملايين'],
[10_000_000, 'عشرة ملايين'],
[1_000_000_000, 'مليار'],
[9_000_000_000_000, 'تسعة تريليونات']
]

tests.each do |num, output|
it "#{num} equals #{output}" do
expect(num.humanize).to eql(output)
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 eq('لا نهاية')
expect(neg_inf.humanize).to eq('سالب لا نهاية')
expect(nan.humanize).to eq('غير معرف')
end
end
end