[Ruby] When executing rb file when Rails / Sinatra is not used, i18n gets angry with ʻen is not a valid locale (I18n :: InvalidLocale)'

background

Reason for writing the article

There is nothing you can tell immediately by looking at the official website, Blogs, like Qiita, I couldn't find any non-primary information that emphasized clarity.

Status

When I was executing the section ** Monetize example ** in Metaprogramming Ruby. For such a display.

ruby $ ruby 202010270014.rb
[WARNING] The default rounding mode will change from `ROUND_HALF_EVEN` to `ROUND_HALF_UP` in the next major release. Set it explicitly using `Money.rounding_mode=` to avoid potential problems.
[DEPRECATION] You are using the default localization behaviour that will change in the next major release. Find out more - https://github.com/RubyMoney/money#deprecation
Traceback (most recent call last):
  10: from 202010270014.rb:5:in `<main>'
   9: from /Users/username/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/money-6.13.8/lib/money/money.rb:600:in `format'
   8: from /Users/username/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/money-6.13.8/lib/money/money/formatter.rb:232:in `to_s'
   7: from /Users/username/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/money-6.13.8/lib/money/money/formatter.rb:257:in `format_number'
   6: from /Users/username/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/money-6.13.8/lib/money/money/formatter.rb:333:in `format_whole_part'
   5: from /Users/username/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/money-6.13.8/lib/money/money/formatter.rb:238:in `thousands_separator'
   4: from /Users/username/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/money-6.13.8/lib/money/money/formatter.rb:366:in `lookup'
   3: from /Users/username/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/money-6.13.8/lib/money/locale_backend/legacy.rb:15:in `lookup'
   2: from /Users/username/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/money-6.13.8/lib/money/locale_backend/i18n.rb:19:in `lookup'
   1: from /Users/username/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/i18n-1.6.0/lib/i18n.rb:182:in `translate'
/Users/username/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/i18n-1.6.0/lib/i18n.rb:326:in `enforce_available_locales!': :en is not a valid locale (I18n::InvalidLocale)

Source code

When an error occurs

error.rb


require "monetize"
bargain_price = Monetize.from_numeric(99, "USD")
p bargain_price.format

When the error is resolved

success.rb


require "monetize"
I18n.load_path << Dir[File.expand_path("config/locales") + "/*.yml"]
I18n.default_locale = :en # (note that `en` is already the default!)
bargain_price = Monetize.from_numeric(99, "USD")
p bargain_price.format

I borrowed the 2nd and 3rd lines from the README of Official repository. Rewrite the above source + Place the yml file in config / locales / en.yml. The contents are as follows as the README of the official repository.

en.yml


en:
  test: "This is a test"

Run-time display

ruby $ ruby 202010270014.rb
[WARNING] The default rounding mode will change from `ROUND_HALF_EVEN` to `ROUND_HALF_UP` in the next major release. Set it explicitly using `Money.rounding_mode=` to avoid potential problems.
[DEPRECATION] You are using the default localization behaviour that will change in the next major release. Find out more - https://github.com/RubyMoney/money#deprecation
[DEPRECATION] You are using the default localization behaviour that will change in the next major release. Find out more - https://github.com/RubyMoney/money#deprecation
"$99.00"

WARNING is out, but it worked for the time being.

References / Sites

Metaprogramming Ruby i18n Official Repository

Recommended Posts

[Ruby] When executing rb file when Rails / Sinatra is not used, i18n gets angry with ʻen is not a valid locale (I18n :: InvalidLocale)'
[Ruby on rails] When executing the heroku command, bash: heroku: command not found is displayed. [Rails tutorial]
File path is not recognized when executing an external command from a Java application
What to do when rails creates a 〇〇 2.rb file