Question

I'm working with an application with three languages initially. I've made a switcher that works OK (in development and in production) but redirecting to the home page.

I just want the switcher to redirect to the current page, so I've been dealing with that and I solved it in development mode but when I deploy to heroku, it loose even the translations.

What am I missing? I'm very junior with Rails...

This is my code in application_controller.rb:

class ApplicationController < ActionController::Base

    before_action :set_i18n_locale_from_params
    before_action :redirect_to_locale
    # ...
    # Prevent CSRF attacks by raising an exception.
    # For APIs, you may want to use :null_session instead.
    protect_from_forgery with: :exception

    # ...

    protected
    def set_i18n_locale_from_params
        if params[:set_locale]
            if I18n.available_locales.map(&:to_s).include?(params[:set_locale])
                I18n.locale = params[:set_locale]
            else
                flash.now[:notice] =
                    "#{params[:set_locale]} translation not available"
                logger.error flash.now[:notice]
            end
        end
    end

    def default_url_options
        { locale: I18n.locale }     
    end

    helper_method :current_path
    def current_path
        url = request.env["PATH_INFO"]
        Rails.application.routes.recognize_path url
    end

    def redirect_to_locale
        if params[:set_locale].present?
            route = current_path
            route[:locale] = I18n.locale
            redirect_to route
        end
    end

end

and this is the switcher in the application.html.erb:

            <div id="languages">
                <%= I18n.locale %>
                <%= form_tag current_path, class: 'locale', :method => :get do %>
                    <%= select_tag 'set_locale',
                        options_for_select(LANGUAGES, I18n.locale.to_s),
                        onchange: 'this.form.submit()' %>
                    <%= submit_tag 'submit' %>
                    <%= javascript_tag "$('.locale input').hide()" %>
                <% end %>
            </div>

Any help will be appreciate! Thanks.

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top