Beispiel #1
0
        /// <summary>
        /// Prepare paged locale resource list model
        /// </summary>
        /// <param name="searchModel">Locale resource search model</param>
        /// <param name="language">Language</param>
        /// <returns>Locale resource list model</returns>
        public virtual async Task <LocaleResourceListModel> PrepareLocaleResourceListModelAsync(LocaleResourceSearchModel searchModel, Language language)
        {
            if (searchModel == null)
            {
                throw new ArgumentNullException(nameof(searchModel));
            }

            if (language == null)
            {
                throw new ArgumentNullException(nameof(language));
            }

            //get locale resources
            var localeResources = (await _localizationService.GetAllResourceValuesAsync(language.Id, loadPublicLocales: null))
                                  .OrderBy(localeResource => localeResource.Key).AsQueryable();

            //filter locale resources
            if (!string.IsNullOrEmpty(searchModel.SearchResourceName))
            {
                localeResources = localeResources.Where(l => l.Key.ToLowerInvariant().Contains(searchModel.SearchResourceName.ToLowerInvariant()));
            }
            if (!string.IsNullOrEmpty(searchModel.SearchResourceValue))
            {
                localeResources = localeResources.Where(l => l.Value.Value.ToLowerInvariant().Contains(searchModel.SearchResourceValue.ToLowerInvariant()));
            }

            var pagedLocaleResources = await localeResources.ToPagedListAsync(searchModel.Page - 1, searchModel.PageSize);

            //prepare list model
            var model = new LocaleResourceListModel().PrepareToGrid(searchModel, pagedLocaleResources, () =>
            {
                //fill in model values from the entity
                return(pagedLocaleResources.Select(localeResource => new LocaleResourceModel
                {
                    LanguageId = language.Id,
                    Id = localeResource.Value.Key,
                    ResourceName = localeResource.Key,
                    ResourceValue = localeResource.Value.Value
                }));
            });

            return(model);
        }
        public async Task <IActionResult> ResourceDownload(int languageId)
        {
            var language = await _languageService.GetLanguageByIdAsync(languageId);

            if (language == null)
            {
                return(new EmptyResult());
            }

            var resources = await _localizationService.GetAllResourceValuesAsync(languageId);

            if (resources.Any())
            {
                Response.Headers.Add($"Content-Disposition", $"attachment; filename=language-{language.Name}.json");
                var downloadData = resources.ToDictionary(x => x.Key, y => y.Value.Value);
                return(new FileContentResult(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(downloadData)), "text/json"));
            }

            return(new EmptyResult());
        }