minting-rails 0.8.1 → 0.8.2

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: dfed7ac7c21d15b7b983eac0bd391ec8f4eb4f1f89b1cb9ae0308701dd15ba76
4
- data.tar.gz: da2eb9d34f56f26a71674a45a792b922adb57d0ff9d6a5098eb96d2e3cf5394a
3
+ metadata.gz: 6c6f9a0ca2ddc8795a648b6c6047728d626106e2afddbe63869ad3159fcd819e
4
+ data.tar.gz: 863f9135a359162e053f988e991a9ac2a832e56e310cc1c7511410212e7c0427
5
5
  SHA512:
6
- metadata.gz: bca70a3dc33e70b65635e1dddf9a435ee718b5d6e06e26262fdc53b8d7a9c7e3b7534b88e4ee064cdc6015c2c2772d7941a9482787e96d84397c8a74ec8cd51e
7
- data.tar.gz: 73ef1dcbedaa6cbbf832d37b450c08af1ebf41c12d47a76eb1a884a571ba869c1fbebb89be24a0422a855e53e245c53cd64811466b7a73ef0fa7de5878aa0791
6
+ metadata.gz: 148ee6a06dde29d5685c47e5c3e284fdce46c7d75cd84ab265760087f1e3fcc43afb9f60e949bbe615cf311b576bb9a6180569f103c5665a137d10849604e42c
7
+ data.tar.gz: 7c0eaca1345709f22bb446764b9220c5767cca2998e0570490871555cce3c2d692e5839eea9eedcae282cdbe032dca603d1a89bb8ba61d358aa725e83b26b883
data/README.md CHANGED
@@ -86,6 +86,51 @@ end
86
86
 
87
87
  See the [Minting gem](https://github.com/gferraz/minting) for full configuration options (custom currencies, formatting, rounding).
88
88
 
89
+ ### I18n / Locale-aware formatting
90
+
91
+ Minting-rails integrates with Rails I18n to automatically format money amounts according to the current locale.
92
+
93
+ With `I18n.locale` set to `:en`:
94
+ ```ruby
95
+ Mint.money(1234.56, 'USD').to_s # => "$1,234.56"
96
+ ```
97
+
98
+ Switch to `:'pt-BR'` and the separators change automatically (requires [`rails-i18n`](https://github.com/svenfuchs/rails-i18n) or your own locale file):
99
+ ```ruby
100
+ I18n.locale = :'pt-BR'
101
+ Mint.money(1234.56, 'USD').to_s # => "$1.234,56"
102
+ ```
103
+
104
+ The locale backend reads `number.currency.format` from your I18n translations and maps Rails format syntax (`%n` for amount, `%u` for unit) to `Mint::Money#to_s`. If the translation key is missing (no locale file for that language), it falls back to hardcoded defaults (`.` decimal, `,` thousand, `%<symbol>s%<amount>f` format).
105
+
106
+ You can configure per-sign formatting by adding `positive`, `negative`, and `zero` keys to your locale:
107
+
108
+ ```yaml
109
+ # config/locales/minting-rails.en.yml
110
+ en:
111
+ number:
112
+ currency:
113
+ format:
114
+ format: "%u%n" # fallback when no per-sign key matches
115
+ positive: "%u%n" # "$1,234.56"
116
+ negative: "(%u%n)" # "($1,234.56)"
117
+ zero: "--" # "--"
118
+ separator: "."
119
+ delimiter: ","
120
+ ```
121
+
122
+ When any of `positive`, `negative`, or `zero` is present, a Hash format is built. Missing keys fall back to `format`:
123
+
124
+ ```ruby
125
+ Mint.money(1234.56, 'USD').to_s # => "$1,234.56"
126
+ Mint.money(-1234.56, 'USD').to_s # => "($1,234.56)"
127
+ Mint.money(0, 'USD').to_s # => "--"
128
+ ```
129
+
130
+ If none of those keys are set, `format` is used as a plain string (simple formatting).
131
+
132
+ > Formatting respects the currency's own `subunit` for decimal precision — `I18n` locale settings for `precision` are ignored since that is a currency property, not a locale one.
133
+
89
134
  ## Usage — Two modes
90
135
 
91
136
  ### Decision table
@@ -411,7 +456,7 @@ Minting-rails is intentionally minimal — it focuses on storing and reading mon
411
456
  | **Mongoid support** | Yes | ActiveRecord only |
412
457
  | **Migration helpers** | `add_monetize :products, :price` | None |
413
458
  | **View helpers** | `humanized_money`, `money_without_cents`, etc. | None |
414
- | **I18n / locale files** | Built-in locale-aware formatting | None |
459
+ | **I18n / locale files** | Locale-aware formatting via I18n `number.currency.format` — reads your existing translations, no extra setup | Built-in locale-aware formatting with bundled translations |
415
460
  | **Test matcher** | `monetize(:price_cents)` RSpec matcher | None |
416
461
  | **Currency exchange** | `default_bank`, `add_rate`, EuCentralBank | None |
417
462
  | **Custom currencies** | `register_currency` for non-ISO codes | Via `minting` gem config |
@@ -429,7 +474,6 @@ If you need any of these features today, money-rails may be a better fit. mintin
429
474
  1. **Allow nil** — `money_attribute :price, currency: 'USD', allow_nil: true`
430
475
  1. **Method-level currency** — lambda-based currency resolution for multi-tenant and instance-level scenarios
431
476
  1. **Migration helper**
432
- 1. **Internationalization**
433
477
 
434
478
  Contributions and suggestions are welcome — open an issue or PR at [gferraz/minting-rails](https://github.com/gferraz/minting-rails).
435
479
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Mint
4
4
  module MoneyAttribute
5
- VERSION = '0.8.1'
5
+ VERSION = '0.8.2'
6
6
  end
7
7
  end
@@ -7,6 +7,30 @@ module Mint
7
7
  end
8
8
 
9
9
  config.after_initialize do
10
+ setup_locale_backend!
11
+ register_custom_currencies!
12
+ end
13
+
14
+ def self.setup_locale_backend!
15
+ Mint.locale_backend = lambda {
16
+ fmt = I18n.t('number.currency.format', default: {})
17
+ translator = ->(s) { s&.gsub('%n', '%<amount>f')&.gsub('%u', '%<symbol>s') }
18
+
19
+ format = if fmt.key?(:positive) || fmt.key?(:negative) || fmt.key?(:zero)
20
+ {
21
+ positive: translator.call(fmt[:positive] || fmt[:format]),
22
+ negative: translator.call(fmt[:negative] || fmt[:format]),
23
+ zero: translator.call(fmt[:zero] || fmt[:format])
24
+ }
25
+ else
26
+ translator.call(fmt[:format])
27
+ end
28
+
29
+ { decimal: fmt[:separator], thousand: fmt[:delimiter], format: format }
30
+ }
31
+ end
32
+
33
+ def self.register_custom_currencies!
10
34
  Array(Mint.config.added_currencies).each do |currency_data|
11
35
  if currency_data.respond_to?(:values_at)
12
36
  code = currency_data[:currency] || currency_data['currency']
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: minting-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.1
4
+ version: 0.8.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gilson Ferraz