Module: Num2words::Locales::AR

Defined in:
lib/num2words/locales/ar.rb

Constant Summary collapse

ONES_MASC =
I18n.t("num2words.ones_masc", locale: :ar)
ONES_FEM =
I18n.t("num2words.ones_fem", locale: :ar)
TEENS =
I18n.t("num2words.teens", locale: :ar)
TENS =
I18n.t("num2words.tens", locale: :ar)
HUNDREDS =
I18n.t("num2words.hundreds", locale: :ar)
SCALES =
I18n.t("num2words.scales", locale: :ar)
FRACTIONS =
I18n.t("num2words.fractions", locale: :ar)
GRAMMAR =
I18n.t("num2words.grammar", locale: :ar)
DATE =
I18n.t("num2words.date", locale: :ar)
DATE_TEMPLATE =
I18n.t("num2words.date.template", locale: :ar)
TIME =
I18n.t("num2words.time", locale: :ar)
TIME_TEMPLATE =
I18n.t("num2words.time.template", locale: :ar)
DATETIME_TEMPLATE =
I18n.t("num2words.datetime.template", locale: :ar)
ORDINALS =
I18n.t("num2words.numbers.ordinals", locale: :ar)

Class Method Summary collapse

Class Method Details

.date_day(day, format:, date_case:) ⇒ Object



128
129
130
# File 'lib/num2words/locales/ar.rb', line 128

def date_day(day, format:, date_case:)
  ordinal(day, format)
end

.date_year(year, format:) ⇒ Object



132
133
134
# File 'lib/num2words/locales/ar.rb', line 132

def date_year(year, format:)
  integer_to_words(year)
end

.decimal_fraction_words(fraction_string) ⇒ Object



108
109
110
# File 'lib/num2words/locales/ar.rb', line 108

def decimal_fraction_words(fraction_string)
  fraction_string.chars.map { |digit| integer_to_words(digit.to_i) }.join(" ")
end

.decimal_separator_wordObject



104
105
106
# File 'lib/num2words/locales/ar.rb', line 104

def decimal_separator_word
  GRAMMAR[:conjunction]
end

.default_fraction_wordObject



100
101
102
# File 'lib/num2words/locales/ar.rb', line 100

def default_fraction_word
  GRAMMAR[:default_fraction]
end

.fraction_joiner(joiner) ⇒ Object



96
97
98
# File 'lib/num2words/locales/ar.rb', line 96

def fraction_joiner(joiner)
  joiner.to_sym == :and ? "و" : GRAMMAR[:conjunction]
end

.fraction_numerator_feminine?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/num2words/locales/ar.rb', line 124

def fraction_numerator_feminine?
  false
end

.integer_to_words(number, feminine: false) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/num2words/locales/ar.rb', line 26

def integer_to_words(number, feminine: false)
  integer_value = Integer(number)
  negative = integer_value.negative?
  integer_value = integer_value.abs

  return (feminine ? ONES_FEM[0] : ONES_MASC[0]) if integer_value.zero?

  groups = integer_value.to_s
                        .chars.reverse.each_slice(3).map(&:reverse)
                        .map(&:join).map!(&:to_i).reverse

  words = []
  groups.each_with_index do |group_value, index|
    next if group_value.zero?

    scale_idx = groups.size - index - 1
    words << scale_group_to_words(group_value, scale_idx, feminine: feminine && scale_idx.zero?)
  end

  result = words.join(" و")
  negative ? [minus_word, result].join(" ") : result
end

.join_fraction_words(words) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/num2words/locales/ar.rb', line 112

def join_fraction_words(words)
  parts = words.reject(&:empty?)
  joiner_index = parts.index("و")

  if joiner_index && parts[joiner_index + 1]
    parts[joiner_index + 1] = "و#{parts[joiner_index + 1]}"
    parts.delete_at(joiner_index)
  end

  parts.join(" ")
end

.minus_wordObject



92
93
94
# File 'lib/num2words/locales/ar.rb', line 92

def minus_word
  GRAMMAR[:minus]
end

.ordinal(value, format, gender: :masculine) ⇒ Object



136
137
138
139
140
141
142
143
# File 'lib/num2words/locales/ar.rb', line 136

def ordinal(value, format, gender: :masculine)
  ordinals = ORDINALS[format] || ORDINALS[:default]
  gender_data = ordinals[gender] || ordinals[:masculine]

  return gender_data[value - 1] if value.between?(1, gender_data.length)

  integer_to_words(value)
end

.pluralize(number, singular, dual, plural) ⇒ Object



145
146
147
148
149
150
151
152
# File 'lib/num2words/locales/ar.rb', line 145

def pluralize(number, singular, dual, plural)
  number = number.abs
  return singular if number == 1
  return dual if number == 2
  return plural if number.zero? || number.between?(3, 10)

  singular
end

.scale_group_to_words(number, scale_idx, feminine: false) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/num2words/locales/ar.rb', line 53

def scale_group_to_words(number, scale_idx, feminine: false)
  return under_thousand(number, feminine: feminine) if scale_idx.zero?

  scale_forms = SCALES[scale_idx]
  case number
  when 1
    scale_forms[0]
  when 2
    scale_forms[1]
  else
    scale_form = number.between?(3, 10) ? scale_forms[2] : scale_forms[0]
    [under_thousand(number), scale_form].join(" ")
  end
end

.triple_to_words(number, scale_idx, feminine: false) ⇒ Object



49
50
51
# File 'lib/num2words/locales/ar.rb', line 49

def triple_to_words(number, scale_idx, feminine: false)
  scale_group_to_words(number, scale_idx, feminine: feminine).split
end

.under_hundred(number, feminine: false) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/num2words/locales/ar.rb', line 78

def under_hundred(number, feminine: false)
  ones_data = feminine ? ONES_FEM : ONES_MASC

  return ones_data[number] if number < 10
  return TEENS[number - 10] if number < 20

  tens = number / 10
  ones = number % 10

  return TENS[tens] if ones.zero?

  [ones_data[ones], TENS[tens]].join(" و")
end

.under_thousand(number, feminine: false) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/num2words/locales/ar.rb', line 68

def under_thousand(number, feminine: false)
  hundreds = number / 100
  rest = number % 100

  return under_hundred(rest, feminine: feminine) if hundreds.zero?
  return HUNDREDS[hundreds] if rest.zero?

  [HUNDREDS[hundreds], under_hundred(rest, feminine: feminine)].join(" و")
end