增加后端多语言 修复前端语言错误

This commit is contained in:
lingling 2025-02-26 21:48:37 +08:00
parent 5ee8f58807
commit ba4b085462
8 changed files with 107 additions and 29 deletions

View File

@ -13,7 +13,7 @@ class ApiResponseApp
return json([
'code' => $code,
'data' => $data,
'msg' => $message,
'msg' => trans($message),
'time' => time()
]);
}
@ -23,7 +23,7 @@ class ApiResponseApp
$code = 0;
return json([
'code' => $code,
'msg' => $message,
'msg' => trans($message),
'data' => $data,
'time' => time()
]);

32
app/middleware/Lang.php Normal file
View File

@ -0,0 +1,32 @@
<?php
namespace app\middleware;
use Webman\MiddlewareInterface;
use Webman\Http\Response;
use Webman\Http\Request;
class Lang implements MiddlewareInterface
{
public function process(Request $request, callable $handler) : Response
{
// 获取请求头中的 Accept-Language
$acceptLanguage = $request->header('Accept-Language', 'zhcn'); // 默认值是 'zh_CN'
// 将 Accept-Language 中的语言与系统支持的语言进行匹配
// 这里假设我们支持 'zh_CN' 和 'en_US',你可以根据实际需求扩展更多语言
$lang = 'zh_CN'; // 默认语言
// 提取 Accept-Language 中的语言部分,如 'zh_CN' 或 'en_US'
if (strpos($acceptLanguage, 'zh') === 0) {
$lang = 'zhcn';
} elseif (strpos($acceptLanguage, 'en') === 0) {
$lang = 'en';
}
// 设置语言,假设你有一个 locale 函数来处理语言设置
locale(session('lang', $lang));
// 返回请求处理
return $handler($request);
}
}

View File

@ -36,7 +36,8 @@
"hhink/webman-sms": "^1.0",
"simplehtmldom/simplehtmldom": "^2.0@RC",
"guzzlehttp/guzzle": "^7.9",
"workerman/crontab": "^1.0"
"workerman/crontab": "^1.0",
"symfony/translation": "^6.0"
},
"suggest": {
"ext-event": "For better performance. "

48
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "abda64e112867835ad0472d5a7b86c66",
"content-hash": "6e60ac96c1f817e5e663943291142785",
"packages": [
{
"name": "cakephp/core",
@ -4211,16 +4211,16 @@
},
{
"name": "symfony/translation",
"version": "v5.4.45",
"version": "v6.0.19",
"source": {
"type": "git",
"url": "https://github.com/symfony/translation.git",
"reference": "98f26acc99341ca4bab345fb14d7b1d7cb825bed"
"reference": "9c24b3fdbbe9fb2ef3a6afd8bbaadfd72dad681f"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/translation/zipball/98f26acc99341ca4bab345fb14d7b1d7cb825bed",
"reference": "98f26acc99341ca4bab345fb14d7b1d7cb825bed",
"url": "https://api.github.com/repos/symfony/translation/zipball/9c24b3fdbbe9fb2ef3a6afd8bbaadfd72dad681f",
"reference": "9c24b3fdbbe9fb2ef3a6afd8bbaadfd72dad681f",
"shasum": "",
"mirrors": [
{
@ -4230,35 +4230,33 @@
]
},
"require": {
"php": ">=7.2.5",
"symfony/deprecation-contracts": "^2.1|^3",
"php": ">=8.0.2",
"symfony/polyfill-mbstring": "~1.0",
"symfony/polyfill-php80": "^1.16",
"symfony/translation-contracts": "^2.3"
"symfony/translation-contracts": "^2.3|^3.0"
},
"conflict": {
"symfony/config": "<4.4",
"symfony/console": "<5.3",
"symfony/dependency-injection": "<5.0",
"symfony/http-kernel": "<5.0",
"symfony/twig-bundle": "<5.0",
"symfony/yaml": "<4.4"
"symfony/config": "<5.4",
"symfony/console": "<5.4",
"symfony/dependency-injection": "<5.4",
"symfony/http-kernel": "<5.4",
"symfony/twig-bundle": "<5.4",
"symfony/yaml": "<5.4"
},
"provide": {
"symfony/translation-implementation": "2.3"
"symfony/translation-implementation": "2.3|3.0"
},
"require-dev": {
"psr/log": "^1|^2|^3",
"symfony/config": "^4.4|^5.0|^6.0",
"symfony/config": "^5.4|^6.0",
"symfony/console": "^5.4|^6.0",
"symfony/dependency-injection": "^5.0|^6.0",
"symfony/finder": "^4.4|^5.0|^6.0",
"symfony/dependency-injection": "^5.4|^6.0",
"symfony/finder": "^5.4|^6.0",
"symfony/http-client-contracts": "^1.1|^2.0|^3.0",
"symfony/http-kernel": "^5.0|^6.0",
"symfony/intl": "^4.4|^5.0|^6.0",
"symfony/http-kernel": "^5.4|^6.0",
"symfony/intl": "^5.4|^6.0",
"symfony/polyfill-intl-icu": "^1.21",
"symfony/service-contracts": "^1.1.2|^2|^3",
"symfony/yaml": "^4.4|^5.0|^6.0"
"symfony/yaml": "^5.4|^6.0"
},
"suggest": {
"psr/log-implementation": "To use logging capability in translator",
@ -4294,7 +4292,7 @@
"description": "Provides tools to internationalize your application",
"homepage": "https://symfony.com",
"support": {
"source": "https://github.com/symfony/translation/tree/v5.4.45"
"source": "https://github.com/symfony/translation/tree/v6.0.19"
},
"funding": [
{
@ -4310,7 +4308,7 @@
"type": "tidelift"
}
],
"time": "2024-09-25T14:11:13+00:00"
"time": "2023-01-01T08:36:10+00:00"
},
{
"name": "symfony/translation-contracts",
@ -5074,6 +5072,6 @@
"platform": {
"php": ">=7.2"
},
"platform-dev": {},
"platform-dev": [],
"plugin-api-version": "2.6.0"
}

View File

@ -17,5 +17,6 @@ return [
// ... 这里省略其它中间件
app\middleware\CorsMiddleware::class,
app\middleware\JwtAuthMiddleware::class,
app\middleware\Lang::class,
]
];

View File

@ -15102,6 +15102,8 @@ const Vf = {
非必填: "Not required",
余额不足: "Insufficient balance",
请添加印度: "Please add Bangladesh",
请添加孟加拉国:"Please add Bangladesh",
孟加拉国:"Bangladesh",
提款需要300积分: "300 points required for withdrawal",
提款需要1000积分: "1000 points required for withdrawal",
提款需要500积分: "500 points required for withdrawal",
@ -15453,7 +15455,9 @@ const Vf = {
积分: "积分",
非必填: "非必填",
余额不足: "余额不足",
请添加印度: "请添加孟加拉国",
请添加印度: "请添加印度",
请添加孟加拉国:"请添加孟加拉国",
孟加拉国:"孟加拉国",
提款需要300积分: "提款需要300积分",
提款需要1000积分: "提款需要1000积分",
提款需要500积分: "提款需要500积分",

View File

@ -0,0 +1,21 @@
<?php
return [
'挂机时长没有超过6小时暂不能签到' => 'You have not been idle for more than 6 hours, cannot sign in yet.',
'您已登陆' => 'You are already logged in.',
'请发送验证码' => 'Please send the verification code.',
'账户余额不足' => 'Insufficient account balance.',
'需要完成Whatsapp任务' => 'You need to complete the Whatsapp task.',
'账号或密码错误' => 'Incorrect username or password.',
'账号被禁用' => 'The account is disabled.',
'登录成功' => 'Login successful.',
'账号已存在' => 'Account already exists.',
'代理不存在' => 'Agent does not exist.',
'注册成功' => 'Registration successful.',
'原密码错误' => 'Incorrect original password.',
'修改成功' => 'Modification successful.',
'领取成功' => 'Claim successful.',
'每天只能提现三次' => 'You can only withdraw three times a day.',
'等待管理员审核' => 'Waiting for administrator review.',
'转账成功' => 'Transfer successful.',
];

View File

@ -0,0 +1,21 @@
<?php
return [
'挂机时长没有超过6小时暂不能签到' => '挂机时长没有超过6小时暂不能签到',
'您已登陆' => '您已登陆',
'请发送验证码' => '请发送验证码',
'账户余额不足' => '账户余额不足',
'需要完成Whatsapp任务' => '需要完成Whatsapp任务',
'账号或密码错误' => '账号或密码错误',
'账号被禁用' => '账号被禁用',
'登录成功' => '登录成功',
'账号已存在' => '账号已存在',
'代理不存在' => '代理不存在',
'注册成功' => '注册成功',
'原密码错误' => '原密码错误',
'修改成功' => '修改成功',
'领取成功' => '领取成功',
'每天只能提现三次' => '每天只能提现三次',
'等待管理员审核' => '等待管理员审核',
'转账成功' => '转账成功',
];