不在Rails 3.1.3中设置默认语言环境

时间:2012-02-13 14:51:34

标签: ruby-on-rails-3 internationalization i18n-gem

未在Rails 3.1.3中设置default_locale

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)

5 个答案:

答案 0 :(得分: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 !@matched_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

答案 1 :(得分:2)

我不认为这是它,但也许你在你的环境文件中覆盖了config.i18n.default_locale,即。 environments/development.rb

一个好的起点是通过grepping查找I18n.localeI18n.default_locale和相关值的所有出现。我将从grep 'locale' app -rn开始(r表示递归,n表示行号),然后在项目根目录中再次执行。

答案 2 :(得分:1)

将此行application.rb添加到config.i18n.locale = :ca,Rails将设置默认语言环境。它在生产服务器中为我工作,但我设置了:ru区域设置

答案 3 :(得分:1)

我正在学习铁轨。我正在使用Rails进行实用的敏捷Web开发。 我只是尝试并尝试使本地化工作,它只是没有工作。过了一段时间,我注意到在我的测试中,语言环境是正确的。奇怪的。 然后我意识到该视图已被缓存。因此,尝试删除rails_app_path / tmp中的文件。

干杯!

答案 4 :(得分:0)

您的问题可能与使用Gettext有关。如果我没有弄错,它使用自己的机制来存储和获取语言环境,我想它根本不遵守任何default_locale设置。如果你们有些人来到这里,因为你们遇到类似的问题而没有Gettext。对于你们,我可以说至少使用最新稳定的rails版本default_locale可以正常工作。您可能已重新定义它,或者在某处明确设置区域设置。 Grey你的项目参考:

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

或smhtn ..

另一件需要记住的事情是,新的default_locale设置可能需要重启服务器才能生效。