Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 3 在RubyonRails3中使用URL切换语言_Ruby On Rails 3_Url_Internationalization_Locale - Fatal编程技术网

Ruby on rails 3 在RubyonRails3中使用URL切换语言

Ruby on rails 3 在RubyonRails3中使用URL切换语言,ruby-on-rails-3,url,internationalization,locale,Ruby On Rails 3,Url,Internationalization,Locale,我有一个法语和英语的网站。我希望用户能够在点击标题中的链接时动态切换语言。很简单 我遵循了Ruby on Rails 3指南,我有以下几点: class ApplicationController < ActionController::Base before_filter :set_locale def set_locale I18n.locale = params[:locale] || I18n.default_locale end protect_from_forgery

我有一个法语和英语的网站。我希望用户能够在点击标题中的链接时动态切换语言。很简单

我遵循了Ruby on Rails 3指南,我有以下几点:

class ApplicationController < ActionController::Base
before_filter :set_locale
def set_locale
    I18n.locale = params[:locale] || I18n.default_locale
end

protect_from_forgery

  def default_url_options(options={})
  logger.debug "default_url_options is passed options: #{options.inspect}\n"
  { :locale => I18n.locale }
   end
end
在我看来,我是这样做的:

<% if I18n.locale == I18n.default_locale %>
  <%= link_to "English", '/en'%>

<% else %>
  <%= link_to "Français", '/fr'%>

<%end%>

所有这些都可以在主页中正常工作,但如果我在特定控制器中尝试切换语言,就会返回主页。我试图补充这一点:

<% if I18n.locale == I18n.default_locale %>
  <%= link_to "English", '/en/' + params[:controller]%>

<% else %>
  <%= link_to "Français", '/fr/' + params[:controller]%>

<%end%>

这修复了控制器,但是如果我们在一个特定的动作中,再次切换语言,我会回到这个控制器的“索引”

我的问题是:处理这个问题的最佳方式是什么


我想我可以在ApplicationController(filter)中实现一些东西来检查是否传递了控制器/action/id,并将其附加到语言环境中。或者我可以在routes.rb中这样做吗?

哦,好吧,终于找到了一种我喜欢的方式

<% if I18n.locale == I18n.default_locale %>
  <%= link_to "English", { :locale=>'en' } %>

<% else %>
  <%= link_to "Français", { :locale=>'fr' } %>

<%end%>

'en'}%>
'fr'}%>
就这么简单


Ruby on Rails万岁

如果您有这样的范围路由覆盖所有本地化路由:

scope "/:locale" do
  ...
end
如果您位于该范围路由内的页面中,例如“/en/…”,则可以创建链接以更改区域设置,而无需更改当前站点位置,如下所示:

scope "(:locale)", :locale => /en|fr/ do

match 'home' => 'home#index'
match 'home/ajax_twitter' => 'home#ajax_twitter'
match 'equipe' => 'equipe#index'
match 'equipe/sylvain' => 'equipe#sylvain'
match 'equipe/benoit' => 'equipe#benoit'
match 'equipe/stephane' => 'equipe#stephane'
match 'equipe/suemarie' => 'equipe#suemarie'
match 'equipe/regis' => 'equipe#regis'
match 'equipe/fred' => 'equipe#fred'

match 'equipe/callback' => 'equipe#callback'
match 'equipe/auth' => 'equipe#auth'
match 'equipe/ajax_contact' => 'equipe#ajax_contact'

match 'linkedinauth/callback' => 'linkedinAuth#callback'
match 'linkedinauth/init_auth' => 'linkedinAuth#init_auth'

match 'mission' => 'mission#index'
match 'service' => 'service#index'
match 'developmen' => 'developmen#index'

match 'contact' => 'contact#index'

match 'mandats' => 'mandats#index'
end

match '/:locale' => "home#index"
<%= link_to "Français", params.merge({:locale => :fr}) %>
<%= link_to "English", params.merge({:locale => :en}) %>
:fr})%>
:en})%%>

