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 handling breaks as soon as route_translator gem is activated #88

Closed
GSI opened this issue Feb 3, 2015 · 4 comments
Closed
Labels

Comments

@GSI
Copy link

GSI commented Feb 3, 2015

Vanilla behaviour:

  1. Set I18n.locale in a controller action
  2. Call the according action from within a test
  3. After the above call, notice that the I18n.locale now equals the one set in the controller action

When adding gem 'route_translator', '4.0.0' to Gemfile, the I18n.locale is back to default instead (step 3).

I'll add a basic rails project that reproduces the error.

@GSI
Copy link
Author

GSI commented Feb 3, 2015

Error reproduced in https://github.com/GSI/route_translator-88

@tagliala
Copy link
Collaborator

Hmm... this should not be an error... route_translator runs your controller code inside a block and then restores I18n.locale

you could skip route_translator in this way

class StaticController < ApplicationController

  skip_around_action :set_locale_from_url, only: [:unlocalized]

  def unlocalized
    I18n.locale = params[:locale]
  end

end

and your test will pass

$ rake test
# Running:

.

Finished in 0.072595s, 13.7751 runs/s, 27.5501 assertions/s.

But... I advise you to use around_action for your purposes

class StaticController < ApplicationController

  skip_around_action :set_locale_from_url, only: [:unlocalized]
  around_action :set_locale_from_param, only: [:unlocalized]

  private
  def set_locale_from_param
    I18n.with_locale params[:locale] do
      yield
    end
  end
end

Your test will fail, but you will not experience stuff like #44.

@tagliala
Copy link
Collaborator

Related comments: #44 (comment) #44 (comment)

@tagliala
Copy link
Collaborator

tagliala commented Apr 9, 2018

Closing here

@tagliala tagliala closed this as completed Apr 9, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants