书籍列表接口新增参数
This commit is contained in:
@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
)
|
||||
@ -111,13 +112,20 @@ var languageMap = map[string]map[string]string{
|
||||
"user_follow_author_create_failed": "关注作者失败",
|
||||
"user_follow_author_not_found": "关注记录不存在",
|
||||
"user_follow_author_delete_failed": "取消关注失败",
|
||||
"user_follow_author_update_failed": "关注作者更新失败",
|
||||
// 作者相关
|
||||
"author_query_failed": "作者查询失败",
|
||||
"author_user_exists": "该用户已绑定作者",
|
||||
"author_create_failed": "作者创建失败",
|
||||
"author_not_found": "作者不存在",
|
||||
"author_update_failed": "作者更新失败",
|
||||
"author_delete_failed": "作者删除失败",
|
||||
"author_query_failed": "作者查询失败",
|
||||
"author_user_exists": "该用户已绑定作者",
|
||||
"author_create_failed": "作者创建失败",
|
||||
"author_not_found": "作者不存在",
|
||||
"author_update_failed": "作者更新失败",
|
||||
"author_delete_failed": "作者删除失败",
|
||||
"author_info_failed": "获取作者信息失败",
|
||||
"not_author": "当前用户不是作者",
|
||||
"author_id_required": "作者ID不能为空",
|
||||
"author_review_failed": "作者审核失败",
|
||||
"author_review_status_invalid": "审核状态无效",
|
||||
"author_review_remark_too_long": "审核备注过长",
|
||||
// 书架相关
|
||||
"bookshelve_query_failed": "书架查询失败",
|
||||
"bookshelve_exists": "该书已在书架中",
|
||||
@ -142,6 +150,60 @@ var languageMap = map[string]map[string]string{
|
||||
"bookshelf_update_failed": "书架更新失败",
|
||||
"chapter_count_failed": "章节统计失败",
|
||||
"read_chapter_count_failed": "已读章节统计失败",
|
||||
// 图片上传相关
|
||||
"image_file_required": "图片文件不能为空",
|
||||
"image_type_invalid": "只允许上传图片文件",
|
||||
"image_format_invalid": "仅支持 jpg、png、gif、webp 格式的图片",
|
||||
"image_size_exceeded": "图片大小不能超过1MB",
|
||||
"image_read_failed": "无法读取图片内容",
|
||||
"image_upload_failed": "图片上传失败",
|
||||
// 推荐相关
|
||||
"book_recommendation_query_failed": "推荐查询失败",
|
||||
"book_recommendation_exists": "该类型下该书籍已存在推荐",
|
||||
"book_recommendation_create_failed": "推荐创建失败",
|
||||
"book_recommendation_not_found": "推荐不存在",
|
||||
"book_recommendation_update_failed": "推荐更新失败",
|
||||
"book_recommendation_delete_failed": "推荐删除失败",
|
||||
// 签到奖励规则相关
|
||||
"sign_in_reward_rule_query_failed": "签到奖励规则查询失败",
|
||||
"sign_in_reward_rule_exists": "规则名称已存在",
|
||||
"sign_in_reward_rule_create_failed": "签到奖励规则创建失败",
|
||||
"sign_in_reward_rule_not_found": "签到奖励规则不存在",
|
||||
"sign_in_reward_rule_update_failed": "签到奖励规则更新失败",
|
||||
"sign_in_reward_rule_delete_failed": "签到奖励规则删除失败",
|
||||
// 签到奖励明细相关
|
||||
"sign_in_reward_detail_query_failed": "签到奖励明细查询失败",
|
||||
"sign_in_reward_detail_exists": "该规则下该天奖励已存在",
|
||||
"sign_in_reward_detail_create_failed": "签到奖励明细创建失败",
|
||||
"sign_in_reward_detail_not_found": "签到奖励明细不存在",
|
||||
"sign_in_reward_detail_update_failed": "签到奖励明细更新失败",
|
||||
"sign_in_reward_detail_delete_failed": "签到奖励明细删除失败",
|
||||
// 签到日志相关
|
||||
"user_sign_in_log_query_failed": "签到日志查询失败",
|
||||
"user_sign_in_log_create_failed": "签到日志创建失败",
|
||||
"user_points_log_create_failed": "积分日志创建失败",
|
||||
"user_points_update_failed": "用户积分更新失败",
|
||||
// 用户阅读历史相关
|
||||
"user_read_history_query_failed": "历史记录查询失败",
|
||||
"user_read_history_update_failed": "历史记录更新失败",
|
||||
"user_read_history_create_failed": "历史记录创建失败",
|
||||
"user_read_history_delete_failed": "历史记录删除失败",
|
||||
"user_read_history_not_found": "历史记录不存在",
|
||||
"user_id_or_book_id_or_chapter_id_invalid": "用户ID、书籍ID或章节ID无效",
|
||||
"user_id_or_book_ids_invalid": "用户ID或书籍ID列表无效",
|
||||
// 任务相关
|
||||
"task_query_failed": "任务查询失败",
|
||||
"task_add_failed": "任务添加失败",
|
||||
"task_edit_failed": "任务编辑失败",
|
||||
"task_delete_failed": "任务删除失败",
|
||||
"task_not_found": "任务不存在",
|
||||
"task_log_query_failed": "任务日志查询失败",
|
||||
// 任务类型相关
|
||||
"task_type_query_failed": "任务类型查询失败",
|
||||
"task_type_add_failed": "任务类型添加失败",
|
||||
"task_type_edit_failed": "任务类型编辑失败",
|
||||
"task_type_delete_failed": "任务类型删除失败",
|
||||
"task_type_not_found": "任务类型不存在",
|
||||
},
|
||||
"en-US": {
|
||||
"hello": "Hello World!",
|
||||
@ -239,12 +301,20 @@ var languageMap = map[string]map[string]string{
|
||||
"user_follow_author_not_found": "Follow record not found",
|
||||
"user_follow_author_delete_failed": "Unfollow failed",
|
||||
// Author related
|
||||
"author_query_failed": "Author query failed",
|
||||
"author_user_exists": "User already has an author profile",
|
||||
"author_create_failed": "Author creation failed",
|
||||
"author_not_found": "Author not found",
|
||||
"author_update_failed": "Author update failed",
|
||||
"author_delete_failed": "Author deletion failed",
|
||||
"author_query_failed": "Author query failed",
|
||||
"author_user_exists": "User already has an author profile",
|
||||
"author_create_failed": "Author creation failed",
|
||||
"author_not_found": "Author not found",
|
||||
"author_update_failed": "Author update failed",
|
||||
"author_delete_failed": "Author deletion failed",
|
||||
"author_info_failed": "Failed to get author info",
|
||||
"not_author": "Current user is not an author",
|
||||
"author_id_required": "Author ID cannot be empty",
|
||||
"author_review_failed": "Author review failed",
|
||||
"author_review_status_invalid": "Invalid review status",
|
||||
"author_review_remark_too_long": "Review remark is too long",
|
||||
"user_follow_author_update_failed": "Author update failed",
|
||||
|
||||
// Bookshelve related
|
||||
"bookshelve_query_failed": "Bookshelf query failed",
|
||||
"bookshelve_exists": "Book already in bookshelf",
|
||||
@ -269,11 +339,92 @@ var languageMap = map[string]map[string]string{
|
||||
"bookshelf_update_failed": "Bookshelf update failed",
|
||||
"chapter_count_failed": "Chapter count failed",
|
||||
"read_chapter_count_failed": "Read chapter count failed",
|
||||
// 图片上传相关
|
||||
"image_file_required": "Image file is required",
|
||||
"image_type_invalid": "Only image files are allowed",
|
||||
"image_format_invalid": "Only jpg, png, gif, webp formats are supported",
|
||||
"image_size_exceeded": "Image size cannot exceed 1MB",
|
||||
"image_read_failed": "Failed to read image file",
|
||||
"image_upload_failed": "Image upload failed",
|
||||
// Recommendation related
|
||||
"book_recommendation_query_failed": "Recommendation query failed",
|
||||
"book_recommendation_exists": "The book already exists in this recommendation type",
|
||||
"book_recommendation_create_failed": "Recommendation creation failed",
|
||||
"book_recommendation_not_found": "Recommendation not found",
|
||||
"book_recommendation_update_failed": "Recommendation update failed",
|
||||
"book_recommendation_delete_failed": "Recommendation deletion failed",
|
||||
// 签到奖励规则相关
|
||||
"sign_in_reward_rule_query_failed": "Sign-in reward rule query failed",
|
||||
"sign_in_reward_rule_exists": "Rule name already exists",
|
||||
"sign_in_reward_rule_create_failed": "Sign-in reward rule creation failed",
|
||||
"sign_in_reward_rule_not_found": "Sign-in reward rule not found",
|
||||
"sign_in_reward_rule_update_failed": "Sign-in reward rule update failed",
|
||||
"sign_in_reward_rule_delete_failed": "Sign-in reward rule deletion failed",
|
||||
// Sign-in reward detail related
|
||||
"sign_in_reward_detail_query_failed": "Sign-in reward detail query failed",
|
||||
"sign_in_reward_detail_exists": "Reward for this day already exists under the rule",
|
||||
"sign_in_reward_detail_create_failed": "Sign-in reward detail creation failed",
|
||||
"sign_in_reward_detail_not_found": "Sign-in reward detail not found",
|
||||
"sign_in_reward_detail_update_failed": "Sign-in reward detail update failed",
|
||||
"sign_in_reward_detail_delete_failed": "Sign-in reward detail deletion failed",
|
||||
// Sign-in log related
|
||||
"user_sign_in_log_query_failed": "Sign-in log query failed",
|
||||
"user_sign_in_log_create_failed": "Sign-in log creation failed",
|
||||
"user_points_log_create_failed": "Points log creation failed",
|
||||
"user_points_update_failed": "User points update failed",
|
||||
// User read history related
|
||||
"user_read_history_query_failed": "Read history query failed",
|
||||
"user_read_history_update_failed": "Read history update failed",
|
||||
"user_read_history_create_failed": "Read history creation failed",
|
||||
"user_read_history_delete_failed": "Read history deletion failed",
|
||||
"user_read_history_not_found": "Read history not found",
|
||||
"user_id_or_book_id_or_chapter_id_invalid": "User ID, Book ID or Chapter ID is invalid",
|
||||
"user_id_or_book_ids_invalid": "User ID or Book IDs is invalid",
|
||||
// Task related
|
||||
"task_query_failed": "Task query failed",
|
||||
"task_add_failed": "Task add failed",
|
||||
"task_edit_failed": "Task edit failed",
|
||||
"task_delete_failed": "Task delete failed",
|
||||
"task_not_found": "Task not found",
|
||||
"task_log_query_failed": "Task log query failed",
|
||||
// TaskType related
|
||||
"task_type_query_failed": "Task type query failed",
|
||||
"task_type_add_failed": "Task type add failed",
|
||||
"task_type_edit_failed": "Task type edit failed",
|
||||
"task_type_delete_failed": "Task type delete failed",
|
||||
"task_type_not_found": "Task type not found",
|
||||
},
|
||||
}
|
||||
|
||||
// I18n 单例结构体
|
||||
type I18n struct {
|
||||
languageMap map[string]map[string]string
|
||||
mu sync.RWMutex
|
||||
}
|
||||
|
||||
var (
|
||||
instance *I18n
|
||||
once sync.Once
|
||||
)
|
||||
|
||||
// GetInstance 获取单例实例
|
||||
func GetInstance() *I18n {
|
||||
once.Do(func() {
|
||||
instance = &I18n{
|
||||
languageMap: languageMap,
|
||||
}
|
||||
})
|
||||
return instance
|
||||
}
|
||||
|
||||
// init 初始化函数
|
||||
func init() {
|
||||
// 确保单例实例被创建
|
||||
GetInstance()
|
||||
}
|
||||
|
||||
// GetLanguage 从请求头或查询参数获取语言设置
|
||||
func GetLanguage(ctx context.Context) string {
|
||||
func (i *I18n) GetLanguage(ctx context.Context) string {
|
||||
// 优先从请求头获取
|
||||
if r := g.RequestFromCtx(ctx); r != nil {
|
||||
// 从 Accept-Language 头获取
|
||||
@ -302,9 +453,12 @@ func GetLanguage(ctx context.Context) string {
|
||||
}
|
||||
|
||||
// T 翻译消息
|
||||
func T(ctx context.Context, key string) string {
|
||||
lang := GetLanguage(ctx)
|
||||
if messages, exists := languageMap[lang]; exists {
|
||||
func (i *I18n) T(ctx context.Context, key string) string {
|
||||
lang := i.GetLanguage(ctx)
|
||||
i.mu.RLock()
|
||||
defer i.mu.RUnlock()
|
||||
|
||||
if messages, exists := i.languageMap[lang]; exists {
|
||||
if message, exists := messages[key]; exists {
|
||||
return message
|
||||
}
|
||||
@ -312,7 +466,7 @@ func T(ctx context.Context, key string) string {
|
||||
|
||||
// 如果当前语言没有找到,尝试默认语言
|
||||
if lang != DefaultLanguage {
|
||||
if messages, exists := languageMap[DefaultLanguage]; exists {
|
||||
if messages, exists := i.languageMap[DefaultLanguage]; exists {
|
||||
if message, exists := messages[key]; exists {
|
||||
return message
|
||||
}
|
||||
@ -324,14 +478,27 @@ func T(ctx context.Context, key string) string {
|
||||
}
|
||||
|
||||
// Tf 翻译消息并格式化
|
||||
func Tf(ctx context.Context, key string, args ...interface{}) string {
|
||||
message := T(ctx, key)
|
||||
func (i *I18n) Tf(ctx context.Context, key string, args ...interface{}) string {
|
||||
message := i.T(ctx, key)
|
||||
if len(args) > 0 {
|
||||
message = fmt.Sprintf(message, args...)
|
||||
}
|
||||
return message
|
||||
}
|
||||
|
||||
// 为了保持向后兼容,提供全局函数
|
||||
func GetLanguage(ctx context.Context) string {
|
||||
return GetInstance().GetLanguage(ctx)
|
||||
}
|
||||
|
||||
func T(ctx context.Context, key string) string {
|
||||
return GetInstance().T(ctx, key)
|
||||
}
|
||||
|
||||
func Tf(ctx context.Context, key string, args ...interface{}) string {
|
||||
return GetInstance().Tf(ctx, key, args...)
|
||||
}
|
||||
|
||||
// isSupportedLanguage 检查是否为支持的语言
|
||||
func isSupportedLanguage(lang string) bool {
|
||||
for _, supported := range SupportedLanguages {
|
||||
|
||||
Reference in New Issue
Block a user