最初は 3 つの言語のアプリケーションを使用しています。正常に動作するスイッチャーを作成しました (開発中および運用中) が、ホームページにリダイレクトします。
スイッチャーを現在のページにリダイレクトさせたいだけなので、それを処理して開発モードで解決しましたが、herokuにデプロイすると、翻訳も失われます。
私は何が欠けていますか?私はRailsで非常に後輩です...
これは、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
これは、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>
どんな助けでも感謝します!ありがとう。