This commit is contained in:
2025-12-14 02:43:40 +08:00
parent 0741368b44
commit 54e9c7962d
10 changed files with 220 additions and 278 deletions

View File

@@ -1,6 +1,9 @@
using Atomx.Utils.Json;
using Atomx.Data;
using Atomx.Data.CacheServices;
using Atomx.Utils.Json;
using Hangfire;
using Microsoft.Extensions.Logging;
using System.Security.Cryptography;
using System.Text.Json;
namespace Atomx.Core.Jos
@@ -12,9 +15,13 @@ namespace Atomx.Core.Jos
public class LocalizationJob
{
readonly ILogger<LocalizationJob> _logger;
public LocalizationJob(ILogger<LocalizationJob> logger)
readonly DataContext _dbContext;
readonly ICacheService _cacheService;
public LocalizationJob(ILogger<LocalizationJob> logger, DataContext dataContext, ICacheService cacheService)
{
_logger = logger;
_dbContext = dataContext;
_cacheService = cacheService;
}
/// <summary>
@@ -30,46 +37,58 @@ namespace Atomx.Core.Jos
var fileName = $"{culture}.json";
var filePath = Path.Combine(path, fileName);
if (!Directory.Exists(filePath))
if (!Directory.Exists(path))
{
Directory.CreateDirectory(filePath);
_logger.LogInformation("Created Resources directory: {Path}", filePath);
Directory.CreateDirectory(path);
_logger.LogInformation("Created Resources directory: {Path}", path);
}
var json = await File.ReadAllTextAsync(filePath);
var fileData = JsonSerializer.Deserialize<Dictionary<string, string>>(json, new JsonSerializerOptions
var fileData = new Dictionary<string, string>();
if (File.Exists(filePath))
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
});
if (fileData == null)
{
fileData = new Dictionary<string, string>();
var json = await File.ReadAllTextAsync(filePath);
fileData = JsonSerializer.Deserialize<Dictionary<string, string>>(json, new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
}) ?? new Dictionary<string, string>();
}
foreach (var item in translations)
{
if (fileData.ContainsKey(item.Key))
{
fileData[item.Key] = item.Value;
}
else
{
fileData.Add(item.Key, item.Value);
}
fileData[item.Key] = item.Value;
}
var options = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
WriteIndented = true
};
json = JsonSerializer.Serialize(fileData, options);
await File.WriteAllTextAsync(filePath, json);
var updatedJson = JsonSerializer.Serialize(fileData, options);
await File.WriteAllTextAsync(filePath, updatedJson);
// 更新文件后,更新数据库中的资源版本
string fileHash;
using (var sha256 = SHA256.Create())
using (var stream = File.OpenRead(filePath))
{
var hashBytes = sha256.ComputeHash(stream);
fileHash = BitConverter.ToString(hashBytes).Replace("-", "").ToLowerInvariant();
}
var language = _dbContext.Languages.FirstOrDefault(l => l.Culture == culture);
if (language != null)
{
language.UpdateTime = DateTime.UtcNow;
language.ResourceVersion = fileHash;
await _dbContext.SaveChangesAsync();
await _cacheService.GetLanguageById(language.Id, language);
}
_logger.LogInformation("Saved localization file for culture: {Culture} with {Count} translations",
culture, translations.Count);
_logger.LogInformation("Saved localization file for culture: {Culture} with {Count} translations. File hash: {Hash}",
culture, translations.Count, fileHash);
}
catch(Exception ex) {
catch (Exception ex)
{
_logger.LogError(ex, "Error saving localization file for culture: {Culture}", culture);
}
}