ruby on rails - I18n.locale getting reset to :en in between controller and the view -
ruby on rails - I18n.locale getting reset to :en in between controller and the view -
i'm trying internationalize rails/spree app using spree's own spree_i18n
gem, can't work.
i made minimal app recreates problem here.
to cutting long story short, have next code in applicationcontroller:
before_action :set_locale def set_locale i18n.locale = params[:locale] || i18n.default_locale puts i18n.locale end
and code in view should translated (<%= t("whatever") %>
). no matter do, text output in english.
with additional code debugging, can see 1 time set_locale
called while execution still within controller, locale right (e.g. if visit url /?locale=es
, puts
statement in above controller code outputs es
).
but time execution has reached view, locale has somehow been reset en
. (e.g. adding <% raise i18n.locale.to_s %>
within view raises "en" error message.)
i've opened issue on spree's github because far can tell i've followed instructions , it's still not working, may still missing something. why isn't locale getting set properly?
(note: should add together spree.t
doesn't work either, not t
.)
edit: if @ comment on github issue, you'll see got working. however, i'm 99% sure solution hack , there's improve method should using. bounty goes whoever can tell me i'm dong wrong.
spree i18n gives way set default language: on config/application.rb
config.i18n.default_locale = :es
and possibility of setting languages changed. perhaps on config/initializers/spree_i18n.rb
spreei18n::config.available_locales = [:en, :es, :de] spreei18n::config.supported_locales = [:en, :es, :de]
after that, can remove set_locale
on applicationcontroller, because has no effect.
with @ place, works charm.
edited:
i alter message of error because want sure works:
<%= product_description(@product) rescue spree.t(:product_has_no_description) + ' ' + spree.t(:action) %>
and add together new product without description. running server @ localhost
in english language see: "this product has no description action"
in spanish see: "este producto no tiene descripción acción"
in deutsch see: "produkt hat keine beschreibung aktion"
exactly expected.
you can see source changes @ github
ruby-on-rails internationalization locale spree rails-i18n
Comments
Post a Comment