多语言v1.01
This commit is contained in:
parent
afae8967ee
commit
bdf63a2f86
|
|
@ -1,4 +1,5 @@
|
|||
using CloudGaming.Api.Base;
|
||||
using CloudGaming.Code.AppExtend;
|
||||
using CloudGaming.Code.Config;
|
||||
using CloudGaming.DtoModel;
|
||||
|
||||
|
|
@ -39,4 +40,15 @@ public class AppController : CloudGamingControllerBase
|
|||
return appConfig;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 保存多语言缓存
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
public bool SaveLanguageCache()
|
||||
{
|
||||
CloudGamingBase cloudGamingBase = new CloudGamingBase(ServiceProvider);
|
||||
AppLanguage.SaveLanguageCache(cloudGamingBase);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -72,6 +72,8 @@ public static class AppLanguage
|
|||
return languageDic;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 加载多语言配置
|
||||
/// </summary>
|
||||
|
|
@ -103,14 +105,25 @@ public static class AppLanguage
|
|||
/// </summary>
|
||||
/// <param name="appConfig"></param>
|
||||
/// <returns></returns>
|
||||
public static string LanguageToJson(AppConfig appConfig)
|
||||
public static string LanguageToJson(AppConfig appConfig, string language)
|
||||
{
|
||||
var settings = new JsonSerializerSettings
|
||||
{
|
||||
ContractResolver = new CamelCasePropertyNamesContractResolver()
|
||||
};
|
||||
var appLanguage = GetAppConfigLanguage(appConfig);
|
||||
var appLanguage = GetAppLanguage(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);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ namespace CloudGaming.Code.AppExtend
|
|||
/// <summary>
|
||||
/// app 请求信息
|
||||
/// </summary>
|
||||
public class AppRequestConfig(HttpRequest? httpRequest)
|
||||
public class AppRequestConfig(HttpRequest? httpRequest, AppConfig appConfig)
|
||||
{
|
||||
private string channel { get; set; }
|
||||
/// <summary>
|
||||
|
|
@ -106,7 +106,7 @@ namespace CloudGaming.Code.AppExtend
|
|||
{
|
||||
if (!(httpRequest?.Headers.TryGetValue("Language", out var _language) ?? false))
|
||||
{
|
||||
_language = "zh";
|
||||
_language = appConfig.DefaultLanguage ?? "zh";
|
||||
}
|
||||
language = _language;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
using CloudGaming.Code.Game;
|
||||
|
||||
using HuanMeng.DotNetCore.Redis;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
|
@ -28,7 +30,7 @@ public class AppService(IServiceProvider scopeFactory) : IHostedService
|
|||
CloudGamingBase cloudGamingBase = new CloudGamingBase(scopedProvider);
|
||||
//加载图片缓存
|
||||
cloudGamingBase.Cache.AppImageCache.ReloadData();
|
||||
//cloudGamingBase.RedisCache.StringGet("language");
|
||||
//cloudGamingBase.RedisCache.StringGet("languages");
|
||||
//appConfig.InitAppLanguage();
|
||||
|
||||
}
|
||||
|
|
@ -38,6 +40,28 @@ public class AppService(IServiceProvider scopeFactory) : IHostedService
|
|||
public Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
|
||||
//var appConfigs = AppConfigurationExtend.AppConfigs.Where(it => it.Key != "default")
|
||||
// .Select(it => it.Value)
|
||||
// .ToList();
|
||||
|
||||
//// 为每个项目启动一个独立的任务
|
||||
//foreach (var appConfig in appConfigs)
|
||||
//{
|
||||
// using var scope = scopeFactory.CreateScope();
|
||||
// var scopedProvider = scope.ServiceProvider;
|
||||
// var app = scopedProvider.GetRequiredService<AppConfig>();
|
||||
// appConfig.ToAppConfig(app);
|
||||
// var languages = appConfig.GetLanguages();
|
||||
// CloudGamingBase cloudGamingBase = new CloudGamingBase(scopedProvider);
|
||||
// ////加载图片缓存
|
||||
// //cloudGamingBase.Cache.AppImageCache.ReloadData();
|
||||
|
||||
// foreach (var language in languages)
|
||||
// {
|
||||
// var json = AppLanguage.LanguageToJson(appConfig, language);
|
||||
// cloudGamingBase.RedisCache.StringSet($"languages:{language}", json);
|
||||
// }
|
||||
//}
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -219,7 +219,7 @@ namespace CloudGaming.Code.AppExtend
|
|||
{
|
||||
if (appRequestInfo == null)
|
||||
{
|
||||
appRequestInfo = new AppRequestConfig(HttpContextAccessor?.HttpContext?.Request);
|
||||
appRequestInfo = new AppRequestConfig(HttpContextAccessor?.HttpContext?.Request, AppConfig);
|
||||
}
|
||||
return appRequestInfo;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,14 +26,14 @@ namespace CloudGaming.Code.Cache.Special
|
|||
/// <summary>
|
||||
/// 游戏缓存表
|
||||
/// </summary>
|
||||
public class GameEntityCache(DAO dao, IDatabase database, IMapper mapper, AppConfig appConfig) : 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;
|
||||
|
||||
public override string key => $"{appConfig.Identifier}:game:gameInfo";
|
||||
public override string key => $"{appConfig.Identifier}:game:gameInfo:{language}";
|
||||
|
||||
public string locaKey => $"lock:gameInfo";
|
||||
public string RedisKey => $"cache:game:gameInfo";
|
||||
public string locaKey => $"lock:gameInfo:{language}";
|
||||
public string RedisKey => $"cache:game:gameInfo:{language}";
|
||||
|
||||
public override List<GameInfo> GetDataList()
|
||||
{
|
||||
|
|
@ -65,6 +65,7 @@ namespace CloudGaming.Code.Cache.Special
|
|||
var game = gameListDict[gameCbt.GameId];
|
||||
var gameInfo = mapper.Map<GameInfo>(gameCbt);
|
||||
game.ToGameInfo(gameInfo);
|
||||
//game.GameName
|
||||
gameInfo.GameType = GetGameExtendedAttributes(gameChildList, gameCbt.GameId, 1, gameTypesDict);
|
||||
gameInfo.GameTags = GetGameExtendedAttributes(gameChildList, gameCbt.GameId, 2, gameTagsDict);
|
||||
if (!gameUserShare.TryGetValue(gameCbt.GameId, out string NickName))
|
||||
|
|
|
|||
|
|
@ -1,7 +1,11 @@
|
|||
|
||||
|
||||
using CloudGaming.Code.AppExtend;
|
||||
|
||||
using Newtonsoft.Json;
|
||||
|
||||
using static SKIT.FlurlHttpClient.Wechat.TenpayV3.Models.CreateApplyForSubMerchantApplymentRequest.Types.Business.Types.SaleScene.Types;
|
||||
|
||||
namespace CloudGaming.Code.Filter;
|
||||
|
||||
|
||||
|
|
@ -44,6 +48,7 @@ public class CustomResultFilter : IResultFilter
|
|||
// 获取当前用户的信息
|
||||
if (objectResult.Value != null)
|
||||
{
|
||||
//GetAppApiLanguage
|
||||
var httpContext = context.HttpContext;
|
||||
var path = httpContext.Request.Path.Value ?? "";
|
||||
var apiPrefix = path.Replace('/', '.').TrimStart('.');
|
||||
|
|
@ -88,13 +93,14 @@ public class CustomResultFilter : IResultFilter
|
|||
}
|
||||
}
|
||||
}
|
||||
var language = _appConfig.GetAppLanguage(apiPrefix);
|
||||
string c = cloudGamingBase.AppRequestInfo.Language;
|
||||
var languageDic = _appConfig.GetAppApiLanguage(c, apiPrefix);
|
||||
var dic = value.ToDictionaryOrList(false, apiPrefix, it => cloudGamingBase.Cache.AppImageCache[it], (value, prefix, isArray) =>
|
||||
{
|
||||
if (value.ContainsChineseOptimized())
|
||||
{
|
||||
//中文
|
||||
var v = language.GetOrAdd(value, value);
|
||||
var v = languageDic.GetOrAdd(prefix, value);
|
||||
if (!string.IsNullOrEmpty(v))
|
||||
{
|
||||
return v;
|
||||
|
|
|
|||
|
|
@ -114,7 +114,7 @@ namespace CloudGaming.Code.MiddlewareExtend
|
|||
|
||||
private string GenerateCacheKey(HttpRequest request, AppConfig appConfig)
|
||||
{
|
||||
var appRequestInfo = new AppRequestConfig(request);
|
||||
var appRequestInfo = new AppRequestConfig(request, appConfig);
|
||||
// 基于请求路径和查询参数生成缓存键
|
||||
var cacheKey = $"{appConfig.Identifier}:{request.Path.Value.Replace('/', '.').TrimStart('.')}:{(string.IsNullOrEmpty(request.QueryString.Value) ? "default" : request.QueryString)}:{appRequestInfo.Key}";
|
||||
return cacheKey;
|
||||
|
|
|
|||
|
|
@ -104,7 +104,7 @@ namespace CloudGaming.Code.MiddlewareExtend
|
|||
|
||||
private string GenerateCacheKey(HttpRequest request, AppConfig appConfig)
|
||||
{
|
||||
var appRequestInfo = new AppRequestConfig(request);
|
||||
var appRequestInfo = new AppRequestConfig(request, appConfig);
|
||||
var cacheKey = $"cache:api:{request.Path.Value.Replace('/', '.').TrimStart('.')}:{appRequestInfo.Key}:{(string.IsNullOrEmpty(request.QueryString.Value) ? "default" : request.QueryString)}";
|
||||
return cacheKey;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -107,7 +107,7 @@ public static class ObjectExtensions
|
|||
}
|
||||
if (!string.IsNullOrEmpty(stringValue) && languageFunc != null)
|
||||
{
|
||||
stringValue = languageFunc?.Invoke(stringValue, propertyPath, false) ?? "";
|
||||
stringValue = languageFunc?.Invoke(stringValue, propertyPath, isEnumerable) ?? "";
|
||||
}
|
||||
keyValuePairs[accessor.PropertyName] = stringValue;
|
||||
continue;
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user