Module: Num2words::Locales::ZH
- Defined in:
- lib/num2words/locales/zh.rb
Constant Summary collapse
- ONES_MASC =
I18n.t("num2words.ones_masc", locale: :zh)
- ONES_FEM =
I18n.t("num2words.ones_fem", locale: :zh)
- TEENS =
I18n.t("num2words.teens", locale: :zh)
- TENS =
I18n.t("num2words.tens", locale: :zh)
- HUNDREDS =
I18n.t("num2words.hundreds", locale: :zh)
- SCALES =
I18n.t("num2words.scales", locale: :zh)
- FRACTIONS =
I18n.t("num2words.fractions", locale: :zh)
- GRAMMAR =
I18n.t("num2words.grammar", locale: :zh)
- DATE =
I18n.t("num2words.date", locale: :zh)
- DATE_TEMPLATE =
I18n.t("num2words.date.template", locale: :zh)
- TIME =
I18n.t("num2words.time", locale: :zh)
- TIME_TEMPLATE =
I18n.t("num2words.time.template", locale: :zh)
- DATETIME_TEMPLATE =
I18n.t("num2words.datetime.template", locale: :zh)
- ORDINALS =
I18n.t("num2words.numbers.ordinals", locale: :zh)
- LARGE_SCALES =
["", "万", "亿", "兆", "京"].freeze
- SMALL_UNITS =
["", "十", "百", "千"].freeze
Class Method Summary collapse
- .cardinal(number) ⇒ Object
- .currency_major_feminine?(_currency) ⇒ Boolean
- .currency_minor_feminine?(_currency) ⇒ Boolean
- .currency_number_words(number, _currency, unit:) ⇒ Object
- .date_day(day, format:, date_case:) ⇒ Object
- .date_year(year, format:) ⇒ Object
- .decimal_fraction_words(fraction_string) ⇒ Object
- .decimal_separator_word ⇒ Object
- .default_fraction_word ⇒ Object
- .fraction_joiner(joiner) ⇒ Object
- .fraction_numerator_feminine? ⇒ Boolean
- .integer_to_words(number, feminine: false) ⇒ Object
- .join_currency_parts(parts) ⇒ Object
- .join_decimal_words(words) ⇒ Object
- .join_fraction_words(words) ⇒ Object
- .join_time_words(number_words, unit_words) ⇒ Object
- .lower_nonzero?(digits, position) ⇒ Boolean
- .minus_word ⇒ Object
- .ordinal(value, format, gender: :masculine) ⇒ Object
- .pluralize(number, singular, few, _plural) ⇒ Object
- .time_number_words(number, unit:) ⇒ Object
- .time_unit_feminine?(_unit) ⇒ Boolean
- .triple_to_words(number, scale_idx, feminine: false) ⇒ Object
- .under_ten_thousand(number) ⇒ Object
- .unit_words(digit, position, group_start) ⇒ Object
Class Method Details
.cardinal(number) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/num2words/locales/zh.rb', line 119 def cardinal(number) integer_value = Integer(number) negative = integer_value.negative? integer_value = integer_value.abs return ONES_MASC[0] if integer_value.zero? groups = integer_value.to_s .chars.reverse.each_slice(4).map(&:reverse) .map(&:join).map!(&:to_i).reverse words = [] zero_between = false groups.each_with_index do |group_value, index| scale_idx = groups.size - index - 1 if group_value.zero? zero_between = words.any? next end words << ONES_MASC[0] if words.any? && (zero_between || group_value < 1000) && words.last != ONES_MASC[0] words << "#{under_ten_thousand(group_value)}#{LARGE_SCALES.fetch(scale_idx, '')}" zero_between = false end words.unshift(minus_word) if negative words.join end |
.currency_major_feminine?(_currency) ⇒ Boolean
94 95 96 |
# File 'lib/num2words/locales/zh.rb', line 94 def currency_major_feminine?(_currency) false end |
.currency_minor_feminine?(_currency) ⇒ Boolean
98 99 100 |
# File 'lib/num2words/locales/zh.rb', line 98 def currency_minor_feminine?(_currency) false end |
.currency_number_words(number, _currency, unit:) ⇒ Object
102 103 104 |
# File 'lib/num2words/locales/zh.rb', line 102 def currency_number_words(number, _currency, unit:) cardinal(number) end |
.date_day(day, format:, date_case:) ⇒ Object
74 75 76 |
# File 'lib/num2words/locales/zh.rb', line 74 def date_day(day, format:, date_case:) cardinal(day) end |
.date_year(year, format:) ⇒ Object
78 79 80 |
# File 'lib/num2words/locales/zh.rb', line 78 def date_year(year, format:) cardinal(year) end |
.decimal_fraction_words(fraction_string) ⇒ Object
59 60 61 |
# File 'lib/num2words/locales/zh.rb', line 59 def decimal_fraction_words(fraction_string) fraction_string.chars.map { |digit| ONES_MASC[digit.to_i] }.join end |
.decimal_separator_word ⇒ Object
55 56 57 |
# File 'lib/num2words/locales/zh.rb', line 55 def decimal_separator_word GRAMMAR[:decimal_separator] || "点" end |
.default_fraction_word ⇒ Object
47 48 49 |
# File 'lib/num2words/locales/zh.rb', line 47 def default_fraction_word GRAMMAR[:default_fraction] end |
.fraction_joiner(joiner) ⇒ Object
43 44 45 |
# File 'lib/num2words/locales/zh.rb', line 43 def fraction_joiner(joiner) joiner.to_sym == :and ? "又" : GRAMMAR[:conjunction] end |
.fraction_numerator_feminine? ⇒ Boolean
51 52 53 |
# File 'lib/num2words/locales/zh.rb', line 51 def fraction_numerator_feminine? false end |
.integer_to_words(number, feminine: false) ⇒ Object
29 30 31 |
# File 'lib/num2words/locales/zh.rb', line 29 def integer_to_words(number, feminine: false) cardinal(number) end |
.join_currency_parts(parts) ⇒ Object
106 107 108 |
# File 'lib/num2words/locales/zh.rb', line 106 def join_currency_parts(parts) parts.join end |
.join_decimal_words(words) ⇒ Object
63 64 65 |
# File 'lib/num2words/locales/zh.rb', line 63 def join_decimal_words(words) words.compact.reject(&:empty?).join end |
.join_fraction_words(words) ⇒ Object
67 68 69 70 71 72 |
# File 'lib/num2words/locales/zh.rb', line 67 def join_fraction_words(words) sign_word, integer_words, conjunction_word, numerator_words, denominator_words = words numerator_words = "" if denominator_words.to_s.end_with?(numerator_words.to_s) [sign_word, integer_words, conjunction_word, denominator_words, numerator_words].compact.reject(&:empty?).join end |
.join_time_words(number_words, unit_words) ⇒ Object
90 91 92 |
# File 'lib/num2words/locales/zh.rb', line 90 def join_time_words(number_words, unit_words) "#{number_words}#{unit_words}" end |
.lower_nonzero?(digits, position) ⇒ Boolean
178 179 180 |
# File 'lib/num2words/locales/zh.rb', line 178 def lower_nonzero?(digits, position) (0...position).any? { |lower_position| digits[lower_position].to_i.positive? } end |
.minus_word ⇒ Object
39 40 41 |
# File 'lib/num2words/locales/zh.rb', line 39 def minus_word GRAMMAR[:minus] end |
.ordinal(value, format, gender: :masculine) ⇒ Object
110 111 112 113 114 115 116 117 |
# File 'lib/num2words/locales/zh.rb', line 110 def ordinal(value, format, gender: :masculine) ordinals = ORDINALS[format] || ORDINALS[:default] || ORDINALS[:nominative] gender_data = ordinals[gender] || ordinals[:masculine] return gender_data[value - 1] if value.between?(1, gender_data.length) "第#{cardinal(value)}" end |
.pluralize(number, singular, few, _plural) ⇒ Object
182 183 184 |
# File 'lib/num2words/locales/zh.rb', line 182 def pluralize(number, singular, few, _plural) number == 1 ? singular : few end |
.time_number_words(number, unit:) ⇒ Object
86 87 88 |
# File 'lib/num2words/locales/zh.rb', line 86 def time_number_words(number, unit:) cardinal(number) end |
.time_unit_feminine?(_unit) ⇒ Boolean
82 83 84 |
# File 'lib/num2words/locales/zh.rb', line 82 def time_unit_feminine?(_unit) false end |
.triple_to_words(number, scale_idx, feminine: false) ⇒ Object
33 34 35 36 37 |
# File 'lib/num2words/locales/zh.rb', line 33 def triple_to_words(number, scale_idx, feminine: false) return [] if number.zero? ["#{under_ten_thousand(number)}#{LARGE_SCALES[scale_idx]}"] end |
.under_ten_thousand(number) ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/num2words/locales/zh.rb', line 150 def under_ten_thousand(number) digits = number.digits words = [] zero_needed = false (3).downto(0) do |position| digit = digits[position].to_i if digit.zero? zero_needed = words.any? && lower_nonzero?(digits, position) next end words << ONES_MASC[0] if zero_needed words << unit_words(digit, position, words.empty?) zero_needed = false end words.join end |
.unit_words(digit, position, group_start) ⇒ Object
171 172 173 174 175 176 |
# File 'lib/num2words/locales/zh.rb', line 171 def unit_words(digit, position, group_start) return ONES_MASC[digit] if position.zero? return SMALL_UNITS[position] if position == 1 && digit == 1 && group_start "#{ONES_MASC[digit]}#{SMALL_UNITS[position]}" end |