/// <summary> /// Adiciona suporte a globalização para a API. /// </summary> /// <param name="app"></param> /// <param name="configuration"></param> /// <param name="appSettingsGlobalizationSection">Seção do appsettings, onde estão as configurações de globalização.</param> /// <returns></returns> public static IApplicationBuilder UseGlobalization(this IApplicationBuilder app, IConfiguration configuration, string appSettingsGlobalizationSection = "Globalization") { var globalizationOptions = new GlobalizationOptions(); configuration.GetSection(appSettingsGlobalizationSection).Bind(globalizationOptions); var requestOptions = new RequestLocalizationOptions { DefaultRequestCulture = GetDefaultRequestCulture(globalizationOptions.DefaultRequestCulture), SupportedCultures = GetSupportedCultures(globalizationOptions.SupportedCultures), SupportedUICultures = GetSupportedUICultures(globalizationOptions.SupportedUICultures), }; if (!globalizationOptions.UseQueryStringRequestCultureProvider) { RequestCultureProvider requestProvider = requestOptions.RequestCultureProviders.OfType <QueryStringRequestCultureProvider>().FirstOrDefault(); if (requestProvider != null) { requestOptions.RequestCultureProviders.Remove(requestProvider); } } app.UseRequestLocalization(requestOptions); return(app); }
/// <summary> /// Gets all registered reports for a single user /// </summary> /// <param name="userInfo">The user information</param> /// <param name="options">The globalization options (optional)</param> /// <returns>A collection of registered reports</returns> public IEnumerable <RegisteredReport> GetReportsForUser ( ReportUserInfo userInfo, GlobalizationOptions options = null ) { Validate.IsNotNull(userInfo); if (userInfo.Roles == null) { throw new ArgumentException ( "No user roles have been defined." ); } var assignments = _roleAssignmentRepository.GetAssignmentsForRoles ( userInfo.Roles ); var reportNames = assignments.Select ( a => a.ReportName ); reportNames = reportNames.Distinct(); var allReports = _reportRepository.GetAllReports(); // Get reports that match the role assignments var matchingReports = allReports.Where ( report => reportNames.Any ( name => report.Name.Equals(name, StringComparison.OrdinalIgnoreCase) ) ); // Get reports without roles and merge them with the matching reports var reportsWithoutRoles = GetReportsWithoutRoles(); var userReports = new List <RegisteredReport> ( matchingReports ); userReports.AddRange(reportsWithoutRoles); userReports.Localize ( _translator, options ); return(userReports.OrderBy ( r => r.Name )); }
/// <summary> /// Gets all registered reports /// </summary> /// <param name="categoryName">The category name</param> /// <param name="options">The globalization options (optional)</param> /// <returns>A collection of registered reports</returns> public IEnumerable <RegisteredReport> GetReportsByCategory ( string categoryName, GlobalizationOptions options = null ) { var category = _categoryRepository.GetCategory ( categoryName ); var reportNames = category.AssignedReports.Select ( a => a.ReportName ) .ToList(); var allReports = _reportRepository.GetAllReports(); var matchingReports = allReports.Where ( report => reportNames.Any ( name => name.Equals(report.Name, StringComparison.OrdinalIgnoreCase) ) ); matchingReports.Localize ( _translator, options ); return(matchingReports); }
/// <summary> /// Construtor padrão /// </summary> /// <param name="nextMiddlewareInPipelineDelegate"></param> /// <param name="tokenManager"></param> /// <param name="logger"></param> /// <param name="errorLogger"></param> /// <param name="translateOptions"></param> /// <param name="globalizationOptions"></param> /// <param name="memoryCache"></param> /// <param name="httpClientFactory"></param> public TranslationMiddleware(RequestDelegate nextMiddlewareInPipelineDelegate, IClientTokenManager tokenManager, ILogger <TranslationMiddleware> logger, IErrorLogger errorLogger, IOptions <TranslationOptions> translateOptions, IOptions <GlobalizationOptions> globalizationOptions, IMemoryCache memoryCache, IHttpClientFactory httpClientFactory) { this.nextMiddlewareInPipelineDelegate = nextMiddlewareInPipelineDelegate; this.tokenManager = tokenManager; this.logger = logger; this.errorLogger = errorLogger; this.translateOptions = translateOptions?.Value ?? throw new ArgumentException("Translate options required."); this.globalizationOptions = globalizationOptions?.Value ?? throw new ArgumentException("Globalization options required."); this.memoryCache = memoryCache ?? throw new ArgumentException("IMemoryCache options required."); this.httpClientFactory = httpClientFactory ?? throw new ArgumentException("IHttpClientFactory required."); recyclableMemoryStreamManager = new RecyclableMemoryStreamManager(); }
public static IApplicationBuilder UseGlobalization(this IApplicationBuilder applicationBuilder, IConfiguration configuration) { var globalizationOptions = new GlobalizationOptions(); configuration.GetSection(AppSettingsGlobalization).Bind(globalizationOptions); applicationBuilder.UseRequestLocalization(new RequestLocalizationOptions { DefaultRequestCulture = new RequestCulture(globalizationOptions?.DefaultRequestCulture, globalizationOptions?.DefaultRequestCulture), SupportedCultures = globalizationOptions?.SupportedCultures.Select(it => new CultureInfo(it)).ToList(), SupportedUICultures = globalizationOptions?.SupportedUICultures.Select(it => new CultureInfo(it)).ToList(), }); return(applicationBuilder); }
/// <summary> /// Gets all registered reports /// </summary> /// <param name="options">The globalization options (optional)</param> /// <returns>A collection of registered reports</returns> public IEnumerable <RegisteredReport> GetAllReports ( GlobalizationOptions options = null ) { var reports = _reportRepository.GetAllReports(); reports.Localize ( _translator, options ); return(reports); }
/// <summary> /// Gets the root report categories in the repository /// </summary> /// <param name="options">The globalization options (optional)</param> /// <returns>A collection of categories</returns> public IEnumerable <ReportCategory> GetRootCategories ( GlobalizationOptions options = null ) { var categories = _categoryRepository.GetRootCategories(); categories.Localize ( _translator, options ); return(categories); }
/// <summary> /// Gets all registered reports for a user and category /// </summary> /// <param name="userInfo">The user information</param> /// <param name="categoryName">The category string</param> /// <param name="options">The globalization options (optional)</param> /// <returns>A collection of registered reports</returns> public IEnumerable <RegisteredReport> GetReportsForUser ( ReportUserInfo userInfo, string categoryName, GlobalizationOptions options = null ) { Validate.IsNotNull(userInfo); Validate.IsNotEmpty(categoryName); var category = _categoryRepository.GetCategory ( categoryName ); var reportNames = category.AssignedReports.Select ( a => a.ReportName ) .ToList(); var userReports = GetReportsForUser ( userInfo ); userReports = userReports.Where ( report => reportNames.Any ( name => name.Equals(report.Name, StringComparison.OrdinalIgnoreCase) ) ); userReports.Localize ( _translator, options ); return(userReports.OrderBy ( r => r.Name )); }
/// <summary> /// Gets a single registered report /// </summary> /// <param name="name">The name of the report</param> /// <param name="options">The globalization options (optional)</param> /// <returns>The matching registered report</returns> public RegisteredReport GetReport ( string name, GlobalizationOptions options = null ) { var report = _reportRepository.GetReport ( name ); report.Localize ( _translator, options ); return(report); }
/// <summary> /// Gets sub report categories from a parent category /// </summary> /// <param name="parentCategoryName">The parent category name</param> /// <param name="options">The globalization options (optional)</param> /// <returns>A collection of report categories</returns> public IEnumerable <ReportCategory> GetSubCategories ( string parentCategoryName, GlobalizationOptions options = null ) { var categories = _categoryRepository.GetSubCategories ( parentCategoryName ); categories.Localize ( _translator, options ); return(categories); }
/// <summary> /// Gets a single report category /// </summary> /// <param name="name">The category name</param> /// <param name="options">The globalization options (optional)</param> /// <returns>The matching category</returns> public ReportCategory GetCategory ( string name, GlobalizationOptions options = null ) { var category = _categoryRepository.GetCategory ( name ); category.Localize ( _translator, options ); return(category); }