Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I18n.locale is wrong after I install this gem. #163

Closed
piya23300 opened this issue Dec 23, 2017 · 19 comments
Closed

I18n.locale is wrong after I install this gem. #163

piya23300 opened this issue Dec 23, 2017 · 19 comments

Comments

@piya23300
Copy link

piya23300 commented Dec 23, 2017

My rails version is 5.1.4 and r18n gem version is

r18n-core (2.2.0)
    r18n-rails (2.2.0)
      r18n-rails-api (= 2.2.0)
    r18n-rails-api (2.2.0)
      i18n
      r18n-core (= 2.2.0)

At application.rb

config.i18n.default_locale = :th

before I install r18n gem

puts I18n.locale => :th

after I installed r18n gem

puts I18n.locale => :"en-US"

Can you help me?

Thank you in advance.

@ai
Copy link
Member

ai commented Dec 23, 2017

What files so you have in config/locales and app/i18n?

@piya23300
Copy link
Author

I have 2 files at config/locales. en.yml and th.yml

@ai
Copy link
Member

ai commented Dec 23, 2017

Strange. Where do you put I18n.locale? What do you have in R18n.get?

@piya23300
Copy link
Author

piya23300 commented Dec 23, 2017

Where do you put I18n.locale?

I put it on console (rails console). My view is wrong. I try to debug it.

What do you have in R18n.get?

I can call it. it returns a lot of data.

@ai
Copy link
Member

ai commented Dec 23, 2017

Show me R18n.get.code

@piya23300
Copy link
Author

#<R18n::Untranslated:0x007fbaf2251f70 @translated_path="", @untranslated_path="code", @locale=Locale en-US (American English), @filters=#<R18n::GlobalFilterList:0x007fbaefcf2a18>>

@ai
Copy link
Member

ai commented Dec 23, 2017

What locale will you have in Rails controller in HTTP request from browser with th locale?

@piya23300
Copy link
Author

I don't send any locale.

Started GET "/pet_cards/new" for 127.0.0.1 at 2017-12-23 17:57:40 +0700
Processing by PetCardsController#new as HTML
  Rendering pet_cards/new.html.slim within layouts/application
  Rendered pet_cards/_form.html.slim (17.2ms)
  Rendered pet_cards/new.html.slim within layouts/application (25.1ms)
Completed 200 OK in 690ms (Views: 43.8ms | ActiveRecord: 0.0ms)

@ai
Copy link
Member

ai commented Dec 23, 2017

Put I18n.locale to your controller code or in your view file

@piya23300
Copy link
Author

I18n.locale => en-US
18n.default_locale => th

@ai
Copy link
Member

ai commented Dec 23, 2017

What HTTP_ACCEPT_LANGUAGES header do you have in your browser?

Maybe your can create small Rails project to reproduce the problem, do I will not ask you for many tests 😅?

@piya23300
Copy link
Author

{"X-Frame-Options"=>"SAMEORIGIN", "X-XSS-Protection"=>"1; mode=block", "X-Content-Type-Options"=>"nosniff"}

I just create the project 1 week ago - -"

@piya23300
Copy link
Author

piya23300 commented Dec 23, 2017

my repo https://github.com/piya23300/hopet_r18n

URL FOR TEST http://localhost:3000/pet_cards/new

all translations is correct except prompt of select

I expect

At locales/th.yml

datetime:
    prompts:
      day: -- เลือกวัน --
      hour: -- เลือกชั่วโมง --
      minute: -- เลือกนาที --
      month: -- เลือกเดือน --
      second: -- เลือกวินาที --
      year: -- เลือกปี --

but it shows

Day
Month
Year

screenshot-localhost-3000-2017-12-23-18-44-05-343

@ai
Copy link
Member

ai commented Dec 29, 2017

Here are two different problems:

  1. Locale in rails console
  2. Locale in controller during a web request.

Let’s fix the first one first. Here is current R18n locale choosing for rails console:

  1. Take ENV['LANG'].
  2. If ENV['LANG'] is missed, use I18n.default_locale.

So, what is your operation system language? Show puts ENV['LANG']

@piya23300
Copy link
Author

irb(main):001:0> ENV['LANG']
=> "en_US.UTF-8"

Oh, I see. >< but I think the gem should use I18n.default_locale the same rails to be a default locale.

http://guides.rubyonrails.org/i18n.html#configure-the-i18n-module

The I18n library will use English as a default locale, i.e. if a different locale is not set, :en will be used for looking up translations.

Thank you to help me and Happy New Year 2018 🎉🎉🎉

@ai
Copy link
Member

ai commented Jan 1, 2018

Good question. R18n Rails is compatibility layout. But it is not 100% the same since it try to improve I18n 😊.

Because current R18n behavior is public API I can't change it without major version.

Do you really need Rails behavior? I can give you small snippet to ignore ENV.

@piya23300
Copy link
Author

piya23300 commented Jan 1, 2018

Yes, I need it.

because I think gems shouldn't change the behavior of default Rails configuration.

@ai
Copy link
Member

ai commented Jan 1, 2018

Create config/initializers/r18n_fix.rb and put this code:

ActiveSupport.on_load(:after_initialize) do
  locale = I18n.default_locale.to_s
  if defined? Rails::Console and not defined? Wirble
    i18n   = R18n::I18n.new(locale, R18n.default_places,
                            off_filters: :untranslated,
                            on_filters:  :untranslated_bash)
    R18n.set(i18n)
  else
    R18n.set(locale)
  end
end

it will remove ENV usage.

@piya23300
Copy link
Author

Thank you so much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants