You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
6.4 KiB
6.4 KiB
前台Mobile端多语言支持实现总结
实现内容
已成功为前台mobile端所有接口添加中英双语支持,确保所有错误消息和响应文本都能根据 Accept-Language 头部自动切换语言。
修改的文件
1. 语言文件
中文语言文件 (lang/zh-CN/mobile.php)
<?php
return [
// 用户管理
'user' => [
'code_required' => 'code必填',
'code_invalid' => 'code异常',
'update_success' => '更新成功',
'mobile_required' => 'code必填',
'id_required' => 'Id必填',
],
// 拜访管理
'visit' => [
'name_required' => '名称必填',
'visit_area_id_required' => '区域id必填',
'accept_admin_id_required' => '接待人必填',
'accompany_id_required' => '陪同人必填',
'accept_admin_department_not_exists' => '接待人部门不存在',
'accept_admin_department_manager_not_exists' => '接待人部门不存在负责人',
'update_success' => '更新成功',
'type_required' => '类型必填',
'not_studied' => '未学习',
'study_expired' => '学习过期',
'study_valid' => '学习有效中',
'idcard_required' => '身份证数组必填',
],
// 通用
'common' => [
'mobile_required' => '手机号必填',
'mobile_numeric' => '手机号格式错误',
'type_required' => '类型必填',
'send_too_frequent' => '请勿频繁发送',
'send_success' => '发送成功',
'send_failed' => '发送失败',
],
];
英文语言文件 (lang/en/mobile.php)
<?php
return [
// User Management
'user' => [
'code_required' => 'Code is required',
'code_invalid' => 'Code is invalid',
'update_success' => 'Update successful',
'mobile_required' => 'Code is required',
'id_required' => 'Id is required',
],
// Visit Management
'visit' => [
'name_required' => 'Name is required',
'visit_area_id_required' => 'Visit area id is required',
'accept_admin_id_required' => 'Accept admin is required',
'accompany_id_required' => 'Accompany person is required',
'accept_admin_department_not_exists' => 'Accept admin department does not exist',
'accept_admin_department_manager_not_exists' => 'Accept admin department manager does not exist',
'update_success' => 'Update successful',
'type_required' => 'Type is required',
'not_studied' => 'Not studied',
'study_expired' => 'Study expired',
'study_valid' => 'Study is valid',
'idcard_required' => 'Id card array is required',
],
// Common
'common' => [
'mobile_required' => 'Mobile is required',
'mobile_numeric' => 'Mobile format error',
'type_required' => 'Type is required',
'send_too_frequent' => 'Please do not send too frequently',
'send_success' => 'Send successful',
'send_failed' => 'Send failed',
],
];
2. 控制器修改
UserController.php
- 修正了所有硬编码的中文错误消息
- 使用
__('mobile.user.xxx')格式调用多语言文本 - 修正了
Config::get()为Config::all()的调用错误
VisitController.php
- 修正了所有硬编码的中文错误消息
- 使用
__('mobile.visit.xxx')格式调用多语言文本 - 包括验证消息、业务错误消息和成功消息
CommonController.php
- 修正了所有硬编码的中文错误消息
- 使用
__('mobile.common.xxx')格式调用多语言文本
3. 路由配置修改
routes/api.php
// 前台
Route::get('mobile/user/login', [\App\Http\Controllers\Mobile\UserController::class, 'login'])->middleware('set.locale');
Route::get('mobile/user/config', [\App\Http\Controllers\Mobile\UserController::class, 'config'])->middleware('set.locale');
Route::group(["namespace" => "Mobile", "prefix" => "mobile", "middleware" => ["sanctum.jwt:mobile", "set.locale"]], function () {
// ... 其他路由
});
支持的接口
用户管理接口
GET /api/mobile/user/login- 用户登录GET /api/mobile/user/config- 获取配置信息POST /api/mobile/user/save- 更新用户信息GET /api/mobile/user/mobile- 获取手机号GET /api/mobile/user/show- 获取用户信息GET /api/mobile/user/my-visit- 我的拜访列表GET /api/mobile/user/my-visit-detail- 我的拜访详情
拜访管理接口
POST /api/mobile/visit/visit-save- 保存拜访GET /api/mobile/visit/get-ask- 获取学习内容GET /api/mobile/visit/visit-area- 拜访区域列表GET /api/mobile/visit/visit-time- 拜访时间段列表GET /api/mobile/visit/ask-log- 获取学习记录POST /api/mobile/visit/ask-save- 保存学习记录POST /api/mobile/visit/idcard-check- 身份证检查
通用接口
GET /api/mobile/common/send-sms- 发送短信
多语言使用方式
客户端请求
// 中文请求
fetch('/api/mobile/user/login', {
headers: {
'Accept-Language': 'zh-CN'
}
});
// 英文请求
fetch('/api/mobile/user/login', {
headers: {
'Accept-Language': 'en'
}
});
响应示例
中文响应
{
"errcode": 10001,
"errmsg": "code必填"
}
英文响应
{
"errcode": 10001,
"errmsg": "Code is required"
}
测试结果
✅ 所有测试通过
- ✅ mobile login endpoint supports multilingual
- ✅ mobile config endpoint supports multilingual
- ✅ mobile visit area endpoint supports multilingual
- ✅ mobile visit time endpoint supports multilingual
技术实现
- 中间件支持: 所有mobile端路由都应用了
set.locale中间件 - 语言检测: 通过
Accept-Language头部自动检测语言 - 统一响应: 使用
ApiResponsetrait 确保响应格式一致 - 错误码标准化: 使用
ResponseCode常量确保错误码统一
优势
- 国际化支持: 完整的中英双语支持
- 用户体验: 根据用户语言偏好自动切换
- 维护性: 集中的语言文件管理
- 一致性: 与系统其他部分保持统一的响应格式
- 扩展性: 易于添加更多语言支持
总结
前台mobile端已成功实现完整的中英双语支持,所有接口都能根据 Accept-Language 头部自动切换语言,提供更好的国际化用户体验。