Updated for v10 - Get lang files via FileProviders (and support Razor Class Libraries) (#12313)

* Update localized to get the physical paths on .net 6

* Update src/Umbraco.Web.BackOffice/DependencyInjection/UmbracoBuilder.LocalizedText.cs

Co-authored-by: Ronald Barendse <ronald@barend.se>

* Minor amends for variable naming and to resolve warnings on use of explicit types.

Co-authored-by: Ronald Barendse <ronald@barend.se>
Co-authored-by: Andy Butland <abutland73@gmail.com>
This commit is contained in:
Kevin Jump
2022-04-29 11:58:44 +01:00
committed by GitHub
parent cf2b9a0f21
commit 6b5bc7cebd
2 changed files with 46 additions and 23 deletions

View File

@@ -45,7 +45,7 @@ namespace Umbraco.Cms.Infrastructure.DependencyInjection
builder.Services.AddUnique<IPackagingService, PackagingService>();
builder.Services.AddUnique<IServerRegistrationService, ServerRegistrationService>();
builder.Services.AddUnique<ITwoFactorLoginService, TwoFactorLoginService>();
builder.Services.AddTransient(SourcesFactory);
builder.Services.AddTransient(LocalizedTextServiceFileSourcesFactory);
builder.Services.AddUnique(factory => CreatePackageRepository(factory, "createdPackages.config"));
builder.Services.AddUnique<ICreatedPackagesRepository, CreatedPackageSchemaRepository>();
builder.Services.AddSingleton<PackageDataInstallation>();
@@ -77,7 +77,7 @@ namespace Umbraco.Cms.Infrastructure.DependencyInjection
factory.GetRequiredService<FileSystems>(),
packageRepoFileName);
private static LocalizedTextServiceFileSources SourcesFactory(IServiceProvider container)
private static LocalizedTextServiceFileSources LocalizedTextServiceFileSourcesFactory(IServiceProvider container)
{
var hostingEnvironment = container.GetRequiredService<IHostingEnvironment>();
var mainLangFolder = new DirectoryInfo(hostingEnvironment.MapPathContentRoot(WebPath.Combine(Constants.SystemDirectories.Umbraco, "config", "lang")));

View File

@@ -18,10 +18,8 @@ namespace Umbraco.Extensions
public static partial class UmbracoBuilderExtensions
{
/// <summary>
/// Add the SupplementaryLocalizedTextFilesSources
/// Adds the supplementary localized texxt file sources from the various physical and virtual locations supported.
/// </summary>
/// <param name="builder"></param>
/// <returns></returns>
private static IUmbracoBuilder AddSupplemenataryLocalizedTextFileSources(this IUmbracoBuilder builder)
{
builder.Services.AddTransient(sp =>
@@ -40,28 +38,24 @@ namespace Umbraco.Extensions
private static IEnumerable<LocalizedTextServiceSupplementaryFileSource> GetSupplementaryFileSources(
IWebHostEnvironment webHostEnvironment)
{
var webFileProvider = webHostEnvironment.WebRootFileProvider;
var contentFileProvider = webHostEnvironment.ContentRootFileProvider;
IFileProvider webFileProvider = webHostEnvironment.WebRootFileProvider;
IFileProvider contentFileProvider = webHostEnvironment.ContentRootFileProvider;
// plugins in /app_plugins
var pluginLangFolders = GetPluginLanguageFileSources(contentFileProvider, Cms.Core.Constants.SystemDirectories.AppPlugins, false);
// files in /wwwroot/app_plugins (or any virtual location that maps to this)
var razorPluginFolders = GetPluginLanguageFileSources(webFileProvider, Cms.Core.Constants.SystemDirectories.AppPlugins, false);
// gets all langs files in /app_plugins real or virtual locations
IEnumerable<LocalizedTextServiceSupplementaryFileSource> pluginLangFileSources = GetPluginLanguageFileSources(webFileProvider, Cms.Core.Constants.SystemDirectories.AppPlugins, false);
// user defined langs that overwrite the default, these should not be used by plugin creators
var userConfigLangFolder = Cms.Core.Constants.SystemDirectories.Config
.TrimStart(Cms.Core.Constants.CharArrays.Tilde);
var userLangFolders = contentFileProvider.GetDirectoryContents(userConfigLangFolder)
IEnumerable<LocalizedTextServiceSupplementaryFileSource> userLangFileSources = contentFileProvider.GetDirectoryContents(userConfigLangFolder)
.Where(x => x.IsDirectory && x.Name.InvariantEquals("lang"))
.Select(x => new DirectoryInfo(x.PhysicalPath))
.SelectMany(x => x.GetFiles("*.user.xml", SearchOption.TopDirectoryOnly))
.Select(x => new LocalizedTextServiceSupplementaryFileSource(x, true));
return pluginLangFolders
.Concat(razorPluginFolders)
.Concat(userLangFolders);
return pluginLangFileSources
.Concat(userLangFileSources);
}
@@ -77,14 +71,43 @@ namespace Umbraco.Extensions
{
// locate all the *.xml files inside Lang folders inside folders of the main folder
// e.g. /app_plugins/plugin-name/lang/*.xml
var fileSources = new List<LocalizedTextServiceSupplementaryFileSource>();
return fileProvider.GetDirectoryContents(folder)
.Where(x => x.IsDirectory)
.SelectMany(x => fileProvider.GetDirectoryContents(WebPath.Combine(folder, x.Name)))
.Where(x => x.IsDirectory && x.Name.InvariantEquals("lang"))
.Select(x => new DirectoryInfo(x.PhysicalPath))
.SelectMany(x => x.GetFiles("*.xml", SearchOption.TopDirectoryOnly))
.Select(x => new LocalizedTextServiceSupplementaryFileSource(x, overwriteCoreKeys));
var pluginFolders = fileProvider.GetDirectoryContents(folder)
.Where(x => x.IsDirectory).ToList();
foreach (IFileInfo pluginFolder in pluginFolders)
{
// get the full virtual path for the plugin folder
var pluginFolderPath = WebPath.Combine(folder, pluginFolder.Name);
// get any lang folders in this plugin
IEnumerable<IFileInfo> langFolders = fileProvider.GetDirectoryContents(pluginFolderPath)
.Where(x => x.IsDirectory && x.Name.InvariantEquals("lang"));
// loop through the lang folder(s)
// - there could be multiple on case sensitive file system
foreach (var langFolder in langFolders)
{
// get the full 'virtual' path of the lang folder
var langFolderPath = WebPath.Combine(pluginFolderPath, langFolder.Name);
// request all the files out of the path, these will have physicalPath set.
var files = fileProvider.GetDirectoryContents(langFolderPath)
.Where(x => x.Name.InvariantEndsWith(".xml") && !string.IsNullOrEmpty(x.PhysicalPath))
.Select(x => new FileInfo(x.PhysicalPath))
.Select(x => new LocalizedTextServiceSupplementaryFileSource(x, overwriteCoreKeys))
.ToList();
// add any to our results
if (files.Count > 0)
{
fileSources.AddRange(files);
}
}
}
return fileSources;
}
}
}