Skip to content

Commit

Permalink
Merge pull request #87 from koei1025/TraditionalChinese-
Browse files Browse the repository at this point in the history
add new language: Traditional Chinese
  • Loading branch information
radar authored May 8, 2024
2 parents c21a72e + d9c09f9 commit 7bfa222
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ Currently supported locales:
* Malaysia `:ms`
* Japanese `:jp`
* Vietnamese `:vi`
* Traditional Chinese `:zh-tw`

## Testing

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 :'zh-tw'
[Humanize::ZhTw, EMPTY]
when :'fr-CH'
[Humanize::FrCh, SPACE]
else
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 fr_ch id ms pt ru th tr jp vi].each do |locale|
%w[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
10 changes: 10 additions & 0 deletions lib/humanize/locales/constants/zh_tw.rb

Large diffs are not rendered by default.

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

module Humanize
class ZhTw
def humanize(number)
iteration = 0
parts = []
until number.zero?
number, remainder = number.divmod(10_000)
unless remainder.zero?
add_grouping(parts, iteration)
parts << SUB_ONE_GROUPING[remainder]
end

iteration += 1
end

parts
end

private

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

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

RSpec.describe Humanize, 'zh tw locale' do
before do
Humanize.configure do |config|
config.default_locale = :'zh-tw'
end
end

tests = [
[1, '壹'],
[48, '肆拾捌'],
[100, '佰'],
[101, '佰壹'],
[110, '佰拾'],
[199, '佰玖拾玖'],
[999, '玖佰玖拾玖'],
[1000, '壹仟'],
[3456, '参仟肆佰伍拾陸'],
[9999, '玖仟玖佰玖拾玖'],
[21_111, '貳萬壹仟佰拾壹'],
[99_999, '玖萬玖仟玖佰玖拾玖'],
[9_876_543, '玖佰捌拾柒萬陸仟伍佰肆拾参'],
[10_000_000, '壹仟萬'],
[100_000_000, '壹億'],
[765_432_109, '柒億陸仟伍佰肆拾参萬貳仟佰玖'],
[-123_422_223.48_948_753, '負壹億貳仟参佰肆拾貳萬貳仟貳佰貳拾参・肆捌玖肆捌捌']
]

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.1592
expect(num.humanize).to eq('捌・壹伍玖貳')
expect(num.humanize(decimals_as: :number)).to eq('捌・壹仟伍佰玖拾貳')
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('無限大')
expect(neg_inf.humanize).to eql('負無限大')
expect(nan.humanize).to eql('未定義')
end
end
end

0 comments on commit 7bfa222

Please sign in to comment.