Rails语言执着?语言环境总是路由到 default_locale
Rails language persistent? Locale always routing to default_locale
我最近开始使用 Rails 和 I18n。
似乎在我的应用程序中,当我发送一个 Http 请求以从一个端点移动到另一个端点时,所选的 I18n 参数不是持久的。
即
我被重定向到默认语言 http://localhost:4000/en
我点击一个按钮"Español"切换语言偏好
我被发送到 http://localhost:4000/es
页面上的文字是西班牙语
(到目前为止表现符合预期)
我点击一个按钮进入应用程序的另一个页面,即 "video"
我被重定向到 http://localhost:4000/en/video
页面文字为英文
可以看出,语言首选项 "es" 消失了,现在我被重定向回默认 "en"。我的 Rails 版本是 5.1。
这是我的 routes.rb
Rails.application.routes.draw do
scope ":locale", locale: /#{I18n.available_locales.join("|")}/ do
root 'pages#home'
get 'accommodation' => 'pages#accommodation'
get 'afterparty' => 'pages#afterparty'
post 'afterparty' => 'pages#create_afterparty'
get 'afterparty/index' => 'pages#afterparty_index'
get 'registry' => 'pages#registry'
get 'rsvp' => 'pages#rsvp'
post 'rsvp' => 'pages#create'
get 'rsvp/index' => 'pages#rsvp_index'
get 'video' => 'pages#video'
end
match '*path', to: redirect("/#{I18n.locale}/%{path}"), :via => [:get, :post]
match '', to: redirect("/#{I18n.locale}"), :via => [:get, :post]
end
这是我的 application_controller.rb:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :set_locale
private
def set_locale
I18n.locale = params[:locale] if params[:locale].present?
end
def default_url_options(options = {})
{locale: I18n.locale}
end
end
这是我在初始化程序目录中定义的locale.rb:
# Where the I18n library should search for translation files
I18n.load_path += Dir[Rails.root.join('lib', 'locale', '*.{rb,yml}')]
# Permitted locales available for the application
I18n.available_locales = [:en, :es]
# Set default locale to something other than :en
# I18n.default_locale = :es
这是 application.html 的摘录,其中显示了语言环境参数分配:
<%= t('.language') %>
<%= link_to_unless_current "English", locale: "en" %> |
<%= link_to_unless_current "Español", locale: "es" %>
这是 application.html 的摘录,它举例说明了将您重定向到不同页面的 2 个锚标记:
<a class="names" href='/'><h4>Christina & Alex</h4></a>
<a class="section" href='/video'>VIDEO</a>
我尝试了几件事都没有成功。请帮助澄清我错在哪里。谢谢。
您的应用程序中的 link 和按钮似乎没有包含来自 default_url_options
的语言环境。
例如,您的 link:
<a class="section" href='/video'>VIDEO</a>
不使用语言环境参数。
如果您使用了 rails 助手,它将:
<%= link_to 'VIDEO', video_path, class: 'section' %>
因为 *_path
和 *_url
助手将合并 default_url_options
,
最终的 HTML 将如下所示:
<a class="section" href='/video?locale=es'>VIDEO</a>
当然,您也可以自己添加语言环境参数。
我最近开始使用 Rails 和 I18n。
似乎在我的应用程序中,当我发送一个 Http 请求以从一个端点移动到另一个端点时,所选的 I18n 参数不是持久的。
即
我被重定向到默认语言 http://localhost:4000/en
我点击一个按钮"Español"切换语言偏好
我被发送到 http://localhost:4000/es
页面上的文字是西班牙语
(到目前为止表现符合预期)
我点击一个按钮进入应用程序的另一个页面,即 "video"
我被重定向到 http://localhost:4000/en/video
页面文字为英文
可以看出,语言首选项 "es" 消失了,现在我被重定向回默认 "en"。我的 Rails 版本是 5.1。 这是我的 routes.rb
Rails.application.routes.draw do
scope ":locale", locale: /#{I18n.available_locales.join("|")}/ do
root 'pages#home'
get 'accommodation' => 'pages#accommodation'
get 'afterparty' => 'pages#afterparty'
post 'afterparty' => 'pages#create_afterparty'
get 'afterparty/index' => 'pages#afterparty_index'
get 'registry' => 'pages#registry'
get 'rsvp' => 'pages#rsvp'
post 'rsvp' => 'pages#create'
get 'rsvp/index' => 'pages#rsvp_index'
get 'video' => 'pages#video'
end
match '*path', to: redirect("/#{I18n.locale}/%{path}"), :via => [:get, :post]
match '', to: redirect("/#{I18n.locale}"), :via => [:get, :post]
end
这是我的 application_controller.rb:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :set_locale
private
def set_locale
I18n.locale = params[:locale] if params[:locale].present?
end
def default_url_options(options = {})
{locale: I18n.locale}
end
end
这是我在初始化程序目录中定义的locale.rb:
# Where the I18n library should search for translation files
I18n.load_path += Dir[Rails.root.join('lib', 'locale', '*.{rb,yml}')]
# Permitted locales available for the application
I18n.available_locales = [:en, :es]
# Set default locale to something other than :en
# I18n.default_locale = :es
这是 application.html 的摘录,其中显示了语言环境参数分配:
<%= t('.language') %>
<%= link_to_unless_current "English", locale: "en" %> |
<%= link_to_unless_current "Español", locale: "es" %>
这是 application.html 的摘录,它举例说明了将您重定向到不同页面的 2 个锚标记:
<a class="names" href='/'><h4>Christina & Alex</h4></a>
<a class="section" href='/video'>VIDEO</a>
我尝试了几件事都没有成功。请帮助澄清我错在哪里。谢谢。
您的应用程序中的 link 和按钮似乎没有包含来自 default_url_options
的语言环境。
例如,您的 link:
<a class="section" href='/video'>VIDEO</a>
不使用语言环境参数。
如果您使用了 rails 助手,它将:
<%= link_to 'VIDEO', video_path, class: 'section' %>
因为 *_path
和 *_url
助手将合并 default_url_options
,
最终的 HTML 将如下所示:
<a class="section" href='/video?locale=es'>VIDEO</a>
当然,您也可以自己添加语言环境参数。