如何检测客户国家,laravel 4 中的语言环境
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21654411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to detect client country, locale in laravel 4
提问by ohm89
I want to detect my client country or locale which they open website from or get the browser recommended language.
我想检测我的客户国家或地区,他们从中打开网站或获取浏览器推荐的语言。
For Example, if you open the browser in Japan it will give me country code or country name current user opened like "en-jp" or "japan".
例如,如果您在日本打开浏览器,它将为我提供当前用户打开的国家代码或国家名称,如“en-jp”或“japan”。
After Searching I found out that "Zend Framework" has function to detect the user/environmental in Zend_locale.
搜索后我发现“Zend Framework”具有检测Zend_locale 中的用户/环境的功能。
So I wonder if I can do the same in laravel 4 or if not what solution you suggest in any method (php, javascript, checking ip, etc.)?
所以我想知道我是否可以在 laravel 4 中做同样的事情,或者如果不能,你在任何方法(php、javascript、检查 ip 等)中建议什么解决方案?
Thank you in advanced.
先谢谢了。
回答by ohm89
Ok I know the answers of my questions as following:
好的,我知道我的问题的答案如下:
How to detect the client country?
As far as I know we need to use geoIP service to detect the client IP which can tell where the client using from (e.g. maxmind)
But this is not solution to detect and alter my website language, if you looking for this solution in laravel 4 I will show you in next question
How to check the language that client want to use? (locale in laravel4)
In summarize I found some of ways that can get which language that client want to use by following:
HTTP Header (HTTP_ACCEPT_LANGUAGE) in
$_SERVER['HTTP_ACCEPT_LANGUAGE']
equal toRequest::server('HTTP_ACCEPT_LANGUAGE')
in laravel4. Which these header tell us the language that current client browser want to use.Direct request - In this condition we will get direct request from client which language they want to use. For easy example like we give them the
<select> <option val="en">English</option> <option val="th">Thailand</option> </select>
And they select from it send to server via url Ex: www.Test.com/en
Cookies (optional) - We can get the cookies from browser which we provide the language that last use by current user. Which is we must send the cookies after they visited the site for first times.
如何检测客户国家?
据我所知,我们需要使用 geoIP 服务来检测客户端 IP,它可以告诉客户端使用的位置(例如maxmind)
但这不是检测和更改我的网站语言的解决方案,如果您在 laravel 4 中寻找此解决方案,我将在下一个问题中向您展示
如何查看客户端想要使用的语言?(laravel4 中的语言环境)
总而言之,我发现了一些可以通过以下方式获得客户想要使用的语言的方法:
HTTP Header (HTTP_ACCEPT_LANGUAGE)
$_SERVER['HTTP_ACCEPT_LANGUAGE']
等同Request::server('HTTP_ACCEPT_LANGUAGE')
于 laravel4。这些标头告诉我们当前客户端浏览器想要使用的语言。直接请求 - 在这种情况下,我们将直接从客户那里得到他们想要使用哪种语言的请求。举个简单的例子,就像我们给他们
<select> <option val="en">English</option> <option val="th">Thailand</option> </select>
他们从中选择通过 url 发送到服务器,例如:www.Test.com/en
Cookies(可选) - 我们可以从浏览器中获取 cookie,我们提供当前用户最后使用的语言。这是我们必须在他们第一次访问网站后发送 cookie。
Before I use it I store the support languages array in app/config/app.php by following:
在我使用它之前,我通过以下方式将支持语言数组存储在 app/config/app.php 中:
'languages' => array('en','th','jp'),
All of it I modify the code in app/filter.php to get all above data and processing with my app by following:
所有这些我都修改了 app/filter.php 中的代码,以通过以下方式获取上述所有数据并使用我的应用程序进行处理:
App::before(function($request){
// 1. get the request langugage
$url_lang = Request::segment(1);
// 2. get Cookie langugage
$cookie_lang = Cookie::get('language');
// 3. Get the Browser Request language
$browser_lang = substr(Request::server('HTTP_ACCEPT_LANGUAGE'), 0, 2);
// 4. Start Checking the request language
// Check that Language tha request is support or not?
if(!empty($url_lang) AND in_array($url_lang, Config::get('app.languages')))
{
// Check whether the request url lang not same as remember in cookies
if($url_lang != $cookie_lang)
{
// Cookie::forever('language',$url_lang);
Session::put('language', $url_lang);
}
// Set the App Locale
App::setLocale($url_lang);
}
// Check that has Language in Forever Cookie and is it support or not?
else if(!empty($cookie_lang) AND in_array($cookie_lang, Config::get('app.languages')))
{
// Set App Locale
App::setLocale($cookie_lang);
}
// Check the browser request langugae is support in app?
else if(!empty($browser_lang) AND in_array($browser_lang, Config::get('app.languages')))
{
// Check whether the request url lang not same as remember in cookies
if($browser_lang != $cookie_lang)
{
// Cookie::forever('language',$browser_lang);
Session::put('language', $browser_lang);
}
// Set Browser Lang
App::setLocale($browser_lang);
}
else
{
// Default Application Setting Language
App::setLocale(Config::get('app.locale'));
}});
And after event of app is following:
应用程序事件后如下:
App::after(function($request, $response){
$lang = Session::get('language');
if(!empty($lang))
{
// Send The language Cookies
$response->withCookie(Cookie::forever('language',$lang));
}
});
Hope this will help you out.
希望这会帮助你。
回答by Dustin Brownell
I use this Ip2Country
for Laravel 4.2 that can retrieve a users country based on a given IP address. Creates a local database that utilizes the MaxMind GeoIP data, so no run time external API calls.
我将此Ip2Country
用于 Laravel 4.2,它可以根据给定的 IP 地址检索用户国家/地区。创建一个利用 MaxMind GeoIP 数据的本地数据库,因此没有运行时外部 API 调用。
回答by Gabriel Glauber
Speaks ohm89! I used the following technique to capture the priority user linguam and show my site in your language:
说 ohm89!我使用以下技术来捕获优先用户的语言并以您的语言显示我的网站:
1o - I created two new arrays in my app.php, alt_langs (supported by my site) and locale_prefix (language prefix to url):
1o - 我在 app.php 中创建了两个新数组,alt_langs(由我的站点支持)和 locale_prefix(url 的语言前缀):
'locale' => 'pt', // Default is en.
'alt_langs' => array ('pt', 'en', 'es'), // Supported by my site
'locale_prefix' => '', // Dinamic array.
2o - In routes.php file:
2o - 在 routes.php 文件中:
// Get the first segment url, ex.: mysite.com/pt and put this in locale_prefix array item:
if (in_array(Request::segment(1), Config::get('app.alt_langs'))) {
App::setLocale(Request::segment(1));
Config::set('app.locale_prefix', Request::segment(1));
}
// Here i usin the prexix to show right language site:
Route::group(array('prefix'=>Config::get('app.locale_prefix')), function()
{
Route::get('', array('uses' => 'HomeController@index'));
});
// And here i usin the http_accept_language to redirect him to default browser language:
Route::get('/', function(){
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
return Redirect::to($lang);
});
I hope I have helped.o/
我希望我有所帮助。o/
回答by c-griffin
I haven't tried this package, but you can probably use this:
https://github.com/webpatser/laravel-countries
我还没有尝试过这个包,但你可以使用这个:https:
//github.com/webpatser/laravel-countries
If that doesn't get you all you need, you can probably combine it with the built in App::setLocale('en');
Referenced in:
http://cheats.jesse-obrien.ca
如果这不能满足您的所有需求,您可以将其与内置的 App::setLocale('en'); 结合使用。
参考:http:
//cheats.jesse-obrien.ca