# How to set the default language in GetText?

**URL:** <https://www.ruby-forum.com/t/how-to-set-the-default-language-in-gettext/94587>\
**Category:** Rails\
**Created:** [May 8, 2006, 8:46am UTC](https://www.ruby-forum.com/t/how-to-set-the-default-language-in-gettext/94587 "2006-05-08T08:46:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![charlie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d6d6ee/32.png) [@charlie](https://www.ruby-forum.com/u/charlie)\
**Post date:** [May 8, 2006, 8:46am UTC](https://www.ruby-forum.com/t/how-to-set-the-default-language-in-gettext/94587/1 "2006-05-08T08:46:05Z")

</div>

How to set the default language in GetText?  
The default language of GetText is en,and I want to change it to another  
language,and also keep the ability to change to any other languages by  
just click the according href.  
Currently I use:  
class ApplicationController \< ActionController::Base  
before\_filter :set\_charset  
def set\_charset  
@headers[“Content-Type”] = “text/html; charset=utf-8”  
cookies[“lang”] = ‘zh\_CN’  
end  
end  
But the language will not switch to my want until the next refresh.Could  
I set the default language from en to others in some way?

Thanks  
Charlie

---

<div class="post-metadata">

**Author:** ![charlie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d6d6ee/32.png) [@charlie](https://www.ruby-forum.com/u/charlie)\
**Post date:** [May 8, 2006, 3:26pm UTC](https://www.ruby-forum.com/t/how-to-set-the-default-language-in-gettext/94587/2 "2006-05-08T15:26:03Z")

</div>

Hi,

On Mon, 8 May 2006 08:46:05 +0200  
charlie [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> How to set the default language in GetText?  
> The default language of GetText is en,and I want to change it to another  
> language,and also keep the ability to change to any other languages by  
> just click the according href.

Here is a sample of application.rb:

require ‘gettext/rails’  
class ApplicationController \< ActionController::Base  
prepend\_before\_filter :default\_locale  
def default\_locale  
if (cookies[“lang”].nil? or cookies[“lang”].empty?)  
GetText.locale = “zh\_CN”  
else  
GetText.locale = cookies[“lang”]  
end  
end  
init\_gettext “yourapp”  
end

> Currently I use:  
> class ApplicationController \< ActionController::Base  
> before\_filter :set\_charset  
> def set\_charset  
> @headers[“Content-Type”] = “text/html; charset=utf-8”  
> cookies[“lang”] = ‘zh\_CN’  
> end  
> end  
> But the language will not switch to my want until the next refresh.Could  
> I set the default language from en to others in some way?

Because you call “set\_charset” method after calling init\_gettext.

BTW, you shouldn’t set Content-Type by yourself with  
Ruby-GetText-Package.  
It may cause other problem.

HTH,

---

<div class="post-metadata">

**Author:** ![charlie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d6d6ee/32.png) [@charlie](https://www.ruby-forum.com/u/charlie)\
**Post date:** [May 9, 2006, 3:38am UTC](https://www.ruby-forum.com/t/how-to-set-the-default-language-in-gettext/94587/3 "2006-05-09T03:38:25Z")

</div>

Very grateful!

Masao M. wrote:

> Hi,  
> Here is a sample of application.rb:

> BTW, you shouldn’t set Content-Type by yourself with  
> Ruby-GetText-Package.  
> It may cause other problem.
> 
> HTH,
