http_accept_language
Ruby on Rails plugin. Fishes out the Accept-Language header into an array.
Is there a way to translate my website automatically based on the visitor's IP and "accept-language" header information using Google Translate Plug-in?
Thank you!
Source: (StackOverflow)
I would like to react to the Accept_Language that a browser sends to a website.
Does anybody know where I can get a reliable list of all available Accept_Languages that the browser might send to a website?
Thank you very much!
Source: (StackOverflow)
I need to detect the country of users but I'm trying to avoid the whole call to an external service to get the location based on IP or to an internal database. I need to make this service really responsive and it takes almost half a second to query any external service so I'm trying to avoid that. Also any call to a db would be costly for this approach. I need the page to respond in less than 20ms.
I'm thinking of get the country locale based on Accept_language
header.
My question is. Does anyone know how accurate may this be? I'm aware country is not always present or users may change default language or be present on a different country with their computers.
Does anyone had previous experience or is there any documentation (I couldn't find one googling or in w3 page) that specifies a percent of accuracy or error using this approach?
Source: (StackOverflow)
I've put together a small script in PHP that checks for the browser's language settings and redirect them to a language version of the site (WP multisite),
function redirect() {
$language = substr( $_SERVER["HTTP_ACCEPT_LANGUAGE"],0,2 );
switch( $language ) {
case 'sv':
header( 'Location: http://www.example.com/sv/' );
break;
case 'no':
header( 'Location: http://www.example.com/no/' );
break;
case 'da':
header( 'Location: http://www.example.com/da/' );
break;
default:
header( 'Location: http://www.example.com/' );
break;
}
}
if ( strlen($url) < 4 ) {
session_start();
if ( empty($_SESSION[ 'language' ]) ) {
$_SESSION[ 'language' ] = true;
redirect();
}
}
When testing with Mobile Safari or Mobile Chrome the redirect doesn't appear to work. Is there any special output for the accept language for mobile browsers that I need to consider?
Update: After some more debugging I found out this:
- Mobile Safari displays the correct language when echoing HTTP_ACCEPT_LANGUAGE but does not redirect.
- Mobile Chrome (iOS only, works on Android) doesn't display the correct language (defaults to "en").
- iOS parses the http header data in a different order, compare iOS Chrome (en-US,en;q=0.8,sv;q=0.6) and OSX Chrome (sv,en-US;q=0.8,en;q=0.6).
Source: (StackOverflow)
i am testing pseudo-localization of a web-site.
i can configure Internet Explorer to have custom accept languages:
- Click Tools, Internet Options
- On the General tab click Languages
- In the Language Preferences dialog click Add.
- Enter a user-defined language of
qps-ploc
(i.e. the Pseudo (Base) locale)
- Click OK

Now when Internet Explorer issues an http request, the accept languages will lead with qpc-ploc
:
GET http://stackoverflow.com/ HTTP/1.1
Accept: application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, */*
Accept-Language: qps-ploc,en-US;q=0.5
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C; .NET4.0E)
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
Host: stackoverflow.com
How do i perform the same change to Chrome? To Firefox?
Update
It should also be noted that Internet Explorer honors my Windows preferences. My Windows is configured to use Pseudo (Base) qps-ploc
locale. By default Internet Explorer uses it.
Google Chrome ignores my Windows preferences, deciding instead to request en-US
and en
language:
GET http://stackoverflow.com/ HTTP/1.1
Host: stackoverflow.com
Connection: keep-alive
Cache-Control: max-age=0
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.220 Safari/535.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Source: (StackOverflow)
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
Source: (StackOverflow)
Our business requirements state that the user should be able to change their language preference without changing their browser or system settings. Our implementation is a simple interface that asks them to choose a language. Originally, I was going to just store their choice in a cookie value and check for it and set the UI Culture, but I am wondering if there is an easier way.
At the moment, I'm trying to see if I can just accept their choice and then set the accept-language in the response so that it will be used in subsequent requests. Right now, I set the accept-language header in the response, but the next request overwrites it with browser values (e.g. en-US,en;q=0.8") Is this possible and how do I go about it? Any other ideas to achieve this functionality is also welcomed.
Source: (StackOverflow)
According to Grails' internationalization doc, i18n is accomplished by either:
- Setting an
Accept-Language
header in the request; or
- Tacking on a
lang
query string param
I'm opting for the former, because its more HTTP compliant and, frankly, I just don't like adding it as a query param.
But how would I set Accept-Language
in the first place?!?
I want my app's locale to be determined by the TLD requested. So, a request for http://myapp.example.com
would return the en_US
version of my app, because *.com
is traditionally associated with English/US. If http://myapp.example.fr
was requested, then Grails should serve back the fr_FR
version of my app. Etc.
So my real question is:
How can I map the requested URL/TLD to a variable that Grails can set on the server, and then inject into GSPs, such that client-side requests always have a matching Accept-Language
header for the given URL/TLD?
Update
I want to confirm browser/Grails behavior here: is it safe to assume that when a user configures their browser, that the browser adjusts the Accept-Language
header accordingly? If so, can I also assume that if Accept-Language
is not sent, and lang
is not specified as a query param, that Grails uses en_US
by default?
Source: (StackOverflow)
as i am new to .htaccess, i couldn´t figure out how to make this work:
On my webserver the default index.html (/var/www/) should be redirected to different other index.html regarding the time of the day and the browser language...
- /index.html (default, german, black background for the night)
- /i/index.html (german, white background for the day)
- /e/index.html (english, black background for the night)
- /e/i/index.html (english, white background for the day)
All pages are linked with each other, so that the user can jump between language and "style".
Using only the time-condition it works for german, but how can i combine this with "english"?
I tried this one, but it didn´t work...
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{TIME_HOUR} >07
RewriteCond %{TIME_HOUR} <16
RewriteCond %{HTTP:Accept-Language} ^en [NC]
RewriteRule ^index\.html$ /e/i/index.html [L]
RewriteCond %{TIME_HOUR} >07
RewriteCond %{TIME_HOUR} <16
RewriteCond %{HTTP:Accept-Language} ^de [NC]
RewriteRule ^index\.html$ /i/index.html [L]
RewriteCond %{TIME_HOUR} >16
RewriteCond %{TIME_HOUR} <07
RewriteCond %{HTTP:Accept-Language} ^en [NC]
RewriteRule ^index\.html$ /e/index.html [L]
RewriteCond %{TIME_HOUR} >16
RewriteCond %{TIME_HOUR} <07
RewriteCond %{HTTP:Accept-Language} ^de [NC]
RewriteRule ^index\.html$ /index.html [L]
Could somebody help please?
Many Thanks!
I had to change the structure of the website, so i also changed the .htaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP:Accept-Language} (de) [NC]
RewriteCond %{TIME_HOUR} >07
RewriteCond %{TIME_HOUR} <16
RewriteRule ^index\.html$ /d/i/index.html [L]
RewriteRule ^index\.html$ /d/index.html [L]
RewriteCond %{HTTP:Accept-Language} (en) [NC]
RewriteCond %{TIME_HOUR} >16
RewriteCond %{TIME_HOUR} <07
RewriteRule ^index\.html$ /e/i/index.html [L]
RewriteRule ^index\.html$ /e/index.html [L]
Now the time-condition for german works fine, but it seems that the english-condition is not even passed to the browser...
Could somebody give me a hint, what could be the problem?
Many thanks!
Source: (StackOverflow)
I am not experienced in php, i know only basics. I dont know if i am asking this correctly, and i dont even know if this is possible, but i will try to explain.
1. I have database with 10+ tables. Each table is named as countrie (Austria, Germany, Latvia, Lithuania..etc).
2. I have script, which displays users browser language when he connects to website.
3. Also, i have php query script, which displays data by users browser language (if user connects with browser, which primary language is Austrian, than php query display data from Austria table).
HTTP_ACCEPT_LANGUAGE
if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])){
$lc = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
}
if($lc == "de") { $langas = "Germany"; }
if($lc == "lt") { $langas = "Lithuania"; }
// etc..
echo $langas;
Query:
foreach($db->query('SELECT * FROM '.$langas.'') as $row) {
echo '<span>'.$row['info1'].'</span>';
echo '<span>'.$row['info2'].'</span>';
// etc..
}
What i want to do:
I want that if user connects to website, and his browser language does not match with my tables, than as default, query should initiate with other table and not send errors (for example Latvia table). How i can do that?
Other question:
I know, that if i will use HTTP_ACCEPT_LANGUAGE function, than i will be not able to use code, that user would be able to manually select countrie. Maybe i am wrong.. maybe someone can explain that for me?
Sorry for bad english, and thanks to any answers.
Source: (StackOverflow)
I have been writing a C# program to deal with and send web requests and they seem to be working as intended, however I have in my code the following two headers within my HttpWebRequest
:
request.Accept = "application/xml";
request.Expect = "application/xml";
I am a little confused of the difference or which way round these are, could some one please clarify to me which is which or if I am wrong. Am I right in thinking Accept
is what I am expecting them to take from me me (e.g an XML) and Expect
is what they should return to me, i.e. what I am expecting in return.
Have I got this wrong? a lot of the stuff I can find on-line in documentation is a little vague and confusing for me to understand, am I even using expect correctly or should it be something completely different?
Any clarification would be fantastic, thanks
Source: (StackOverflow)
I'd like to find the correct method to set the Accept-Language header for my crawler? I read other related answers like Getting imdb movie titles in a specific language and How to set Accept-Language header on request from applet
but they didn't work for me (I get this error: "the method is undefined for type connection"
Here is part of code:
String baseUrl = "http://www.imdb.com/search/title?at=0&count=250";
org.jsoup.Connection con = Jsoup.connect(baseUrl).userAgent("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.21 (KHTML, like Gecko) Chrome/19.0.1042.0 Safari/535.21");
Please help me, I am really new to java.
Thanks
Source: (StackOverflow)
I have a multi-language
page, I want to detect client browser's language then make a 301 home page or other thing. but I am not sure which way is better for seo. I do not know web spider
like which one? Or other way?
<?php
$LG=$_SERVER['HTTP_ACCEPT_LANGUAGE'];
if (preg_match('/^[zZ][hH]/', $LG)) {
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://mydomain.com/cn/");
exit();} //jump to chinese version
else {
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://mydomain.com/en/");
exit();} //jump to english version
?>
OR
<?php
$LG=$_SERVER['HTTP_ACCEPT_LANGUAGE'];
if (preg_match('/^[zZ][hH]/', $LG)) {
include ("http://mydomain.com/cn/");
} //include chinese version
else {
header("HTTP/1.1 301 Moved Permanently");
include ("http://mydomain.com/en/");
} //include english version
?>
OR other way? Thanks.
Source: (StackOverflow)
In my Codeigniter app I use $_SERVER['HTTP_ACCEPT_LANGUAGE']
to determine the users browser language to set the app language based on that, like that:
public function __construct()
{
parent::__construct();
/* set session language if not set. "hu" if browser language "hu", else "en" */
if(!($this->session->userdata("lang")))
{
$browserlang = substr($_SERVER["HTTP_ACCEPT_LANGUAGE"],0,2);
if ($browserlang == "hu")
{
$this->config->set_item("language", "hu");
$this->session->set_userdata("lang", "hu");
$this->lang->load("bh_hu", "hungarian");
}
else
{
$this->config->set_item("language", "en");
$this->session->set_userdata("lang", "en");
$this->lang->load("bh_en", "english");
}
}
else
{
switch ($this->session->userdata("lang"))
{
case "hu": $this->lang->load("bh_hu", "hungarian"); break;
case "en": $this->lang->load("bh_en", "english"); break;
}
}
....
}
Everything's working fine, but as the google crawler does not send any HTTP_ACCEPT_LANGUAGE
, it returns a php error Undefined index: HTTP_ACCEPT_LANGUAGE
when crawling all my pages.
This is really nasty, because the php error even fills up my entire google search snippet.
Is there a way to just let the google bot ignore the HTTP_ACCEPT_LANGUAGE
action?
Thanks in advance!
Source: (StackOverflow)
I recently added some new code to a before_action
in my ApplicationController
:
class ApplicationController < ActionController::Base
before_action :set_locale
def set_locale
I18n.locale = (session[:locale] || params[:locale] || extract_locale_from_accept_language_header).to_s.downcase.presence || I18n.default_locale
end
private
def extract_locale_from_accept_language_header
request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[a-z]{2}/).first
end
end
The problem is that the extract_locale_from_accept_language_header
function disrupts all my controller specs (i.e. they all fail now). It seems that RSpec can't detect any HTTP_ACCEPT_LANGUAGE
.
Is there a way to fake this behaviour for all my controller specs?
The following does work but is a bit ugly since I would have to add the line request.env...
to all my controller tests. And I have many of them.
require 'spec_helper'
describe UsersController do
before :each do
@user = FactoryGirl.create(:user)
request.env['HTTP_ACCEPT_LANGUAGE'] = "en" # ugly
end
...
end
Can anybody help?
Thanks.
Source: (StackOverflow)