human_number 0.1.7 → 0.1.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3bc432b4b82fa70b18804f6db6080c10cf33c95e410c7ee9bb2335c99b053f8d
4
- data.tar.gz: e2aa145ec5319d06c8ff98748ee8a696f6a0a999a571440931fc55e34681f78d
3
+ metadata.gz: b40f4d307a6d79e38dd1e45276c6df0334d28081dfb3cdddce89d943732e9aab
4
+ data.tar.gz: f25564f84991e593d68de48fd88b79e1ef64baaa473d962428b5008b61a25c93
5
5
  SHA512:
6
- metadata.gz: 123d58896c01e0297e7dde721df49e8036e4ced81185b508aa34750cf6b869487449b454fa9149d6816a05284f058bdf8ec6bf674ae52fbee6487ac880b8af42
7
- data.tar.gz: e58469487228d201d424006a2d9a75aec5e8c73dedc3a7580c3e0ed716b20600d07f1055ec2993668aa38f422237b353e6f24091ba4a789c36ee4924e1546d35
6
+ metadata.gz: 030e86e5855e634e6aafb8940834f58a290276a7c2096e1da12b44eb9bbd667803c1a2239822d091f69aa6fb1165c4eb4187268e0911ee6a78a407f80c1ba6c1
7
+ data.tar.gz: 612b2da8d6f9abbd5d06695701e5b2943879d4a8610885f717f2acfe28577fba1e082597a1dee03c07464e58e4996cf00a3c78e54b8def050cdfa1750a74395d
data/CHANGELOG.md CHANGED
@@ -5,7 +5,31 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
- ## [0.1.0] - 2024-01-31
8
+ ## [0.1.9] - 2025-08-05
9
+
10
+ ### Changed
11
+ - **East Asian separator formatting**: Japanese numbers now use culturally appropriate spacing
12
+ - Japanese: `'12億3456万7890円'` (no spaces between units)
13
+ - Korean/Chinese: `'12억 3456만 7890원'` (spaces maintained as default)
14
+ - **Locale-based separator configuration**: Added `number.human.decimal_units.separator` for customizable unit separators
15
+
16
+ ## [0.1.8] - 2025-08-05
17
+
18
+ ### Fixed
19
+ - **I18n fallback contamination**: Resolved currency symbol cross-contamination where Korean won symbols (원) appeared for other currencies like CAD due to I18n fallback behavior
20
+ - **Translation missing errors**: Fixed missing translation errors for various currency/locale combinations
21
+ - **Rails configuration warnings**: Eliminated Rails configuration warnings in test environment
22
+
23
+ ### Changed
24
+ - **Currency native locale mappings**: Corrected CURRENCY_NATIVE_LOCALES to use only accurate locale associations (e.g., CAD now maps to en-CA/fr-CA only, not generic en/fr)
25
+ - **Locale file organization**: Moved locale files from `config/locales/` to `lib/locales/` for better gem structure
26
+
27
+ ### Improved
28
+ - **Rails-i18n integration**: Enhanced loading order and initialization to prevent locale conflicts
29
+ - **Test infrastructure**: Updated RSpec mocks and test expectations for better reliability
30
+ - **I18n isolation**: Added `fallback: false` parameter to prevent cross-locale pollution
31
+
32
+ ## [0.1.0] - 2025-07-31
9
33
 
10
34
  ### Added
11
35
 
@@ -86,4 +110,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
86
110
  - **Formatters::Currency**: Currency symbol and format string application
87
111
  - **Clean separation**: Number formatting independent of currency concerns
88
112
 
113
+ [0.1.9]: https://github.com/ether-moon/human_number/releases/tag/v0.1.9
114
+ [0.1.8]: https://github.com/ether-moon/human_number/releases/tag/v0.1.8
89
115
  [0.1.0]: https://github.com/ether-moon/human_number/releases/tag/v0.1.0
data/README.md CHANGED
@@ -158,9 +158,14 @@ HumanNumber.human_number(1_000_000_000, locale: :en) #=> "1B"
158
158
  HumanNumber.human_number(1_234_567, locale: :ko) #=> "120만"
159
159
  HumanNumber.human_number(1_234_567, locale: :ja) #=> "120万"
160
160
  HumanNumber.human_number(100_000_000, locale: :ko) #=> "1억"
