From 83679fb8ba7f13dff5837bab2919ea7d2893bf45 Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Wed, 29 May 2019 16:58:26 +0300 Subject: [PATCH] implemented multi language on docs module basicly --- .../app/VoloDocs.Web/Pages/Index.cshtml.cs | 10 ---- .../Docs/Documents/GetDefaultDocumentInput.cs | 3 + .../Volo/Docs/Documents/GetDocumentInput.cs | 3 + .../Documents/GetDocumentResourceInput.cs | 3 + .../Documents/GetNavigationDocumentInput.cs | 3 + .../Volo/Docs/Projects/ProjectDto.cs | 2 + .../Docs/DocsApplicationAutoMapperProfile.cs | 2 +- .../Volo/Docs/Documents/DocumentAppService.cs | 55 ++++++++++++++++--- .../Volo/Docs/Projects/ProjectAppService.cs | 4 +- .../Volo/Docs/Projects/ProjectConsts.cs | 1 + .../Volo/Docs/Documents/IDocumentStore.cs | 5 +- .../Volo/Docs/Documents/LanguageConfig.cs | 11 ++++ .../Docs/Documents/LanguageConfigElement.cs | 11 ++++ .../Documents/FileSystemDocumentStore.cs | 15 ++++- .../GitHub/Documents/GithubDocumentStore.cs | 30 +++++++++- .../docs/src/Volo.Docs.Web/DocsWebModule.cs | 4 +- .../Pages/Documents/Index.cshtml.cs | 10 ---- .../Pages/Documents/Project/Index.cshtml | 6 ++ .../Pages/Documents/Project/Index.cshtml.cs | 46 ++++++++++++++-- .../Pages/Documents/Project/index.js | 13 +++++ .../Volo/Docs/GithubDocumentStore_Tests.cs | 2 +- 21 files changed, 193 insertions(+), 46 deletions(-) create mode 100644 modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/LanguageConfig.cs create mode 100644 modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/LanguageConfigElement.cs diff --git a/modules/docs/app/VoloDocs.Web/Pages/Index.cshtml.cs b/modules/docs/app/VoloDocs.Web/Pages/Index.cshtml.cs index 8b6ffd2cdf..018e5fac4c 100644 --- a/modules/docs/app/VoloDocs.Web/Pages/Index.cshtml.cs +++ b/modules/docs/app/VoloDocs.Web/Pages/Index.cshtml.cs @@ -22,16 +22,6 @@ namespace VoloDocs.Web.Pages { Projects = (await _projectAppService.GetListAsync()).Items; - if (Projects.Count == 1) - { - return RedirectToPage("./Documents/Project/Index", new - { - projectName = Projects[0].ShortName, - version = DocsAppConsts.Latest, - documentName = Projects[0].DefaultDocumentName - }); - } - return Page(); } } diff --git a/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetDefaultDocumentInput.cs b/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetDefaultDocumentInput.cs index 770f43b13e..4cfab98225 100644 --- a/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetDefaultDocumentInput.cs +++ b/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetDefaultDocumentInput.cs @@ -10,5 +10,8 @@ namespace Volo.Docs.Documents [StringLength(ProjectConsts.MaxVersionNameLength)] public string Version { get; set; } + + [StringLength(ProjectConsts.MaxLanguageCodeLength)] + public string LanguageCode { get; set; } } } \ No newline at end of file diff --git a/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetDocumentInput.cs b/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetDocumentInput.cs index 2c887fb829..825c1a7942 100644 --- a/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetDocumentInput.cs +++ b/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetDocumentInput.cs @@ -14,5 +14,8 @@ namespace Volo.Docs.Documents [StringLength(ProjectConsts.MaxVersionNameLength)] public string Version { get; set; } + + [StringLength(ProjectConsts.MaxLanguageCodeLength)] + public string LanguageCode { get; set; } } } \ No newline at end of file diff --git a/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetDocumentResourceInput.cs b/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetDocumentResourceInput.cs index e34dfa5512..eb0d853e06 100644 --- a/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetDocumentResourceInput.cs +++ b/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetDocumentResourceInput.cs @@ -14,5 +14,8 @@ namespace Volo.Docs.Documents [StringLength(ProjectConsts.MaxVersionNameLength)] public string Version { get; set; } + + [StringLength(ProjectConsts.MaxLanguageCodeLength)] + public string LanguageCode { get; set; } } } \ No newline at end of file diff --git a/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetNavigationDocumentInput.cs b/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetNavigationDocumentInput.cs index c9ea504272..473014eb25 100644 --- a/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetNavigationDocumentInput.cs +++ b/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Documents/GetNavigationDocumentInput.cs @@ -10,5 +10,8 @@ namespace Volo.Docs.Documents [StringLength(ProjectConsts.MaxVersionNameLength)] public string Version { get; set; } + + [StringLength(ProjectConsts.MaxLanguageCodeLength)] + public string LanguageCode { get; set; } } } \ No newline at end of file diff --git a/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Projects/ProjectDto.cs b/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Projects/ProjectDto.cs index 91f5c63a22..a9ec815a6c 100644 --- a/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Projects/ProjectDto.cs +++ b/modules/docs/src/Volo.Docs.Application.Contracts/Volo/Docs/Projects/ProjectDto.cs @@ -26,5 +26,7 @@ namespace Volo.Docs.Projects public string DocumentStoreType { get; set; } public Dictionary ExtraProperties { get; set; } + + public Dictionary Languages { get; set; } } } \ No newline at end of file diff --git a/modules/docs/src/Volo.Docs.Application/Volo/Docs/DocsApplicationAutoMapperProfile.cs b/modules/docs/src/Volo.Docs.Application/Volo/Docs/DocsApplicationAutoMapperProfile.cs index d29828ad05..320dbe49c3 100644 --- a/modules/docs/src/Volo.Docs.Application/Volo/Docs/DocsApplicationAutoMapperProfile.cs +++ b/modules/docs/src/Volo.Docs.Application/Volo/Docs/DocsApplicationAutoMapperProfile.cs @@ -9,7 +9,7 @@ namespace Volo.Docs { public DocsApplicationAutoMapperProfile() { - CreateMap(); + CreateMap().Ignore(x=>x.Languages); CreateMap(); CreateMap() .Ignore(x => x.Project).Ignore(x => x.Contributors); diff --git a/modules/docs/src/Volo.Docs.Application/Volo/Docs/Documents/DocumentAppService.cs b/modules/docs/src/Volo.Docs.Application/Volo/Docs/Documents/DocumentAppService.cs index 3a4474e877..4b214547f5 100644 --- a/modules/docs/src/Volo.Docs.Application/Volo/Docs/Documents/DocumentAppService.cs +++ b/modules/docs/src/Volo.Docs.Application/Volo/Docs/Documents/DocumentAppService.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.Linq; using System.Threading.Tasks; using Microsoft.Extensions.Caching.Distributed; using Microsoft.Extensions.Logging; @@ -15,17 +16,20 @@ namespace Volo.Docs.Documents private readonly IProjectRepository _projectRepository; private readonly IDocumentStoreFactory _documentStoreFactory; protected IDistributedCache DocumentCache { get; } + protected IDistributedCache LanguaCache { get; } protected IDistributedCache ResourceCache { get; } public DocumentAppService( IProjectRepository projectRepository, IDocumentStoreFactory documentStoreFactory, IDistributedCache documentCache, + IDistributedCache languaCache, IDistributedCache resourceCache) { _projectRepository = projectRepository; _documentStoreFactory = documentStoreFactory; DocumentCache = documentCache; + LanguaCache = languaCache; ResourceCache = resourceCache; } @@ -33,9 +37,10 @@ namespace Volo.Docs.Documents { var project = await _projectRepository.GetAsync(input.ProjectId); - return await GetDocumentWithDetailsDto( + return await GetDocument( project, input.Name, + input.LanguageCode, input.Version ); } @@ -44,9 +49,10 @@ namespace Volo.Docs.Documents { var project = await _projectRepository.GetAsync(input.ProjectId); - return await GetDocumentWithDetailsDto( + return await GetDocument( project, project.DefaultDocumentName, + input.LanguageCode, input.Version ); } @@ -55,9 +61,10 @@ namespace Volo.Docs.Documents { var project = await _projectRepository.GetAsync(input.ProjectId); - return await GetDocumentWithDetailsDto( + return await GetDocument( project, project.NavigationDocumentName, + input.LanguageCode, input.Version ); } @@ -92,20 +99,23 @@ namespace Volo.Docs.Documents ); } - protected virtual async Task GetDocumentWithDetailsDto( + protected virtual async Task GetDocument( Project project, string documentName, + string languageCode, string version) { - var cacheKey = $"Document@{project.ShortName}#{documentName}#{version}"; + var cacheKey = $"Document@{project.ShortName}#{languageCode}#{documentName}#{version}"; async Task GetDocumentAsync() { Logger.LogInformation($"Not found in the cache. Requesting {documentName} from the store..."); var store = _documentStoreFactory.Create(project.DocumentStoreType); - var document = await store.GetDocumentAsync(project, documentName, version); + var languages = await GetLanguageListAsync(store, project, version); + var language = GetLanguageByCode(languages, languageCode); + var document = await store.GetDocumentAsync(project, documentName, language.Code, version); Logger.LogInformation($"Document retrieved: {documentName}"); - return CreateDocumentWithDetailsDto(project, document); + return CreateDocumentWithDetailsDto(project, document, languages); } if (Debugger.IsAttached) @@ -125,11 +135,40 @@ namespace Volo.Docs.Documents ); } - protected virtual DocumentWithDetailsDto CreateDocumentWithDetailsDto(Project project, Document document) + protected virtual LanguageConfigElement GetLanguageByCode(LanguageConfig languageCodes, string languageCode) + { + var language = languageCodes.Languages.FirstOrDefault(l => l.Code == languageCode); + + return language ?? languageCodes.Languages.Single(l => l.IsDefault); + } + + protected virtual async Task GetLanguageListAsync(IDocumentStore store, Project project, string version) + { + async Task GetLanguagesAsync() + { + return await store.GetLanguageListAsync(project, version); + } + + return await LanguaCache.GetOrAddAsync( + project.ShortName, + GetLanguagesAsync, + () => new DistributedCacheEntryOptions + { + AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(24) + } + ); + } + + protected virtual DocumentWithDetailsDto CreateDocumentWithDetailsDto(Project project, Document document, LanguageConfig languages) { var documentDto = ObjectMapper.Map(document); documentDto.Project = ObjectMapper.Map(project); documentDto.Contributors = ObjectMapper.Map, List>(document.Contributors); + documentDto.Project.Languages = new Dictionary(); + foreach (var language in languages.Languages) + { + documentDto.Project.Languages.Add(language.Code,language.DisplayName); + } return documentDto; } } diff --git a/modules/docs/src/Volo.Docs.Application/Volo/Docs/Projects/ProjectAppService.cs b/modules/docs/src/Volo.Docs.Application/Volo/Docs/Projects/ProjectAppService.cs index 02ffac9ee9..63fc5557ce 100644 --- a/modules/docs/src/Volo.Docs.Application/Volo/Docs/Projects/ProjectAppService.cs +++ b/modules/docs/src/Volo.Docs.Application/Volo/Docs/Projects/ProjectAppService.cs @@ -16,17 +16,15 @@ namespace Volo.Docs.Projects private readonly IProjectRepository _projectRepository; private readonly IDistributedCache> _versionCache; private readonly IDocumentStoreFactory _documentStoreFactory; - private readonly IGuidGenerator _guidGenerator; public ProjectAppService( IProjectRepository projectRepository, IDistributedCache> versionCache, - IDocumentStoreFactory documentStoreFactory, IGuidGenerator guidGenerator) + IDocumentStoreFactory documentStoreFactory) { _projectRepository = projectRepository; _versionCache = versionCache; _documentStoreFactory = documentStoreFactory; - _guidGenerator = guidGenerator; } public async Task> GetListAsync() diff --git a/modules/docs/src/Volo.Docs.Domain.Shared/Volo/Docs/Projects/ProjectConsts.cs b/modules/docs/src/Volo.Docs.Domain.Shared/Volo/Docs/Projects/ProjectConsts.cs index 57f3c17112..ea5506151f 100644 --- a/modules/docs/src/Volo.Docs.Domain.Shared/Volo/Docs/Projects/ProjectConsts.cs +++ b/modules/docs/src/Volo.Docs.Domain.Shared/Volo/Docs/Projects/ProjectConsts.cs @@ -8,5 +8,6 @@ public const int MaxNavigationDocumentNameLength = 128; public const int MaxLatestVersionBranchNameLength = 128; public const int MaxVersionNameLength = 128; + public const int MaxLanguageCodeLength = 10; } } diff --git a/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/IDocumentStore.cs b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/IDocumentStore.cs index 1b001303b5..6f6d90de49 100644 --- a/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/IDocumentStore.cs +++ b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/IDocumentStore.cs @@ -1,16 +1,19 @@ using System.Collections.Generic; using System.Threading.Tasks; using Volo.Abp.Domain.Services; +using Volo.Docs.GitHub.Documents; using Volo.Docs.Projects; namespace Volo.Docs.Documents { public interface IDocumentStore : IDomainService { - Task GetDocumentAsync(Project project, string documentName, string version); + Task GetDocumentAsync(Project project, string documentName, string languageCode, string version); Task> GetVersionsAsync(Project project); Task GetResource(Project project, string resourceName, string version); + + Task GetLanguageListAsync(Project project, string version); } } \ No newline at end of file diff --git a/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/LanguageConfig.cs b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/LanguageConfig.cs new file mode 100644 index 0000000000..b53497a948 --- /dev/null +++ b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/LanguageConfig.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Volo.Docs.Documents +{ + public class LanguageConfig + { + public List Languages { get; set; } + } +} diff --git a/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/LanguageConfigElement.cs b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/LanguageConfigElement.cs new file mode 100644 index 0000000000..25525394ca --- /dev/null +++ b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/Documents/LanguageConfigElement.cs @@ -0,0 +1,11 @@ +namespace Volo.Docs.Documents +{ + public class LanguageConfigElement + { + public string DisplayName { get; set; } + + public string Code { get; set; } + + public bool IsDefault { get; set; } + } +} diff --git a/modules/docs/src/Volo.Docs.Domain/Volo/Docs/FileSystem/Documents/FileSystemDocumentStore.cs b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/FileSystem/Documents/FileSystemDocumentStore.cs index 87a74d1702..e221dba23e 100644 --- a/modules/docs/src/Volo.Docs.Domain/Volo/Docs/FileSystem/Documents/FileSystemDocumentStore.cs +++ b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/FileSystem/Documents/FileSystemDocumentStore.cs @@ -1,7 +1,9 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.IO; using System.Security; using System.Threading.Tasks; +using Newtonsoft.Json; using Volo.Abp.Domain.Services; using Volo.Abp.IO; using Volo.Docs.Documents; @@ -14,10 +16,10 @@ namespace Volo.Docs.FileSystem.Documents { public const string Type = "FileSystem"; - public async Task GetDocumentAsync(Project project, string documentName, string version) + public async Task GetDocumentAsync(Project project, string documentName, string languageCode, string version) { var projectFolder = project.GetFileSystemPath(); - var path = Path.Combine(projectFolder, documentName); + var path = Path.Combine(projectFolder, languageCode, documentName); CheckDirectorySecurity(projectFolder, path); @@ -46,6 +48,13 @@ namespace Volo.Docs.FileSystem.Documents return Task.FromResult(new List()); } + public async Task GetLanguageListAsync(Project project, string version) + { + var configAsJson = project.GetFileSystemPath() + "languageConfig.json"; + + return JsonConvert.DeserializeObject(configAsJson); + } + public async Task GetResource(Project project, string resourceName, string version) { var projectFolder = project.GetFileSystemPath(); diff --git a/modules/docs/src/Volo.Docs.Domain/Volo/Docs/GitHub/Documents/GithubDocumentStore.cs b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/GitHub/Documents/GithubDocumentStore.cs index 2d65a17c68..5ca5f310bf 100644 --- a/modules/docs/src/Volo.Docs.Domain/Volo/Docs/GitHub/Documents/GithubDocumentStore.cs +++ b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/GitHub/Documents/GithubDocumentStore.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.Extensions.Logging; +using Newtonsoft.Json; using Volo.Abp.Domain.Services; using Volo.Docs.Documents; using Volo.Docs.GitHub.Projects; @@ -27,14 +28,14 @@ namespace Volo.Docs.GitHub.Documents _githubRepositoryManager = githubRepositoryManager; } - public virtual async Task GetDocumentAsync(Project project, string documentName, string version) + public virtual async Task GetDocumentAsync(Project project, string documentName, string languageCode, string version) { var token = project.GetGitHubAccessTokenOrNull(); var rootUrl = project.GetGitHubUrl(version); - var rawRootUrl = CalculateRawRootUrl(rootUrl); + var userAgent = project.GetGithubUserAgentOrNull(); + var rawRootUrl = CalculateRawRootUrlWithLanguageCode(rootUrl, languageCode); var rawDocumentUrl = rawRootUrl + documentName; var commitHistoryUrl = project.GetGitHubUrlForCommitHistory() + documentName; - var userAgent = project.GetGithubUserAgentOrNull(); var isNavigationDocument = documentName == project.NavigationDocumentName; var editLink = rootUrl.ReplaceFirst("/tree/", "/blob/") + documentName; var localDirectory = ""; @@ -102,6 +103,19 @@ namespace Volo.Docs.GitHub.Documents return new DocumentResource(content); } + public async Task GetLanguageListAsync(Project project, string version) + { + var token = project.GetGitHubAccessTokenOrNull(); + var rootUrl = project.GetGitHubUrl(version); + var userAgent = project.GetGithubUserAgentOrNull(); + + var url = CalculateRawRootUrl(rootUrl) + "languageConfig.json"; + + var configAsJson = await DownloadWebContentAsStringAsync(url, token, userAgent); + + return JsonConvert.DeserializeObject(configAsJson); + } + private async Task> GetReleasesAsync(Project project) { var url = project.GetGitHubUrl(); @@ -202,6 +216,16 @@ namespace Volo.Docs.GitHub.Documents return contributors; } + private static string CalculateRawRootUrlWithLanguageCode(string rootUrl, string languageCode) + { + return (rootUrl + .Replace("github.com", "raw.githubusercontent.com") + .ReplaceFirst("/tree/", "/") + .EnsureEndsWith('/') + + languageCode + ).EnsureEndsWith('/'); + } + private static string CalculateRawRootUrl(string rootUrl) { return rootUrl diff --git a/modules/docs/src/Volo.Docs.Web/DocsWebModule.cs b/modules/docs/src/Volo.Docs.Web/DocsWebModule.cs index 89301265ea..e92a8d21a6 100644 --- a/modules/docs/src/Volo.Docs.Web/DocsWebModule.cs +++ b/modules/docs/src/Volo.Docs.Web/DocsWebModule.cs @@ -39,8 +39,8 @@ namespace Volo.Docs { //TODO: Make configurable! options.Conventions.AddPageRoute("/Documents/Project/Index", "documents/{projectName}"); - - options.Conventions.AddPageRoute("/Documents/Project/Index", "documents/{projectName}/{version}/{*documentName}"); + options.Conventions.AddPageRoute("/Documents/Project/Index", "documents/{languageCode}/{projectName}"); + options.Conventions.AddPageRoute("/Documents/Project/Index", "documents/{languageCode}/{projectName}/{version}/{*documentName}"); }); Configure(options => diff --git a/modules/docs/src/Volo.Docs.Web/Pages/Documents/Index.cshtml.cs b/modules/docs/src/Volo.Docs.Web/Pages/Documents/Index.cshtml.cs index 552aa0c21d..32675ded2c 100644 --- a/modules/docs/src/Volo.Docs.Web/Pages/Documents/Index.cshtml.cs +++ b/modules/docs/src/Volo.Docs.Web/Pages/Documents/Index.cshtml.cs @@ -21,16 +21,6 @@ namespace Volo.Docs.Pages.Documents { var listResult = await _projectAppService.GetListAsync(); - if (listResult.Items.Count == 1) - { - return RedirectToPage("./Project/Index", new - { - projectName = listResult.Items[0].ShortName, - version = DocsAppConsts.Latest, - documentName = listResult.Items[0].DefaultDocumentName - }); - } - Projects = listResult.Items; return Page(); diff --git a/modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/Index.cshtml b/modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/Index.cshtml index 9b98ebe4b4..3e6a8dda35 100644 --- a/modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/Index.cshtml +++ b/modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/Index.cshtml @@ -158,6 +158,11 @@ + @if (Model.LanguageSelectListItems.Count > 1) + { + + } + @if (!string.IsNullOrEmpty(Model.Document.EditLink)) { @L["Edit"] @@ -222,3 +227,4 @@ + \ No newline at end of file diff --git a/modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/Index.cshtml.cs b/modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/Index.cshtml.cs index 378903d121..744ad1d7b9 100644 --- a/modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/Index.cshtml.cs +++ b/modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/Index.cshtml.cs @@ -5,6 +5,7 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Rendering; using Volo.Abp.AspNetCore.Mvc.UI.RazorPages; +using Volo.Abp.Localization; using Volo.Docs.Documents; using Volo.Docs.HtmlConverting; using Volo.Docs.Models; @@ -23,8 +24,13 @@ namespace Volo.Docs.Pages.Documents.Project [BindProperty(SupportsGet = true)] public string DocumentName { get; set; } + [BindProperty(SupportsGet = true)] + public string LanguageCode { get; set; } + public ProjectDto Project { get; set; } + public List LanguageSelectListItems { get; set; } + public string DocumentNameWithExtension { get; private set; } public DocumentWithDetailsDto Document { get; private set; } @@ -58,6 +64,8 @@ namespace Volo.Docs.Pages.Documents.Project await SetVersionAsync(); await SetDocumentAsync(); await SetNavigationAsync(); + SetLanguageSelectListItems(); + AddLanguageCodePrefixToLinks(); } private async Task SetProjectAsync() @@ -72,7 +80,7 @@ namespace Volo.Docs.Pages.Documents.Project ProjectSelectItems = projects.Items.Select(p => new SelectListItem { Text = p.Name, - Value = p.Id != Project.Id ? "/documents/" + p.ShortName + "/" + DocsAppConsts.Latest : null, + Value = p.Id != Project.Id ? "/documents/" + LanguageCode + "/" + p.ShortName + "/" + DocsAppConsts.Latest : null, Selected = p.Id == Project.Id }).ToList(); } @@ -133,6 +141,7 @@ namespace Volo.Docs.Pages.Documents.Project new GetNavigationDocumentInput { ProjectId = Project.Id, + LanguageCode = LanguageCode, Version = Version } ); @@ -154,7 +163,7 @@ namespace Volo.Docs.Pages.Documents.Project version = DocsAppConsts.Latest; } - var link = "/documents/" + ProjectName + "/" + version; + var link = "/documents/" + LanguageCode + "/" + ProjectName + "/" + version; if (documentName != null) { @@ -193,6 +202,7 @@ namespace Volo.Docs.Pages.Documents.Project new GetDefaultDocumentInput { ProjectId = Project.Id, + LanguageCode = LanguageCode, Version = Version } ); @@ -204,6 +214,7 @@ namespace Volo.Docs.Pages.Documents.Project { ProjectId = Project.Id, Name = DocumentNameWithExtension, + LanguageCode = LanguageCode, Version = Version } ); @@ -211,13 +222,35 @@ namespace Volo.Docs.Pages.Documents.Project } catch (DocumentNotFoundException) { - //TODO: Handle it! - throw; + Document = await _documentAppService.GetDefaultAsync( + new GetDefaultDocumentInput + { + ProjectId = Project.Id, + LanguageCode = LanguageCode, + Version = Version + } + ); } ConvertDocumentContentToHtml(); } + private void SetLanguageSelectListItems() + { + LanguageSelectListItems = new List(); + + foreach (var language in Document.Project.Languages) + { + LanguageSelectListItems.Add( + new SelectListItem( + language.Value, + "/documents/" + language.Key + "/" + Project.ShortName + "/" + Version + "/" + DocumentName, + language.Key == LanguageCode + ) + ); + } + } + private void ConvertDocumentContentToHtml() { var converter = _documentToHtmlConverterFactory.Create(Document.Format ?? Project.Format); @@ -238,5 +271,10 @@ namespace Volo.Docs.Pages.Documents.Project Document.Content = content; } + + private void AddLanguageCodePrefixToLinks() + { + Document.Content = Document.Content.Replace("href=\"/documents", "href=\"/documents/" + LanguageCode); + } } } \ No newline at end of file diff --git a/modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/index.js b/modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/index.js index 32ce1933a7..5eeb579f43 100644 --- a/modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/index.js +++ b/modules/docs/src/Volo.Docs.Web/Pages/Documents/Project/index.js @@ -2,10 +2,21 @@ $(function () { + var addLanguageCodePrefixToLinks = function () { + var anchors = $('#sidebar-scroll a'); + + for (var i = 0; i < anchors.length; i++) { + var anchor = $(anchors[i]); + var newhref = anchor.attr('href').replace("/documents", "/documents/" + $('#LanguageCode').val()); + anchor.attr('href', newhref); + } + }; + var initNavigationFilter = function (navigationContainerId) { var $navigation = $("#" + navigationContainerId); + var getShownDocumentLinks = function () { return $navigation.find(".mCSB_container > li a:visible").not(".tree-toggle"); }; @@ -108,6 +119,8 @@ initSocialShareLinks(); + addLanguageCodePrefixToLinks(); + }); })(jQuery); diff --git a/modules/docs/test/Volo.Docs.Domain.Tests/Volo/Docs/GithubDocumentStore_Tests.cs b/modules/docs/test/Volo.Docs.Domain.Tests/Volo/Docs/GithubDocumentStore_Tests.cs index 1f8bfeca77..5e60d5f96f 100644 --- a/modules/docs/test/Volo.Docs.Domain.Tests/Volo/Docs/GithubDocumentStore_Tests.cs +++ b/modules/docs/test/Volo.Docs.Domain.Tests/Volo/Docs/GithubDocumentStore_Tests.cs @@ -28,7 +28,7 @@ namespace Volo.Docs var project = await _projectRepository.FindAsync(_testData.PorjectId); project.ShouldNotBeNull(); - var document = await store.GetDocumentAsync(project, "index2", "0.123.0"); + var document = await store.GetDocumentAsync(project, "index2", "en", "0.123.0"); document.ShouldNotBeNull(); document.Title.ShouldBe("index2");