多语言处理
This commit is contained in:
parent
bdf63a2f86
commit
fa9710ec8e
|
|
@ -1,6 +1,12 @@
|
|||
using CloudGaming.DtoModel.Game;
|
||||
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
|
||||
using SKIT.FlurlHttpClient.Wechat.TenpayV3.Models;
|
||||
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
|
|
@ -19,6 +25,11 @@ public static class AppLanguage
|
|||
/// </summary>
|
||||
public static ConcurrentDictionary<string, ConcurrentDictionary<string, ConcurrentDictionary<string, ConcurrentDictionary<string, string>>>> AppConfigLanguages { get; set; } = new ConcurrentDictionary<string, ConcurrentDictionary<string, ConcurrentDictionary<string, ConcurrentDictionary<string, string>>>>();
|
||||
|
||||
/// <summary>
|
||||
/// 游戏特殊配置
|
||||
/// </summary>
|
||||
public static ConcurrentDictionary<string, ConcurrentDictionary<string, ConcurrentDictionary<string, AppGameLanguage>>> AppConfigGameLanguages { get; set; } = new ConcurrentDictionary<string, ConcurrentDictionary<string, ConcurrentDictionary<string, AppGameLanguage>>>();
|
||||
private static string RedisAppLanguageGame = "language:game";
|
||||
/// <summary>
|
||||
/// 获取当前项目的多语言配置
|
||||
/// </summary>
|
||||
|
|
@ -43,7 +54,11 @@ public static class AppLanguage
|
|||
public static ConcurrentDictionary<string, ConcurrentDictionary<string, string>> GetAppLanguage(this AppConfig appConfig, string language)
|
||||
{
|
||||
var dic = GetAppConfigLanguage(appConfig);
|
||||
|
||||
var languages = appConfig.GetLanguages();
|
||||
if (!languages.Contains(language))
|
||||
{
|
||||
language = appConfig.DefaultLanguage;
|
||||
}
|
||||
if (!dic.TryGetValue(language, out var languageDic))
|
||||
{
|
||||
languageDic = new ConcurrentDictionary<string, ConcurrentDictionary<string, string>>();
|
||||
|
|
@ -61,6 +76,7 @@ public static class AppLanguage
|
|||
/// <returns></returns>
|
||||
public static ConcurrentDictionary<string, string> GetAppApiLanguage(this AppConfig appConfig, string language, string api)
|
||||
{
|
||||
|
||||
var dic = GetAppLanguage(appConfig, language);
|
||||
|
||||
if (!dic.TryGetValue(api, out var languageDic))
|
||||
|
|
@ -72,6 +88,18 @@ public static class AppLanguage
|
|||
return languageDic;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取多语言选项
|
||||
/// </summary>
|
||||
/// <param name="dic"></param>
|
||||
/// <param name="key"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetAppLanguageKey(this ConcurrentDictionary<string, string> dic, string key)
|
||||
{
|
||||
if (string.IsNullOrEmpty(key) || dic == null) { return key; }
|
||||
return dic.GetOrAdd(key, (l) => key);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -116,14 +144,82 @@ public static class AppLanguage
|
|||
return json;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取当前接口的多语言配置
|
||||
/// </summary>
|
||||
/// <param name="appConfig"></param>
|
||||
/// <param name="language">api地址</param>
|
||||
/// <returns></returns>
|
||||
public static ConcurrentDictionary<string, AppGameLanguage> GetAppLanguageGame(this AppConfig appConfig, string language)
|
||||
{
|
||||
if (!AppConfigGameLanguages.TryGetValue(appConfig.Identifier, out var dic))
|
||||
{
|
||||
dic = new ConcurrentDictionary<string, ConcurrentDictionary<string, AppGameLanguage>>();
|
||||
AppConfigGameLanguages.TryAdd(appConfig.Identifier, dic);
|
||||
}
|
||||
var languages = appConfig.GetLanguages();
|
||||
if (!languages.Contains(language))
|
||||
{
|
||||
language = appConfig.DefaultLanguage;
|
||||
}
|
||||
if (!dic.TryGetValue(language, out var languageDic))
|
||||
{
|
||||
languageDic = new ConcurrentDictionary<string, AppGameLanguage>();
|
||||
dic.TryAdd(language, languageDic);
|
||||
}
|
||||
return languageDic;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取游戏多语言配置
|
||||
/// </summary>
|
||||
/// <param name="dic"></param>
|
||||
/// <param name="gameInfo"></param>
|
||||
/// <returns></returns>
|
||||
|
||||
public static AppGameLanguage GetAppGameLanguage(this ConcurrentDictionary<string, AppGameLanguage> dic, GameInfo gameInfo)
|
||||
{
|
||||
if (gameInfo == null || string.IsNullOrEmpty(gameInfo.GameId))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
if (dic == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return dic.GetOrAdd(gameInfo.GameId, (x) => new AppGameLanguage()
|
||||
{
|
||||
GameIntroduce = gameInfo.GameIntroduce,
|
||||
GameName = gameInfo.GameName,
|
||||
GameShare = gameInfo.GameShare
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 保存多语言配置
|
||||
/// </summary>
|
||||
/// <param name="appConfig"></param>
|
||||
/// <returns></returns>
|
||||
public static string LanguageGameToJson(AppConfig appConfig, string language)
|
||||
{
|
||||
var settings = new JsonSerializerSettings
|
||||
{
|
||||
ContractResolver = new CamelCasePropertyNamesContractResolver()
|
||||
};
|
||||
var appLanguage = GetAppLanguageGame(appConfig, language);
|
||||
var json = JsonConvert.SerializeObject(appLanguage, settings);
|
||||
return json;
|
||||
}
|
||||
|
||||
public static void SaveLanguageCache(CloudGamingBase cloudGamingBase)
|
||||
{
|
||||
var languages = cloudGamingBase.AppConfig.GetLanguages();
|
||||
foreach (var language in languages)
|
||||
{
|
||||
var json = LanguageToJson(cloudGamingBase.AppConfig, language);
|
||||
cloudGamingBase.RedisCache.StringSet($"language:{language}", json);
|
||||
|
||||
cloudGamingBase.RedisCache.StringSet($"language:common:{language}", json);
|
||||
var json1 = LanguageGameToJson(cloudGamingBase.AppConfig, language);
|
||||
cloudGamingBase.RedisCache.StringSet($"{RedisAppLanguageGame}:{language}", json1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -100,7 +100,7 @@ public class CloudGamingCache
|
|||
{
|
||||
if (gameEntityCache == null)
|
||||
{
|
||||
gameEntityCache = new GameEntityCache(_gamingBase.Dao, _gamingBase.RedisCache, _gamingBase.Mapper, _gamingBase.AppConfig);
|
||||
gameEntityCache = new GameEntityCache(_gamingBase.Dao, _gamingBase.RedisCache, _gamingBase.Mapper, _gamingBase.AppConfig, _gamingBase.AppRequestInfo.Language);
|
||||
}
|
||||
return gameEntityCache;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,27 +6,20 @@ using CloudGaming.Code.Config;
|
|||
using CloudGaming.Code.DataAccess;
|
||||
using CloudGaming.DtoModel;
|
||||
using CloudGaming.DtoModel.Game;
|
||||
using CloudGaming.GameModel.Db.Db_Game;
|
||||
|
||||
using HuanMeng.DotNetCore.CacheHelper;
|
||||
using HuanMeng.DotNetCore.CacheHelper.Contract;
|
||||
using HuanMeng.DotNetCore.Redis;
|
||||
|
||||
using Newtonsoft.Json;
|
||||
|
||||
using Org.BouncyCastle.Utilities.Collections;
|
||||
|
||||
using StackExchange.Redis;
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace CloudGaming.Code.Cache.Special
|
||||
{
|
||||
/// <summary>
|
||||
/// 游戏缓存表
|
||||
/// </summary>
|
||||
public class GameEntityCache(DAO dao, IDatabase database, IMapper mapper, AppConfig appConfig,string language) : CommonDataEntityCache<GameInfo>(GameEntityCache.GameEntityCacheLock, 60 * 60 * 24 * 7), ICacheClearLocalData
|
||||
public class GameEntityCache(DAO dao, IDatabase database, IMapper mapper, AppConfig appConfig, string language) : CommonDataEntityCache<GameInfo>(GameEntityCache.GameEntityCacheLock, 60 * 60 * 24 * 7), ICacheClearLocalData
|
||||
{
|
||||
public static object GameEntityCacheLock;
|
||||
|
||||
|
|
@ -40,16 +33,37 @@ namespace CloudGaming.Code.Cache.Special
|
|||
var gameCbtList = dao.DaoPhone.Context.T_GameCBT.AsNoTracking().Where(it => it.IsOnline).ToList() ?? new List<T_GameCBT>();
|
||||
var gameListDict = dao.DaoGame.Context.T_Game_List.AsNoTracking().ToDictionary(g => g.GameId);
|
||||
var gameChildList = dao.DaoGame.Context.T_Game_ChildList.AsNoTracking().ToList();
|
||||
var gameTypesDict = dao.DaoGame.Context.T_Game_Types.AsNoTracking().ToDictionary(type => type.TypeId);
|
||||
var gameTagsDict = dao.DaoGame.Context.T_Game_Tags.AsNoTracking().ToDictionary(tag => tag.TagId);
|
||||
var gameTypesDict = dao.DaoGame.Context.T_Game_Types.AsNoTracking().ToDictionary(type => type.TypeId) ?? new Dictionary<int, T_Game_Types>();
|
||||
var gameTagsDict = dao.DaoGame.Context.T_Game_Tags.AsNoTracking().ToDictionary(tag => tag.TagId) ?? new Dictionary<int, T_Game_Tags>();
|
||||
//多语言处理
|
||||
#region 游戏类型处理
|
||||
var gameExtendTypeDic = AppLanguage.GetAppApiLanguage(appConfig, language, "system.game.extend.type");
|
||||
if (gameTypesDict.Count > 0)
|
||||
{
|
||||
foreach (var gameType in gameTypesDict)
|
||||
{
|
||||
gameType.Value.TypeName = gameExtendTypeDic.GetAppLanguageKey(gameType.Value.TypeName);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
#region 游戏标签处理
|
||||
var gameExtendTagDic = AppLanguage.GetAppApiLanguage(appConfig, language, "system.game.extend.tag");
|
||||
if (gameTagsDict.Count > 0)
|
||||
{
|
||||
foreach (var gameTag in gameTagsDict)
|
||||
{
|
||||
gameTag.Value.TagName = gameExtendTagDic.GetAppLanguageKey(gameTag.Value.TagName);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
//游戏分享头像
|
||||
var gameUserShare = dao.DaoGame.Context.T_Game_UserShare.AsNoTracking().GroupBy(it => it.GameId).ToDictionary(it => it.Key, it => it.Last().NickName);
|
||||
//默认游戏钻石消耗
|
||||
var defaultConsumeDiamondNumHour = 0;
|
||||
var appConfig = dao.DaoExt.Context.T_App_Config.AsNoTracking().Where(it => it.ConfigType == 3).ToList();
|
||||
if (appConfig != null && appConfig.Count > 0)
|
||||
var appConfigs = dao.DaoExt.Context.T_App_Config.AsNoTracking().Where(it => it.ConfigType == 3).ToList();
|
||||
if (appConfigs != null && appConfigs.Count > 0)
|
||||
{
|
||||
var appConfigList = mapper.Map<List<AppConfigCache>>(appConfig);
|
||||
var appConfigList = mapper.Map<List<AppConfigCache>>(appConfigs);
|
||||
var config = appConfigList.GetAppConfig(3, 1, null);
|
||||
|
||||
if (!string.IsNullOrEmpty(config?.ConfigValue))
|
||||
|
|
@ -57,6 +71,7 @@ namespace CloudGaming.Code.Cache.Special
|
|||
int.TryParse(config?.ConfigValue, out defaultConsumeDiamondNumHour);
|
||||
}
|
||||
}
|
||||
var gameLanguageDic = AppLanguage.GetAppLanguageGame(appConfig, language);
|
||||
var faker = new Faker("zh_CN");
|
||||
var gameInfos = gameCbtList
|
||||
.Where(gameCbt => gameListDict.ContainsKey(gameCbt.GameId))
|
||||
|
|
@ -74,6 +89,14 @@ namespace CloudGaming.Code.Cache.Special
|
|||
NickName = chineseName;
|
||||
}
|
||||
gameInfo.GameShare = $"{NickName}";
|
||||
var gameLanguageInfo = gameLanguageDic.GetAppGameLanguage(gameInfo);
|
||||
if (gameLanguageInfo != null)
|
||||
{
|
||||
gameInfo.GameName = gameLanguageInfo.GameName;
|
||||
gameInfo.GameIntroduce = gameLanguageInfo.GameIntroduce;
|
||||
gameInfo.GameShare = gameLanguageInfo.GameShare;
|
||||
}
|
||||
gameInfo.GameIntroduce = $"<div style=\"color:#ffffff !important;\">{gameInfo.GameIntroduce}</div>";
|
||||
gameInfo.GameShareUserIcon = 90001;
|
||||
if (gameInfo.ConsumeDiamondNumHour == 0)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -97,7 +97,7 @@ public class CustomResultFilter : IResultFilter
|
|||
var languageDic = _appConfig.GetAppApiLanguage(c, apiPrefix);
|
||||
var dic = value.ToDictionaryOrList(false, apiPrefix, it => cloudGamingBase.Cache.AppImageCache[it], (value, prefix, isArray) =>
|
||||
{
|
||||
if (value.ContainsChineseOptimized())
|
||||
if (!isArray && value.ContainsChineseOptimized())
|
||||
{
|
||||
//中文
|
||||
var v = languageDic.GetOrAdd(prefix, value);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,29 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace CloudGaming.AppConfigModel
|
||||
{
|
||||
/// <summary>
|
||||
/// 游戏多语言
|
||||
/// </summary>
|
||||
public class AppGameLanguage
|
||||
{
|
||||
public AppGameLanguage() { }
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public string GameName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 游戏简介
|
||||
/// </summary>
|
||||
public string GameIntroduce { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 游戏分享人
|
||||
/// </summary>
|
||||
public string GameShare { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -35,7 +35,7 @@ namespace CloudGaming.DtoModel.Game
|
|||
gameInfo.GameIsSaveFile = gameList.GameIsSaveFile;
|
||||
gameInfo.GameIsAdapter = gameList.GameIsAdapter;
|
||||
gameInfo.GameCloudId = gameList.GameCloudId;
|
||||
gameInfo.GameIntroduce = $"<div style=\"color:#ffffff !important;\">{gameList.GameIntroduce}</div>";
|
||||
gameInfo.GameIntroduce = $"{gameList.GameIntroduce}";
|
||||
gameInfo.ScreenOrientation = gameList.ScreenOrientation;
|
||||
gameInfo.GameIsEditionMouse = gameList.GameIsEditionMouse;
|
||||
gameInfo.SteamId = gameList.SteamId;
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user