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

Gettext translations cached after switching locale? #24

Closed
armandsar opened this issue Jun 21, 2018 · 4 comments
Closed

Gettext translations cached after switching locale? #24

armandsar opened this issue Jun 21, 2018 · 4 comments

Comments

@armandsar
Copy link

Hi.
I have a problem with translations being cached.
Behaviour in rails console, works fine:

I18n.locale = :en

_('Dashboard')
=> "Dashboard"

I18n.locale = :lv

_('Dashboard')
=> "Sākums"

In controller action (via binding pry):

[1] pry> I18n.locale = "en"
=> "en"
[2] pry)> _('Dashboard')
=> "Dashboard"
[3] pry> I18n.locale = "lv"
=> "lv"
[4] pry> _('Dashboard')
=> "Dashboard"

There is no problem if locale is set only once per request (we do this in before action in base controller).
But in this controller action we need part of code within a block
I18n.with_locale(locale) executed with different locale.
Inspecting I18n.locale and GetText.locale within the block we see that locale is switched correctly, but translations appear cached.
Calling ::GetText::TextDomainManager.clear_caches does not help.

@MichaelHoste
Copy link
Member

Hi @armandsar,

I tried to reproduce without success. I put that piece of code in a controller and it worked as expected:

def my_action
  I18n.locale = :fr
  puts _("Assigned")

  I18n.locale = :en
  puts _("Assigned")

  I18n.with_locale(:fr) do
    puts _("Assigned")
  end

  I18n.with_locale(:en) do
    puts _("Assigned")
  end
end

The output in the console was :

Assigné
Assigned
Assigné
Assigned

I tried in development with config.action_controller.perform_caching as true and false (we never know ^^). It was in a Rails 4 application.

What exactly is your stack so I could try to reproduce with the same one. Does it break also in development environment or only in production?

@MichaelHoste
Copy link
Member

I just tested the same example on a Rails 5.0.6 application and it worked too.

@armandsar
Copy link
Author

Simplest bit of code i got to replicate this:

class TestController < ActionController::Base

  before_action :set_default_locale

  def index
    a = []
    a << [_('Dashboard'), I18n.locale]
    I18n.locale = 'lv'
    a << [_('Dashboard'), I18n.locale]
    render json: a
  end

  private

  def set_default_locale
    I18n.locale    = 'en'
    GetText.locale = 'en'
  end

end

Remove "GetText.locale = 'en'" line, reload page, translation still wrong.
Restart the server without gettext line and all is fine.

I now do realize that there is no need to set locale for GetText directly and I didn't really notice this until now, as this was implemented by another developer.

Still the behavior seems strange, given that we do call "I18n.locale = 'lv'" in controller action later..

I guess this can be closed :)

@MichaelHoste
Copy link
Member

I found out why it wasn't working: https://github.com/ruby-gettext/gettext/blob/master/lib/gettext.rb#L261-L282

GetText.locale= is the same as set_locale and fix the locale once for all the application (see the comments).

Gettext.current_locale= is the same as set_current_locale and is more flexible (many locales in the application).

This comment says all: # Note that if #set_locale is set, this value is ignored.

I tried your example with `current_locale=" and it's working. But as you noticed, Translation gem takes care of that for you so you don't have to worry about configuring the GetText locale: https://github.com/translation/rails/blob/master/lib/translation_io/railtie.rb#L21

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