diff --git a/FilterLists.sln.DotSettings b/FilterLists.sln.DotSettings index 2fa462f5f..aa2cf0065 100644 --- a/FilterLists.sln.DotSettings +++ b/FilterLists.sln.DotSettings @@ -185,4 +185,5 @@ False True True + True \ No newline at end of file diff --git a/src/FilterLists.Agent/Extensions/ServiceCollectionExtensions.cs b/src/FilterLists.Agent/Extensions/ServiceCollectionExtensions.cs index 420e5de85..027026f51 100644 --- a/src/FilterLists.Agent/Extensions/ServiceCollectionExtensions.cs +++ b/src/FilterLists.Agent/Extensions/ServiceCollectionExtensions.cs @@ -19,6 +19,7 @@ public static class ServiceCollectionExtensions public static void RegisterAgentServices(this IServiceCollection services) { services.AddConfiguration(); + services.AddLocalization(); services.AddLoggingCustom(); services.AddTransient(); services.AddMediatR(AppDomain.CurrentDomain.GetAssemblies()); diff --git a/src/FilterLists.Agent/FilterLists.Agent.csproj b/src/FilterLists.Agent/FilterLists.Agent.csproj index e777b0676..091683d30 100644 --- a/src/FilterLists.Agent/FilterLists.Agent.csproj +++ b/src/FilterLists.Agent/FilterLists.Agent.csproj @@ -40,6 +40,7 @@ + diff --git a/src/FilterLists.Agent/Infrastructure/Clients/AgentGitHubClient.cs b/src/FilterLists.Agent/Infrastructure/Clients/AgentGitHubClient.cs index 89cd098b0..2b6aba723 100644 --- a/src/FilterLists.Agent/Infrastructure/Clients/AgentGitHubClient.cs +++ b/src/FilterLists.Agent/Infrastructure/Clients/AgentGitHubClient.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.Threading.Tasks; using FilterLists.Agent.AppSettings; +using Microsoft.Extensions.Localization; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Octokit; @@ -18,15 +19,17 @@ public interface IAgentGitHubClient public class AgentGitHubClient : IAgentGitHubClient { - private const string ExceptionMessageSuffix = " from the GitHub API."; private readonly GitHubClient _gitHubClient; private readonly GitHubSettings _gitHubSettings; + private readonly IStringLocalizer _localizer; private readonly ILogger _logger; - public AgentGitHubClient(IOptions gitHubOptions, ILogger logger) + public AgentGitHubClient(IOptions gitHubOptions, ILogger logger, + IStringLocalizer stringLocalizer) { _gitHubSettings = gitHubOptions.Value; _logger = logger; + _localizer = stringLocalizer; _gitHubClient = new GitHubClient(new ProductHeaderValue(_gitHubSettings.ProductHeaderValue)) { Credentials = new Credentials(_gitHubSettings.PersonalAccessToken) @@ -42,7 +45,7 @@ public async Task> GetAllIssues(RepositoryIssueRequest repo } catch (ApiException ex) { - _logger.LogError(ex, $"Failed getting all Issues{ExceptionMessageSuffix}"); + _logger.LogError(ex, _localizer["Failed getting all Issues from the GitHub API."]); throw; } } @@ -56,7 +59,7 @@ public async Task CreateIssue(NewIssue newIssue) } catch (ApiException ex) { - _logger.LogError(ex, $"Failed creating Issue{ExceptionMessageSuffix}"); + _logger.LogError(ex, _localizer["Failed creating Issue with the GitHub API."]); throw; } } @@ -70,7 +73,7 @@ public async Task UpdateIssue(int issueNumber, IssueUpdate issueUpdate) } catch (ApiException ex) { - _logger.LogError(ex, $"Failed updating Issue{ExceptionMessageSuffix}"); + _logger.LogError(ex, _localizer["Failed updating Issue with the GitHub API."]); throw; } } diff --git a/src/FilterLists.Agent/Infrastructure/Clients/FilterListsApiClient.cs b/src/FilterLists.Agent/Infrastructure/Clients/FilterListsApiClient.cs index e2495f856..7d0f9a6ca 100644 --- a/src/FilterLists.Agent/Infrastructure/Clients/FilterListsApiClient.cs +++ b/src/FilterLists.Agent/Infrastructure/Clients/FilterListsApiClient.cs @@ -1,5 +1,6 @@ using System; using System.Threading.Tasks; +using Microsoft.Extensions.Localization; using RestSharp; namespace FilterLists.Agent.Infrastructure.Clients @@ -12,11 +13,12 @@ public interface IFilterListsApiClient public class FilterListsApiClient : IFilterListsApiClient { private const string FilterListsApiBaseUrl = "https://filterlists.com/api/v1"; - private const string ExceptionMessage = "Error retrieving response from the FilterLists API."; + private readonly IStringLocalizer _localizer; private readonly IRestClient _restClient; - public FilterListsApiClient() + public FilterListsApiClient(IStringLocalizer stringLocalizer) { + _localizer = stringLocalizer; _restClient = new RestClient(FilterListsApiBaseUrl) {UserAgent = "FilterLists.Agent"}; } @@ -25,7 +27,8 @@ public async Task ExecuteAsync(IRestRequest request) var response = await _restClient.ExecuteTaskAsync(request); if (response.ErrorException == null) return response.Data; - throw new ApplicationException(ExceptionMessage, response.ErrorException); + throw new ApplicationException(_localizer["Error retrieving response from the FilterLists API."], + response.ErrorException); } } } \ No newline at end of file diff --git a/src/FilterLists.Agent/Infrastructure/Repositories/UrlRepository.cs b/src/FilterLists.Agent/Infrastructure/Repositories/UrlRepository.cs index 0c1dc8e1b..25722bb2d 100644 --- a/src/FilterLists.Agent/Infrastructure/Repositories/UrlRepository.cs +++ b/src/FilterLists.Agent/Infrastructure/Repositories/UrlRepository.cs @@ -6,6 +6,7 @@ using FilterLists.Agent.Core.Interfaces; using FilterLists.Agent.Features.Urls.Models.DataFileUrls; using FilterLists.Agent.Infrastructure.Clients; +using Microsoft.Extensions.Localization; using RestSharp; namespace FilterLists.Agent.Infrastructure.Repositories @@ -22,17 +23,18 @@ public class UrlRepository : IUrlRepository }; private readonly IFilterListsApiClient _apiClient; + private readonly IStringLocalizer _localizer; - public UrlRepository(IFilterListsApiClient apiClient) + public UrlRepository(IFilterListsApiClient apiClient, IStringLocalizer stringLocalizer) { _apiClient = apiClient; + _localizer = stringLocalizer; } public async Task> GetAllAsync() { if (!EntityUrlsEndpoints.ContainsKey(typeof(TModel).Name)) - throw new InvalidEnumArgumentException("The type of TModel is not valid."); - + throw new InvalidEnumArgumentException(_localizer["The type of TModel is not valid."]); var request = new RestRequest($"{EntityUrlsEndpoints[typeof(TModel).Name]}/seed"); var response = await _apiClient.ExecuteAsync>(request); return response.SelectMany(r =>