|
/** User Language Class: get/set the page Language accessed by the User. if the User does not set the access Language, read the Accept-Language
* Date: 2014-05-26
* Author: fdipzone
* Ver: 1.0
*
* Func:
* Public get obtains the user access language
* Public set: set the user access language
* Private getAcceptLanguage: HTTP_ACCEPT_LANGUAGE
*/
Class UserLang {// class start
Private $ name = 'userlang '; // cookie name
Private $ expire = 2592000; // cookie expire 30 days
/** Initialization
* @ Param String $ name cookie name
* @ Param int $ expire cookie expire
*/
Public function _ construct ($ name = '', $ expire = null ){
// Set the cookie name
If ($ name! = ''){
$ This-> name = $ name;
}
// Set cookie expire
If (is_numeric ($ expire) & $ expire> 0 ){
$ This-> expire = intval ($ expire );
}
}
/** Obtain the user access language */
Public function get (){
// Determine whether the user has set a language
If (isset ($ _ COOKIE [$ this-> name]) {
$ Lang = $ _ COOKIE [$ this-> name];
} Else {
$ Lang = $ this-> getAcceptLanguage ();
}
Return $ lang;
}
/** Set the user access language
* @ Param String $ lang user access language
*/
Public function set ($ lang = ''){
$ Lang = strtolower ($ lang );
// Only English, Simplified Chinese, and traditional Chinese
If (in_array ($ lang, array ('en', 'scs', 'TC '))){
Setcookie ($ this-> name, $ lang, time () + $ this-> expire );
}
}
/** Get HTTP_ACCEPT_LANGUAGE */
Private function getAcceptLanguage (){
$ Lang = strtolower ($ _ SERVER ['http _ ACCEPT_LANGUAGE ']);
If (in_array (substr ($ lang,), array ('zh-tw ', 'zh _ hk '))){
$ Lang = 'TC ';
} Elseif (in_array (substr ($ lang,), array ('zh-cn', 'zh-sg '))){
$ Lang = 'scs ';
} Else {
$ Lang = 'en ';
}
Return $ lang;
}
} // Class end
?>
|