php实现获取及设置用户访问页面语言类 本文实例讲述了php实现获取及设置用户访问页面语言类,分享给大家供大家参考。具体分析如下:
该实例user language class 获取/设置用户访问的页面语言,如果用户没有设置访问语言,则读取accept-language。根据用户选择的语言显示对应的页面(英文,简体中文,繁体中文)
userlang.class.php类文件如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
name = $name;
}
// 设置cookie expire
if(is_numeric($expire) && $expire>0){
$this->expire = intval($expire);
}
}
/** 获取用户访问语言 */
public function get(){
// 判断用户是否有设置过语言
if(isset($_cookie[$this->name])){
$lang = $_cookie[$this->name];
}else{
$lang = $this->getacceptlanguage();
}
return $lang;
}
/** 设置用户访问语言
* @param string $lang 用户访问语言
*/
public function set($lang=''){
$lang = strtolower($lang);
// 只能是英文,简体中文,繁体中文
if(in_array($lang, array('en','sc','tc'))){
setcookie($this->name, $lang, time()+$this->expire);
}
}
/** 获取http_accept_language */
private function getacceptlanguage(){
$lang = strtolower($_server['http_accept_language']);
if(in_array(substr($lang,0,5), array('zh-tw','zh_hk'))){
$lang = 'tc';
}elseif(in_array(substr($lang,0,5), array('zh-cn','zh-sg'))){
$lang = 'sc';
}else{
$lang = 'en';
}
return $lang;
}
} // class end
?>
demo示例程序如下:
1
2
3
4
5
6
7
get().'
';
?>
http://www.bkjia.com/phpjc/886548.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/886548.htmltecharticlephp实现获取及设置用户访问页面语言类 本文实例讲述了php实现获取及设置用户访问页面语言类,分享给大家供大家参考。具体分析如下:...