161
+
162
+ # Complete mode shows cultural spacing differences
163
+ HumanNumber.human_number(12_345_678, locale: :ko, max_digits: nil) #=> "1234만 5678" # Korean: spaces
164
+ HumanNumber.human_number(12_345_678, locale: :ja, max_digits: nil) #=> "1234万5678" # Japanese: no spaces
161
165
  ```
162
166
 
163
- **Units:** 만/万 (ten thousand), 억/億 (hundred million), 조/兆 (trillion)
167
+ **Units:** 만/万 (ten thousand), 억/億 (hundred million), 조/兆 (trillion)
168
+ **Cultural spacing:** Japanese uses no spaces between units, Korean/Chinese use spaces (configurable via locale files)
164
169
 
165
170
  ### Indian System (lakh/crore)
166
171
  **Used by:** Hindi (hi), Urdu (ur), Bengali (bn), Indian English (en-IN)
@@ -419,6 +419,11 @@ module HumanNumber
419
419
  ].freeze
420
420
 
421
421
  class << self
422
+ def format_number(number, locale:, abbr_units: true, min_unit: nil, max_digits: 2, trim_zeros: true)
423
+ @current_locale = locale.to_sym
424
+ super
425
+ end
426
+
422
427
  private
423
428
 
424
429
  def unit_definitions
@@ -437,6 +442,18 @@ module HumanNumber
437
442
  def apply_symbol_formatting_rules(symbol, _abbr_units)
438
443
  symbol
439
444
  end
445
+
446
+ # Apply culturally appropriate separators for East Asian languages
447
+ def finalize_result(number, formatted_parts)
448
+ separator = lookup_decimal_separator(@current_locale)
449
+ combined_result = formatted_parts.join(separator)
450
+ number.negative? ? "-#{combined_result}" : combined_result
451
+ end
452
+
453
+ # Look up separator from locale file, default to space
454
+ def lookup_decimal_separator(locale)
455
+ I18n.t("#{I18N_DECIMAL_UNITS_KEY}.separator", locale: locale, default: SPACE_SEPARATOR)
456
+ end
440
457
  end
441
458
  end
442
459
 
@@ -12,30 +12,30 @@ module HumanNumber
12
12
  CURRENCY_NATIVE_LOCALES = {
13
13
  "KRW" => [:ko],
14
14
  "JPY" => [:ja],
15
- "USD" => [:en],
15
+ "USD" => %i[en en-US],
16
16
  "EUR" => %i[de fr es it nl],
17
- "GBP" => %i[en en-GB],
18
- "CAD" => %i[en en-CA fr fr-CA],
17
+ "GBP" => %i[en-GB],
18
+ "CAD" => %i[en-CA fr-CA],
19
19
  "CNY" => %i[zh zh-CN],
20
20
  "CHF" => %i[de fr it],
21
- "INR" => %i[hi en],
21
+ "INR" => %i[hi en-IN],
22
22
  "RUB" => [:ru],
23
- "BRL" => %i[pt pt-BR],
24
- "MXN" => [:es],
25
- "AUD" => %i[en en-AU],
26
- "NZD" => %i[en en-NZ],
23
+ "BRL" => %i[pt-BR],
24
+ "MXN" => %i[es-MX],
25
+ "AUD" => %i[en-AU],
26
+ "NZD" => %i[en-NZ],
27
27
  "SEK" => [:sv],
28
28
  "NOK" => %i[no nb],
29
29
  "DKK" => [:da],
30
- "SGD" => %i[en en-SG zh zh-SG],
31
- "HKD" => %i[zh zh-HK en en-HK],
32
- "ZAR" => %i[en en-ZA af],
30
+ "SGD" => %i[en-SG zh-SG],
31
+ "HKD" => %i[zh-HK en-HK],
32
+ "ZAR" => %i[en-ZA af],
33
33
  "TRY" => [:tr],
34
- "ARS" => %i[es es-AR],
34
+ "ARS" => %i[es-AR],
35
35
  "IDR" => [:id],
36
36
  "THB" => [:th],
37
- "MYR" => %i[ms en en-MY],
38
- "PHP" => %i[en en-PH tl],
37
+ "MYR" => %i[ms en-MY],
38
+ "PHP" => %i[en-PH tl],
39
39
  "VND" => [:vi],
40
40
  }.freeze
41
41
 
@@ -44,18 +44,6 @@ module HumanNumber
44
44
 
45
45
  module_function
46
46
 
47
- # Returns the primary locale for displaying a currency.
48
- # If the user's locale matches a native locale for the currency, use it.
49
- # Otherwise, use the first native locale for the currency.
50
- #
51
- # @param currency_code [String] ISO 4217 currency code (e.g., 'USD', 'EUR')
52
- # @param user_locale [Symbol] User's preferred locale
53
- # @return [Symbol] Primary locale for currency display
54
- def primary_locale_for_currency(currency_code, user_locale)
55
- locales = native_locales_for_currency(currency_code)
56
- (locales.include?(user_locale.to_sym) ? user_locale.to_sym : locales.first) || :en
57
- end
58
-
59
47
  # Returns the locale to use for currency precision rules in standard formatting.
60
48
  # Always returns the currency's primary native locale to ensure consistent
61
49
  # precision (e.g., USD always shows 2 decimals, JPY shows 0 decimals).
@@ -114,16 +102,23 @@ module HumanNumber
114
102
  return nil if native_locales_for_currency(currency_code).empty?
115
103
 
116
104
  # For native locales and supported keys, try native formatting first
117
- if native_locale?(currency_code, locale) && SUPPORTED_NATIVE_CURRENCY_KEYS.include?(key)
118
- native_key = native_currency_key(key)
119
- return I18n.t(native_key, locale:) if I18n.exists?(native_key, locale)
105
+ # Additional validation: only currencies that actually have native symbols
106
+ if SUPPORTED_NATIVE_CURRENCY_KEYS.include?(key)
107
+ if native_locale?(currency_code, locale)
108
+ native_key = native_currency_key(key)
109
+ return I18n.t(native_key, locale:) if I18n.exists?(native_key, locale, fallback: false)
110
+ else
111
+ # For currency symbols (:unit) and format, use currency's native locale to ensure consistency
112
+ # (e.g., USD always shows "$1,234.56" format, regardless of display locale)
113
+ target_locale = native_locales_for_currency(currency_code).first || :en
114
+ standard_key = currency_key(key)
115
+ return I18n.t(standard_key, locale: target_locale, default: currency_code)
116
+ end
120
117
  end
121
118
 
122
- # Fallback to standard currency formatting using currency's primary locale
123
- currency_locale = primary_locale_for_currency(currency_code, locale)
119
+ # For other keys, use user's requested locale
124
120
  standard_key = currency_key(key)
125
-
126
- I18n.t(standard_key, locale: currency_locale)
121
+ I18n.t(standard_key, locale:)
127
122
  end
128
123
  end
129
124
  end
@@ -16,8 +16,8 @@ module HumanNumber
16
16
  end
17
17
  end
18
18
 
19
- initializer "human_number.load_locale_data" do
20
- I18n.load_path += Dir[File.expand_path("../../config/locales/*.yml", __dir__)]
19
+ initializer "human_number.load_locale_data", after: "rails-i18n" do
20
+ I18n.load_path += Dir[File.expand_path("../locales/*.yml", __dir__)]
21
21
  end
22
22
  end
23
23
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module HumanNumber
4
- VERSION = "0.1.7"
4
+ VERSION = "0.1.9"
5
5
  end
data/lib/human_number.rb CHANGED
@@ -4,9 +4,6 @@ require "i18n"
4
4
  require "action_view"
5
5
  require "rails-i18n"
6
6
 
7
- # Ensure Rails I18n locales are available
8
- I18n.load_path += Dir[File.expand_path("**/*.yml", Gem.loaded_specs["rails-i18n"].full_gem_path)]
9
-
10
7
  require_relative "human_number/version"
11
8
  require_relative "human_number/locale_support"
12
9
  require_relative "human_number/formatters/number"
@@ -15,9 +12,6 @@ require_relative "human_number/formatters/currency"
15
12
  module HumanNumber
16
13
  class Error < StandardError; end
17
14
 
18
- # Load locale files
19
- I18n.load_path += Dir[File.expand_path("../config/locales/*.yml", __dir__)]
20
-
21
15
  extend ActionView::Helpers::NumberHelper
22
16
 
23
17
  class << self
@@ -213,4 +207,9 @@ module HumanNumber
213
207
  end
214
208
  end
215
209
 
210
+ # Load locale files for all environments to ensure availability
211
+ # locale_files = Dir[File.expand_path("locales/*.yml", __dir__)]
212
+ # I18n.load_path += locale_files unless (I18n.load_path & locale_files).any?
213
+ # I18n.backend.reload! if I18n.backend.respond_to?(:reload!)
214
+
216
215
  require_relative "human_number/railtie" if defined?(Rails::Railtie)
@@ -8,6 +8,7 @@ ja:
8
8
  unit: "¥"
9
9
  human:
10
10
  decimal_units:
11
+ separator: ""
11
12
  abbr_units:
12
13
  thousand: "千"
13
14
  ten_thousand: "万"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: human_number
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ether Moon
@@ -77,20 +77,6 @@ files:
77
77
  - Makefile
78
78
  - README.md
79
79
  - Rakefile
80
- - config/locales/bn.yml
81
- - config/locales/de.yml
82
- - config/locales/en-GB.yml
83
- - config/locales/en-IN.yml
84
- - config/locales/en.yml
85
- - config/locales/es.yml
86
- - config/locales/fr.yml
87
- - config/locales/hi.yml
88
- - config/locales/ja.yml
89
- - config/locales/ko.yml
90
- - config/locales/ur.yml
91
- - config/locales/zh-CN.yml
92
- - config/locales/zh-TW.yml
93
- - config/locales/zh.yml
94
80
  - human_number.gemspec
95
81
  - lib/human_number.rb
96
82
  - lib/human_number/formatters/currency.rb
@@ -99,6 +85,20 @@ files:
99
85
  - lib/human_number/rails/helpers.rb
100
86
  - lib/human_number/railtie.rb
101
87
  - lib/human_number/version.rb
88
+ - lib/locales/bn.yml
89
+ - lib/locales/de.yml
90
+ - lib/locales/en-GB.yml
91
+ - lib/locales/en-IN.yml
92
+ - lib/locales/en.yml
93
+ - lib/locales/es.yml
94
+ - lib/locales/fr.yml
95
+ - lib/locales/hi.yml
96
+ - lib/locales/ja.yml
97
+ - lib/locales/ko.yml
98
+ - lib/locales/ur.yml
99
+ - lib/locales/zh-CN.yml
100
+ - lib/locales/zh-TW.yml
101
+ - lib/locales/zh.yml
102
102
  - scripts/build_and_publish.sh
103
103
  homepage: https://github.com/ether-moon/human_number
104
104
  licenses:
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes