You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
76 lines
2.2 KiB
PHP
76 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Utils\Cookie;
|
|
|
|
class Internationalization
|
|
{
|
|
public function __construct()
|
|
{
|
|
$this->lang;
|
|
}
|
|
|
|
public function detectLang($request, $response, $args)
|
|
{
|
|
$user = Auth::getUser();
|
|
if ($user->isLogin == true && $user->lang != null) {
|
|
if ($user->lang == '' || $user->lang == null) {
|
|
$user->lang = 'zh-cn';
|
|
$user->save();
|
|
}
|
|
$lang = $user->lang;
|
|
} else {
|
|
$cookie = Cookie::get('lang');
|
|
if ($cookie != null || $cookie != '') {
|
|
$lang = $cookie;
|
|
} else {
|
|
$accept_language = $request->getHeader('Accept-Language');
|
|
$accept_language = strtolower($accept_language[0]);
|
|
$accept_language = explode(";",$accept_language)[0];
|
|
$accept_language = explode(",",$accept_language)[0];
|
|
if (strpos($accept_language, 'zh') !== false) {
|
|
if (strpos($accept_language, 'cn') !== false) {
|
|
$lang = 'zh-cn';
|
|
} elseif (strpos($accept_language, 'tw') !== false || strpos($accept_language, 'hk') !== false) {
|
|
$lang = 'zh-tw';
|
|
}
|
|
}
|
|
if (strpos($accept_language, 'en') !== false) {
|
|
$lang = 'en';
|
|
}
|
|
if ($user->isLogin) {
|
|
$user->lang = $lang;
|
|
$user->save();
|
|
}
|
|
Cookie::set([
|
|
'lang' => $lang
|
|
],'1775445251');
|
|
}
|
|
}
|
|
|
|
$this->lang = $lang;
|
|
return $lang;
|
|
}
|
|
|
|
public function get($name, $args=null) {
|
|
global $I18N;
|
|
if ($args == null){
|
|
return $I18N[$name][$this->lang];
|
|
} else {
|
|
$counter = 1;
|
|
$text = $I18N[$name][$this->lang];
|
|
foreach ($args as $arg) {
|
|
$text = str_replace("$".$counter, $arg, $text);
|
|
$counter += 1;
|
|
}
|
|
return $text;
|
|
}
|
|
}
|
|
|
|
public static function getPublicConfig()
|
|
{
|
|
global $I18N;
|
|
return $I18N;
|
|
}
|
|
} |