Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why don't my locale settings in number_to_currency work?

Per the Rails 3.2 API Docs, to use different locales for number_to_currency, I need to do the following:

<%= number_to_currency(1234567890.506, :locale => :fr) %>

I was expecting the following output:

# => 1 234 567 890,51 €

Even though I literally use that exact thing within my app and it keeps outputting the following:

$1,234,567,890.51

When I check for the available_locales within my app I get the following:

> I18n.available_locales
=> [:en, :de, :es, :fr, :ja, :pl, :"pt-BR", :ru, :sv, :"zh-CN"]

So it SHOULD work, but it doesn't.

What am I missing?

Update 1

Per @s3tjan's comment, I did some digging in that linked Rails issue and that led me to my application.rb where I discovered I18n.enforce_available_locales = false. I changed that to true and restarted the server.

When I tried the above again, I am now getting this error:

ActionView::Template::Error (:fr is not a valid locale):

Not sure how to fix this.

Update 2

So I just realize that I never had a locale file in my config/locales. What I really want is to use the GBP Pounds for currency, so I added an en-GB.yml file in my config/locales, then I restarted my server and console.

In my application.rb, I have the following:

I18n.enforce_available_locales = true

Then I checked my console and got this:

[1] pry(main)> I18n.available_locales
=> [:en, :de, :es, :fr, :ja, :pl, :"pt-BR", :ru, :sv, :"zh-CN", :"en-GB"]
[2] pry(main)> 

So the :"en-GB" was added successfully to my app's load path.

But when I do this in my view:

<%= number_to_currency(1234567890.506, :locale => :"en-GB") %>

This is the error I get:

:"en-GB" is not a valid locale excluded from capture due to environment or should_capture callback

ActionView::Template::Error (:"en-GB" is not a valid locale):

So still not working.

Update 3

My en-GB.yml file was taken directly from https://github.com/svenfuchs/rails-i18n/blob/master/rails/locale/en-GB.yml

So it looks exactly like that. Yet I am still getting the same error:

ActionView::Template::Error (:"en-GB" is not a valid locale):
like image 231
marcamillion Avatar asked Feb 26 '19 22:02

marcamillion


3 Answers

Synopsis:

Remove custom language ymls and add the correct version of the i18n-rails gem. This resolved this special issue.

Original answer:

Ok my guess is that your en-GB.yml is empty. So it actually finds the file and adds the locale in I18n.available_locales BUT this does not include that all translations are available.

When you look at the format of such a yml file you will recognize they all start with

---
language-code
  some_keys: ...

This is what actually is loaded into memory and therefore provides all the available translations. Available locale is just defined by found files in config/locales.

When you check the source of number_to_currency It takes the locale from the options and passes it along the key it looks for to I18n.

I18n.translate(:'number.format', :locale => options[:locale], :default => {})

Since you just say that en-GB is available but don't have the actual keys along the locale in memory you get the missing translation issue.

What I suggest is you either use the content of the linked yml file and paste it into your en-GB.yml or you remove your en-GB.yml and find a 3.2 working i18n-rails version and use it. i18n-rails provides plenty of default translations which are utilized all over default rails.

Addition: Before you added the en-GB.yml file it actually worked like expected. When no locale is found it defaults to dollar in here since the currency variable will be just and empty {}.

like image 120
Denny Mueller Avatar answered Nov 17 '22 04:11

Denny Mueller


Somehow, I found the solution that worked for me.

First of all, you need to have a locale file with your requirements in it.

Here is the example of fr.yml file

For an instance, copy and paste this file in app/config/locales/

then restart your console,

then try, number_to_currency(1000.51, locale: :fr)

for sure, you will get '1 000,51 €'

Here is the full list of all supported countries' locale file.

Until and unless you don't have locale file with your format required in it, you won't get the desired result.

like image 31
Vishal Avatar answered Nov 17 '22 06:11

Vishal


my rails version is 3.2.22.5, ruby is 2.4.2(I didnt install below 2.x.x)

I use locale yml from https://github.com/svenfuchs/rails-i18n/blob/rails-3-x/rails/locale/en-GB.yml

This is work well

 <%= number_to_currency(1234567890.506, :locale => :"en-GB") %>

 <%= number_to_currency(1234567890.506, :locale => "en-GB") %>

to result

£1,234,567,890.51

and I add fr.yml too like

fr:
  ...
  number:
    currency:
      format:
        ...
        unit: €

And then this is work too

<%= number_to_currency(1234567890.506, :locale => :fr) %>

to result

€1,234,567,890.51

I didn't change or add configuration. I add only controller, view and locale file. And test it.

You could translate directly like, so test this

I18n.translate(:'number.currency.format', :locale => "en-GB", :default => {})

if it occur a same error, then check out you file's name, extension, path.

and you must restart server

like image 1
ogelacinyc Avatar answered Nov 17 '22 06:11

ogelacinyc