문제

I am adding I18N to my rails application by passing the locale using url params. My urls are looking like http://example.com/en/users and http://example.com/ar/users (for the english and arabic locales respectively).

In my routes file, I have defined my routes with a :path_prefix option:

map.resources :users, :path_prefix => '/:locale'

And locale is being set using a before_filter defined in ApplicationController

def set_locale
    I18n.locale = params[:locale]
end

I also defined ApplicationController#default_url_options, to add locale to all urls generated by the application:

def default_url_options(options={})
    {:locale => I18n.locale}
end

What I want is to add a link in the layout header (displayed in all pages) that would link to the same page but with the other locale.

For instance, if I am browsing the arabic locale, I want a "English" link in the header, that will redirect me back to my current page, and set the locale to english. Is there a way to do this in rails?

도움이 되었습니까?

해결책

window.onbeforeunload 이벤트에 연결할 수 있고 사용자가 페이지를 남기려면 사용자에게 경고 할 수 있습니다.

window.onbeforeunload = confirmExit;
function confirmExit(){       
    return 'you have unsaved work. are you sure?';
}
.

다른 팁

So I found a way to more explicitly do this with out relying on (as much) rails magic.

url_for(params.merge({:your_new_parameter => value}))

This should work in any link_to.

All its doing is taking the current request's parameters and merging your new desired hash into them and then creating a new url for that.

무작위로 3 일 후 이메일 경고가 작동 중입니다 ... 사용자 만 변경했습니다 :

  • 전자 메일 주소를 포함하도록 광고 사용자 개체 업데이트
  • stsadm -o sync -synctiming m : 5 실행
  • stsadm -o sync-sweeptiming m : 5를 실행합니다.

You can parse request_uri, and replace your locale in the path with regular expression

Ok, here is helper example. If I correctly understand the goal

def locale_url(url, locale)
  url.gsub(/\/\w*$/, "/#{locale}")
end

url = "http://www.domain.com/products/1/ru" # or request.request_uri
locale = "en"
locale_url(url, locale) #=> "http://www.domain.com/products/1/en"

This is a start point, so you can make some different stuff that you need

Have a look at this, though it may not be DRY and proper one, but works perfectly for me. It reads all the parameters you supplied replacing only the locale EX urls : http://example.com:3000/us/users?t=123&m=343 etc

  def us_link           
        link_to "US", form_locale_url("/us")            
  end

  def jp_link           
    link_to "Japan",form_locale_url("/jp")           
  end              

  def form_locale_url(locale)            
    new_url = request.request_uri          
    new_locale_url = new_us_url = new_jp_url = new_url           
    if new_url == "/"          
      new_locale_url.sub!(/\//,locale)           
    elsif (new_url =~/\/us/) == 0        
      new_us_url.sub!(/\/us/,locale)        
    elsif (new_url =~/\/jp/) == 0          
      new_jp_url.sub!(/\/jp/,locale)       
    end     
  end

A much quicker avenue - and convenient if you have many parameters that change in different places... avoid the clutter with an anchor tag that just merges the new locale param to the existing ones (and actually killing the old locale param).

<%= link_to "ру", request.params.merge( locale: 'ru' ) %>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top