0

I'm trying to use this gem to determine the user's preferred language, and running into some trouble.

undefined local variable or method http_accept_language for #<HomeController:0x964f5ec>

I included the gem in the Gemfile, ran bundle install, and restarted the server multiple times. Why doesn't my app recognize the gem?

Also, in my ApplicationController I wrote the following method:

def set_i18n_locale
 http_accept_language.user_preferred_languages
 available = %w{en kr}
 params[:locale] = http_accept_language.preferred_language_from(available)
    if params[:locale]
        I18n.locale = params[:locale]
    end
end

One thing I don't understand is the second line, http_accept_language.user_preferred_languages

From https://github.com/iain/http_accept_language, this is supposed to return a sorted array. I thought I had to store the array into some variable and use it, but the author just throws the method like that. How does that work? Can't I just do the following?

available = %w{en kr}
params[:locale] = http_accept_language.language_region_compatible_from(available)

I am just a little confused by the author's explanation.

Thank you for your help.

UPDATE: the gem, http_accept_language, doesn't seem to be installed successfully. It's on the gem list, but when I try to uninstall it, the error message shows that it's not installed. Why does this happen?

max@max-VirtualBox:~/appe$ gem list

*** LOCAL GEMS ***
...
http_accept_language (1.0.2)
...

max@max-VirtualBox:~/app$ sudo gem uninstall http_accept_language
INFO:  gem "http_accept_language" is not installed
michelpm
  • 1,795
  • 2
  • 18
  • 31
Maximus S
  • 10,759
  • 19
  • 75
  • 154
  • I can't figure out which platform you are using, but I think you didn't have problem installing it, I think your "sudo gem" accessed a different ruby. – michelpm May 01 '13 at 20:31
  • One more thing, you are asking two things, you could make the question much more readable if you just removed everything from "Also, " to "UPDATE:". I pretty sure you can figure out whatever you meant to ask there after the main question is solved. – michelpm May 01 '13 at 20:44

5 Answers5

2

Try using request.user_preferred_languages instead of http_accept_language.user_preferred_languages.

Fabian Winkler
  • 1,401
  • 16
  • 24
2

In the documentation states that since version 2.0, the gem is a Rack middleware, but the problem is that the only 2.0 version released in June 2012 is only a pre-release. Therefore, to get the version 2.0 you need to do this:

gem 'http_accept_language', '~> 2.0.0.pre'
michelpm
  • 1,795
  • 2
  • 18
  • 31
  • This solution helped me but when Rails reloads app after change was made the problem appears again. Rails 3.2.13, ruby 2.0.0-p195, Development env. By the way I've found suited me solution here: https://github.com/iain/http_accept_language/pull/19 – vogdb Jun 23 '13 at 12:59
1

I didn't have to use the gem to implement the feature I wanted.

  def set_i18n_locale
    unless params[:locale]
        params[:locale] = extract_locale_from_accept_language_header
    end
    available = ['en', 'kr']
    if available.include? params[:locale]
        I18n.locale = params[:locale]
    end
  end

  def extract_locale_from_accept_language_header
    request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[a-z]{2}/).first
  end

  def default_url_options
    { :locale => I18n.locale }
  end

Official Guide and Agile Development helped a lot.

Maximus S
  • 10,759
  • 19
  • 75
  • 154
  • It is a bad thing this code has made such influential material, it is poor man's content negotiation. – michelpm May 01 '13 at 20:38
  • 1
    Your code is accurate only when user's browsers first preferred language is 'en' or 'kr'. If the user preferred languages does not match-up with your available then you need to specify a default locale to fall-back for. Specific answer to your original question, As @michelpm has mentioned already. You need to specify `gem 'http_accept_language', '~ 2.0.0.pre'` in your Gemfile. – RajG May 10 '13 at 12:09
0

As for me solution was to apply @DouweM PR from the github. Here is the line from the Gemfile:

gem 'http_accept_language', :git => 'https://github.com/DouweM/http_accept_language', :branch => 'no-middleware-no-crash'
vogdb
  • 4,669
  • 3
  • 27
  • 29
0

put in your Gemfile:

gem 'http_accept_language', '~> 2.0.0.pre'

and then in the code use env aka:

env.http_accept_language

works for me.

% bundle show|grep acc
    * http_accept_language (2.0.0.pre)
edx
  • 1,317
  • 1
  • 12
  • 14