[Ruby] Wenn eine rb-Datei ausgeführt wird, wenn Rails / Sinatra nicht verwendet wird, ärgert sich i18n über "en ist kein gültiges Gebietsschema (I18n :: InvalidLocale)".

Hintergrund

Grund für das Schreiben des Artikels

Auf der offiziellen Website können Sie nichts sofort erkennen. Wie ein Blog oder Qiita Ich konnte keine nicht primären Informationen finden, die Klarheit betonten.

Status

Als ich den Abschnitt ** Beispiel monetarisieren ** in Metaprogramming Ruby ausführte. Für eine solche Anzeige.

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)

Quellcode

Wenn ein Fehler auftritt

error.rb


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

Wenn der Fehler behoben ist

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

Ich habe die 2. und 3. Zeile aus der README-Datei von Official Repository ausgeliehen. Schreiben Sie die obige Quelle neu + Platzieren Sie die yml-Datei in config / locales / en.yml. Der Inhalt ist wie folgt: READ ME des offiziellen Repository.

en.yml


en:
  test: "This is a test"

Laufzeitanzeige

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"

WARNUNG ist aus, aber es hat vorerst funktioniert.

Referenzen / Websites

Metaprogramming Ruby i18n Official Repository

Recommended Posts

[Ruby] Wenn eine rb-Datei ausgeführt wird, wenn Rails / Sinatra nicht verwendet wird, ärgert sich i18n über "en ist kein gültiges Gebietsschema (I18n :: InvalidLocale)".
[Ruby on Rails] Wenn der Befehl heroku ausgeführt wird, wird der Befehl bash: heroku: nicht gefunden angezeigt. [Rails Tutorial]
Der Dateipfad wird nicht erkannt, wenn ein externer Befehl von einer Java-App ausgeführt wird