Skip to content

Instantly share code, notes, and snippets.

@kazu69
Created July 2, 2013 16:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kazu69/5911018 to your computer and use it in GitHub Desktop.
Save kazu69/5911018 to your computer and use it in GitHub Desktop.
wordpressで言語設定を取得して振り分けるときの例
<?php
// WordPressの読み込みが完了し、ヘッダーが送信される前に実行する。
add_action( 'init', 'init' );
function init() {
// 言語設定が日本語でなく、パラメーター付与されていないとき針ダイレクト
if(!is_Ja() && $_GET["lang"] != "en"):
$url = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'] . "?lang=en";
header("Location: $url");
exit;
endif;
}
function http_accept_language() {
$languages = split(",", $_SERVER['HTTP_ACCEPT_LANGUAGE'] );
$lang_list = Array();
foreach( $languages as $lang ) {
$lang_array = split(";q=", trim( $lang ) );
$lang = trim( $lang_array[0] );
if( !isset( $lang_array[1] ) )
$q = 1;
else
$q = trim($lang_array[1]);
$lang_list["$lang"] = (float)$q;
}
arsort($lang_list);
$i = 0;
$lang_index = Array();
foreach($lang_list as $lang => $q) {
$lang_index[$i] = $lang;
$i++;
}
return $lang_index;
}
// 日本語 判定
function is_Ja() {
$user_lang = http_accept_language();
return (preg_match('/^ja/', $user_lang[0])) ? true : false ;
}
?>
@taketin
Copy link

taketin commented Jul 3, 2013

apache 環境変数の「HTTP_ACCEPT_LANGUAGE」にブラウザの言語設定が格納されていて、http_accept_language() でそれを取得できる。

今回の要求にはこれで良さそうですねー。:+1:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment