2012-02-13 70 views
3

不設置DEFAULT_LOCALE用Rails 3.1.3不設置默認語言環境中的Rails 3.1.3

application.rb中:

config.i18n.default_locale = :en 

我有3周的語言環境(EN,ES和CA) 當我去我的控制檯:

I18n.locale => :ca # I guess the first one in the array 

我正在使用Gettext。有人知道它可能是什麼?

軟件包:

Using rake (0.9.2.2) 
Using Ascii85 (1.0.1) 
Using ZenTest (4.5.0) 
Using multi_json (1.0.4) 
Using activesupport (3.1.3) 
Using builder (3.0.0) 
Using i18n (0.6.0) 
Using activemodel (3.1.3) 
Using erubis (2.7.0) 
Using rack (1.3.6) 
Using rack-cache (1.1) 
Using rack-mount (0.8.3) 
Using rack-test (0.6.1) 
Using hike (1.2.1) 
Using tilt (1.3.3) 
Using sprockets (2.0.3) 
Using actionpack (3.1.3) 
Using mime-types (1.17.2) 
Using polyglot (0.3.3) 
Using treetop (1.4.10) 
Using mail (2.3.0) 
Using actionmailer (3.1.3) 
Using arel (2.2.1) 
Using tzinfo (0.3.31) 
Using activerecord (3.1.3) 
Using activeresource (3.1.3) 
Using addressable (2.2.6) 
Using adyen (1.3.0) 
Using airbrake (3.0.5) 
Using ansi (1.3.0) 
Using autotest (4.4.6) 
Using xml-simple (1.1.1) 
Using aws-ses (0.4.4) 
Using bcrypt-ruby (3.0.1) 
Using bluecloth (2.2.0) 
Using bulksms (0.5.2) 
Using bundler (1.0.21) 
Using highline (1.6.2) 
Using net-ssh (2.2.1) 
Using net-scp (1.0.4) 
Using net-sftp (2.0.5) 
Using net-ssh-gateway (1.1.0) 
Using capistrano (2.9.0) 
Using capistrano-ext (1.2.1) 
Using nokogiri (1.5.0) 
Using ffi (1.0.9) 
Using childprocess (0.2.2) 
Using json_pure (1.6.1) 
Using rubyzip (0.9.4) 
Using selenium-webdriver (2.8.0) 
Using xpath (0.1.4) 
Using capybara (1.1.1) 
Using carrierwave (0.5.8) 
Using chunky_png (1.2.5) 
Using coffee-script-source (1.1.2) 
Using execjs (1.2.9) 
Using coffee-script (2.2.0) 
Using fssm (0.2.7) 
Using sass (3.1.10) 
Using compass (0.12.alpha.0) 
Using compass-960-plugin (0.10.4) 
Using rest-client (1.6.7) 
Using couchrest (1.1.2) 
Using couchrest_model (1.1.2) 
Using daemons (1.0.10) 
Using diff-lcs (1.1.3) 
Using e164 (0.3.1) 
Using excon (0.7.12) 
Using factory_girl (2.3.0) 
Using rack-ssl (1.3.2) 
Using json (1.6.5) 
Using rdoc (3.12) 
Using thor (0.14.6) 
Using railties (3.1.3) 
Using factory_girl_rails (1.4.0) 
Using multipart-post (1.1.3) 
Using faraday (0.7.5) 
Using faraday_middleware (0.7.0) 
Using fast_gettext (0.6.2) 
Using formatador (0.2.1) 
Using ruby-hmac (0.4.0) 
Using fog (1.1.1) 
Using rails (3.1.3) 
Using formtastic (2.0.0.rc5) 
Using gem_plugin (0.2.3) 
Using geocoder (1.1.0) 
Using geokit (1.6.0) 
Using locale (2.0.5) 
Using gettext (2.1.0) 
Using gettext_i18n_rails (0.3.0) 
Using gravatar (1.0) 
Using guard (1.0.0) 
Using guard-rspec (0.6.0) 
Using haml (3.1.4) 
Using haml-rails (0.3.4) 
Using hashie (1.1.0) 
Using jquery-rails (1.0.17) 
Using kaminari (0.12.4) 
Using kgio (2.6.0) 
Using launchy (2.0.5) 
Using libv8 (3.3.10.2) 
Using metaclass (0.0.1) 
Using subexec (0.1.0) 
Using mini_magick (3.3) 
Using mocha (0.10.0) 
Using money (4.0.1) 
Using mongrel (1.2.0.pre2) 
Using multi_xml (0.4.1) 
Using nifty-generators (0.4.6) 
Using ruby-rc4 (0.1.3) 
Using pdf-reader (1.0.0) 
Using ttfunk (1.0.3) 
Using prawn (0.12.0) 
Using rack-protection (1.1.4) 
Using raindrops (0.8.0) 
Using redis (2.2.2) 
Using redis-namespace (1.0.3) 
Using sinatra (1.3.1) 
Using vegas (0.1.8) 
Using resque (1.19.0) 
Using resque_mailer (2.0.2) 
Using rspec-core (2.8.0) 
Using rspec-expectations (2.8.0) 
Using rspec-mocks (2.8.0) 
Using rspec (2.8.0) 
Using rspec-rails (2.8.1) 
Using ruby-ole (1.2.11.2) 
Using sexp_processor (3.0.7) 
Using ruby_parser (2.3.1) 
Using sass-rails (3.1.5) 
Using simple_oauth (0.1.5) 
Using spreadsheet (0.6.5.9) 
Using state_machine (1.0.2) 
Using steak (2.0.0) 
Using therubyracer (0.9.8) 
Using turn (0.8.3) 
Using twitter (1.7.2) 
Using uglifier (1.0.3) 
Using unicorn (4.1.1) 
Using useragent (0.4.6) 

