# Set locale

**URL:** <https://www.ruby-forum.com/t/set-locale/163245>\
**Category:** Ruby-Forum\
**Created:** [March 18, 2009, 4:48pm UTC](https://www.ruby-forum.com/t/set-locale/163245 "2009-03-18T16:48:37Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![giga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b9bd4f/32.png) [@giga](https://www.ruby-forum.com/u/giga)\
**Post date:** [March 18, 2009, 4:48pm UTC](https://www.ruby-forum.com/t/set-locale/163245/1 "2009-03-18T16:48:37Z")

</div>

Is there any beter way to write this?

before\_filter :set\_locale

def set\_locale  
if(params[:locale] and params[:locale].match /^(da|en|de)$/)  
I18n.locale = params[:locale]  
elsif session[:locale] == nil  
xxx = request.env[‘HTTP\_ACCEPT\_LANGUAGE’].scan(/^[a-z]{2}/).first  
if xxx.match /^(da|en|de)$/  
I18n.locale = xxx  
else  
I18n.locale = ‘en’  
end  
else  
I18n.locale = session[:locale] || ‘en’  
end  
session[:locale] = I18n.locale  
end
