Skip to content

Commit fc605e9

Browse files
committed
Started work on the backend
1 parent 3ec6c52 commit fc605e9

File tree

5 files changed

+80
-13
lines changed

5 files changed

+80
-13
lines changed

API/Controllers/LocaleController.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System.Collections.Generic;
2+
using System.Collections.Immutable;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using API.Data;
6+
using API.Services;
7+
using Microsoft.AspNetCore.Mvc;
8+
9+
namespace API.Controllers;
10+
11+
public class LocaleController : BaseApiController
12+
{
13+
private readonly IUnitOfWork _unitOfWork;
14+
private readonly ILocalizationService _localizationService;
15+
private static readonly IReadOnlyList<string> AllLocales = new List<string>() { "en" };
16+
17+
public LocaleController(IUnitOfWork unitOfWork, ILocalizationService localizationService)
18+
{
19+
_unitOfWork = unitOfWork;
20+
_localizationService = localizationService;
21+
}
22+
23+
[HttpGet]
24+
public ActionResult<IEnumerable<string>> GetAllLocales()
25+
{
26+
return Ok(AllLocales);
27+
}
28+
}

API/Data/DataContext.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ protected override void OnModelCreating(ModelBuilder builder)
100100
builder.Entity<AppUserPreferences>()
101101
.Property(b => b.BookReaderWritingStyle)
102102
.HasDefaultValue(WritingStyle.Horizontal);
103+
builder.Entity<AppUserPreferences>()
104+
.Property(b => b.Locale)
105+
.HasDefaultValue("en");
103106

104107
builder.Entity<Library>()
105108
.Property(b => b.AllowScrobbling)

API/Entities/AppUserPreferences.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ public class AppUserPreferences
127127
/// UI Site Global Setting: Should series reviews be shared with all users in the server
128128
/// </summary>
129129
public bool ShareReviews { get; set; } = false;
130+
/// <summary>
131+
/// UI Site Global Setting: The language locale that should be used for the user
132+
/// </summary>
133+
public string Locale { get; set; }
130134

131135
public AppUser AppUser { get; set; } = null!;
132136
public int AppUserId { get; set; }

API/Extensions/ApplicationServiceExtensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ public static void AddApplicationServices(this IServiceCollection services, ICon
6666
services.AddScoped<IPresenceTracker, PresenceTracker>();
6767
services.AddScoped<IImageService, ImageService>();
6868

69+
services.AddScoped<ILocalizationService, LocalizationService>();
70+
6971

7072
services.AddScoped<IScrobblingService, ScrobblingService>();
7173
services.AddScoped<ILicenseService, LicenseService>();

API/Services/LocalizationService.cs

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Text.Json;
4+
using System.Threading.Tasks;
5+
using Microsoft.Extensions.Caching.Memory;
36
using Microsoft.Extensions.Hosting;
47

58
namespace API.Services;
@@ -8,19 +11,21 @@ namespace API.Services;
811

912
public interface ILocalizationService
1013
{
11-
void LoadLanguage(string languageCode);
12-
//string Get(string key);
14+
Task<Dictionary<string, string>> LoadLanguage(string languageCode);
15+
Task<string> Get(string locale, string key, params object[] args);
1316
}
1417

1518
public class LocalizationService : ILocalizationService
1619
{
1720
private readonly IDirectoryService _directoryService;
21+
private readonly IMemoryCache _cache;
1822
private readonly string _localizationDirectory;
19-
private dynamic? _languageLocale;
2023

21-
public LocalizationService(IDirectoryService directoryService, IHostEnvironment environment)
24+
25+
public LocalizationService(IDirectoryService directoryService, IHostEnvironment environment, IMemoryCache cache)
2226
{
2327
_directoryService = directoryService;
28+
_cache = cache;
2429
if (environment.IsDevelopment())
2530
{
2631
_localizationDirectory = directoryService.FileSystem.Path.Join(
@@ -40,20 +45,45 @@ public LocalizationService(IDirectoryService directoryService, IHostEnvironment
4045
/// </summary>
4146
/// <param name="languageCode"></param>
4247
/// <returns></returns>
43-
public void LoadLanguage(string languageCode)
48+
public async Task<Dictionary<string, string>> LoadLanguage(string languageCode)
4449
{
4550
var languageFile = _directoryService.FileSystem.Path.Join(_localizationDirectory, languageCode + ".json");
4651
if (!_directoryService.FileSystem.FileInfo.New(languageFile).Exists)
4752
throw new ArgumentException($"Language {languageCode} does not exist");
4853

49-
var json = _directoryService.FileSystem.File.ReadAllText(languageFile);
50-
_languageLocale = JsonSerializer.Deserialize<dynamic>(json);
54+
var json = await _directoryService.FileSystem.File.ReadAllTextAsync(languageFile);
55+
return Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
5156
}
5257

53-
// public string Get(string key)
54-
// {
55-
// if (_languageLocale == null) return key;
56-
// return _languageLocale.
57-
//
58-
// }
58+
public async Task<string> Get(string locale, string key, params object[] args)
59+
{
60+
// Check if the translation for the given locale is cached
61+
if (!_cache.TryGetValue($"{locale}_{key}", out string translatedString))
62+
{
63+
// Load the locale JSON file
64+
var translationData = await LoadLanguage(locale);
65+
66+
// Find the translation for the given key
67+
if (translationData.TryGetValue(key, out string value))
68+
{
69+
translatedString = value;
70+
71+
// Cache the translation for subsequent requests
72+
_cache.Set($"{locale}_{key}", translatedString, TimeSpan.FromMinutes(15)); // Cache for 15 minutes
73+
}
74+
else
75+
{
76+
// If the key is not found, use the key as the translated string
77+
translatedString = key;
78+
}
79+
}
80+
81+
// Format the translated string with arguments
82+
if (args.Length > 0)
83+
{
84+
translatedString = string.Format(translatedString, args);
85+
}
86+
87+
return translatedString;
88+
}
5989
}

0 commit comments

Comments
 (0)