回答

1

添加到application.rb此行config.i18n.locale = :ca和Rails將設置默認語言環境。它在生產服務器上爲我工作,但我設置:ru區域設置

+0

嘿,這確實工作。但我真的不喜歡這個解決方案。因爲default_locale是要設置的變量...無論如何,我接受你的答案:) – 2012-02-26 21:27:08

+0

weired的東西,但它的作品! – CodeGroover 2013-06-24 00:14:39

+2

將不適用於我 – Uko 2013-08-08 16:12:54

8

你去掉下面行的?

config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s] 

Rails將僅從該位置接收已翻譯文本。但這是Rails特定的翻譯寶石。如果要設置默認語言環境的gettext需要設置如下

將它添加到environment.rb中

I18n.supported_locales = Dir[File.join(RAILS_ROOT, "locale/*")].collect{|v| File.basename(v)} 

測試在Rails控制檯

::I18n.locale = 'en' 

您可以通過此驗證下面的gettext方法

GetText.locale.to_s 

基本上我通過設置I18n.locale = 'en'或任何其他語言環境。您也可以根據瀏覽器首選語言設置默認語言。您還可以通過傳遞lang ='ca'等來強制語言在(gettext)中設置區域設置。您只需使用:before_filter即可。調用下面的方法,在過濾器

def set_default_language 
    languages = get_browser_preferred_languages_hash(request.env["HTTP_ACCEPT_LANGUAGE"]) 
    @locale  = languages.blank? ? 'en' : app_preferred_language(languages, params[:lang])[0..1] 
    I18n.locale = @locale || 'en' 
end 

    def get_browser_preferred_languages_hash(languages) 
    # no language accepted 
    return [] if languages.blank? 
    # parse Accept-Language 
    accepted = languages.split(",") 
    accepted = accepted.map { |l| l.strip[0..1]} 
    end 

    def app_preferred_language(languages,forced_lang) 
    unless forced_lang.blank? 
     return forced_lang.class == Array ? forced_lang[0] : forced_lang  
    end 
    return 'en' if languages.blank? 
    @matched_lang = '' 
    languages.each do |lang| 
     @matched_lang = support_languages_hash.values.detect{|ele| ele[0..1] == lang} 
     break if [email protected]_lang.blank? 
    end 
    return @matched_lang.blank? ? 'en' : @matched_lang 
    end 

    def support_languages_hash 
    GetText.locale 
    { _('English') => 'en-US', 
     _('Spanish') => 'es-419', 
     _('Catalan') => 'ca' } 
    end 
2

我不認爲這是它,但也許你在你的環境文件覆蓋config.i18n.default_locale,即。 environments/development.rb

一個好的起點是通過grepping查找所有I18n.locale,I18n.default_locale和相關值的出現。我會從grep 'locale' app -rn開始(r爲遞歸,n爲顯示行數),然後在項目根目錄中再次執行。

+0

這對我有效!我真的不知道爲什麼,但我必須在我的測試環境中聲明區域設置。 – hcarreras 2015-04-21 13:21:54

0

您的問題可能與使用Gettext有關。如果我沒有弄錯它使用它自己的機制來存儲和獲取區域設置,我想它並不尊重任何default_locale設置。如果你們中有些人因爲沒有Gettext而遇到過類似的問題而來到這裏。對於你們我可以說,至少在最新的stable rails版本中,default_locale工作得很好。您可能重新定義了它,或者在某處明確設置了區域設置。 Grep您的項目以供參考:

grep -rn default_locale . 
grep -rn I18n.locale . 

或smhtn ..

要記住的另一件事是服務器重新啓動可能需要新的default_locale設置才能生效。

1

我正在學習rails。我正在遵循Rails的語用敏捷Web開發。 我只是在努力嘗試使本地化工作,它只是沒有工作。過了一段時間,我注意到在我的測試中,語言環境是正確的。奇怪的。 然後我意識到視圖被緩存了。所以嘗試刪除rails_app_path/tmp中的文件。

乾杯!

+0

我剛剛花了大概2個小時撞牆我試圖找出爲什麼'config.i18n.default_locale'沒有在我的應用程序中受到尊重。這是固定它。荒謬。但是,謝謝。 – 2014-05-23 10:37:20

相關問題