Kavita/API/Controllers/LocaleController.cs
Joe Milazzo 9f29fa593d
Progress Overhaul + Profile Page and a LOT more! (#4262)
Co-authored-by: Amelia <77553571+Fesaa@users.noreply.github.com>
Co-authored-by: Robbie Davis <robbie@therobbiedavis.com>
2025-12-09 10:00:11 -07:00

51 lines
1.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using API.Constants;
using API.DTOs;
using API.Services;
using EasyCaching.Core;
using Kavita.Common.EnvironmentInfo;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace API.Controllers;
#nullable enable
public class LocaleController : BaseApiController
{
private readonly ILocalizationService _localizationService;
private readonly IEasyCachingProvider _localeCacheProvider;
private static readonly string CacheKey = "locales_" + BuildInfo.Version;
public LocaleController(ILocalizationService localizationService, IEasyCachingProviderFactory cachingProviderFactory)
{
_localizationService = localizationService;
_localeCacheProvider = cachingProviderFactory.GetCachingProvider(EasyCacheProfiles.LocaleOptions);
}
/// <summary>
/// Returns all applicable locales on the server
/// </summary>
/// <remarks>This can be cached as it will not change per version.</remarks>
/// <returns></returns>
[AllowAnonymous]
[HttpGet]
public async Task<ActionResult<IEnumerable<KavitaLocale>>> GetAllLocales()
{
var result = await _localeCacheProvider.GetAsync<IEnumerable<KavitaLocale>>(CacheKey);
if (result.HasValue)
{
return Ok(result.Value);
}
var ret = _localizationService.GetLocales().Where(l => l.TranslationCompletion > 0f);
await _localeCacheProvider.SetAsync(CacheKey, ret, TimeSpan.FromDays(1));
return Ok(ret);
}
}