假设您需要所有语言的选择器。例如,您在
en
locale中,您还有
fr
es
locale

应用程序中\u helper.rb
(或在/app/helpers/中的任何其他帮助程序)

现在,在视图中的任意位置:

<% alternate_locales do |l| %>
   <li class="language <%= l %>"><%= link_to(l,locale: l) %></li>
<% end %>
另外,如果您希望在标题中包含链接[rel=“alternate”]:

<% alternate_locales do |l| %>
  <link rel="alternate" hreflang="<%= l %>" href="<%= url_for(locale: l) %>">
<% end %> 

这将产生:

<li class="language fr"><a href="www.example.com/fr/path/">fr</a></li>
<li class="language es"><a href="www.example.com/es/path/">es</a></li>
<li class="language en"><a href="www.example.com/en/path/">en</a></li>
<link rel="alternate" hreflang="fr" href="www.example.com/fr/path/">
<link rel="alternate" hreflang="es" href="www.example.com/es/path/">
<link rel="alternate" hreflang="en" href="www.example.com/en/path/">

我相信这样会更干净。下面是:

config/application.rb

I18n.available_locales = [:en, :pt, :es, :de]
def change_locale
    l = params[:locale].to_s.strip.to_sym
    l = I18n.default_locale unless I18n.available_locales.include?(l)
    cookies.permanent[:my_locale] = l
    redirect_to request.referer || root_url
end
before_action :set_locale

def set_locale
      if cookies[:my_locale] && I18n.available_locales.include?(cookies[:grolla_locale].to_sym)
        l = cookies[:my_locale].to_sym
      else
        l = I18n.default_locale
        cookies.permanent[:my_locale] = l
      end
      I18n.locale = l
    end
config/locales/:locale.yml

别忘了在每个locale.yml文件上添加这个,这就是在不同位置之间更改时如何转换到t(lang)的
链接

en:
  en: "English"
  pt: "Portuguese"
  es: "Spanish"
  de: "German"
将区域设置列表添加到html:

<a class="dropdown-toggle" data-toggle="dropdown" href="#">
    <%= t(I18n.locale) %> # Current selected locales
    <span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
    <li class="header">Select a language</li>
        <% I18n.available_locales.each do |lang| %> # Array of available locales
        <li>
        <%= link_to t(lang), change_locale_path(lang) %> # Each locale URL param and name
        </li>
        <% end %>
</ul>
app/controllers/index\u controller.rb

I18n.available_locales = [:en, :pt, :es, :de]
def change_locale
    l = params[:locale].to_s.strip.to_sym
    l = I18n.default_locale unless I18n.available_locales.include?(l)
    cookies.permanent[:my_locale] = l
    redirect_to request.referer || root_url
end
before_action :set_locale

def set_locale
      if cookies[:my_locale] && I18n.available_locales.include?(cookies[:grolla_locale].to_sym)
        l = cookies[:my_locale].to_sym
      else
        l = I18n.default_locale
        cookies.permanent[:my_locale] = l
      end
      I18n.locale = l
    end
app/controllers/application\u controller.rb

I18n.available_locales = [:en, :pt, :es, :de]
def change_locale
    l = params[:locale].to_s.strip.to_sym
    l = I18n.default_locale unless I18n.available_locales.include?(l)
    cookies.permanent[:my_locale] = l
    redirect_to request.referer || root_url
end
before_action :set_locale

def set_locale
      if cookies[:my_locale] && I18n.available_locales.include?(cookies[:grolla_locale].to_sym)
        l = cookies[:my_locale].to_sym
      else
        l = I18n.default_locale
        cookies.permanent[:my_locale] = l
      end
      I18n.locale = l
    end

由于某些原因,使用符号(例如
:en
)会导致后续单击时出现
ActionController::RoutingError(没有路由匹配{…})
(在Rails 3.2.12中测试)。使用字符串解决了这个问题(
:locale=>“en”
)。这是一个更有用的答案,因为它处理现有的上下文,即参数。并将它们与区域设置合并,区域设置是一个元级别的参数。。。