/
UmbracoBuilder.LocalizedText.cs
120 lines (101 loc) · 5.3 KB
/
UmbracoBuilder.LocalizedText.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Umbraco.Cms.Core.DependencyInjection;
using Umbraco.Cms.Core.Routing;
using Umbraco.Cms.Core.Services;
namespace Umbraco.Extensions
{
/// <summary>
/// Extension methods for <see cref="IUmbracoBuilder"/> for the Umbraco back office
/// </summary>
public static partial class UmbracoBuilderExtensions
{
/// <summary>
/// Adds the supplementary localized texxt file sources from the various physical and virtual locations supported.
/// </summary>
private static IUmbracoBuilder AddSupplemenataryLocalizedTextFileSources(this IUmbracoBuilder builder)
{
builder.Services.AddTransient(sp =>
{
return GetSupplementaryFileSources(
sp.GetRequiredService<IWebHostEnvironment>());
});
return builder;
}
/// <summary>
/// Loads the suplimentary localization files from plugins and user config
/// </summary>
private static IEnumerable<LocalizedTextServiceSupplementaryFileSource> GetSupplementaryFileSources(
IWebHostEnvironment webHostEnvironment)
{
IFileProvider webFileProvider = webHostEnvironment.WebRootFileProvider;
IFileProvider contentFileProvider = webHostEnvironment.ContentRootFileProvider;
IEnumerable<LocalizedTextServiceSupplementaryFileSource> localPluginFileSources = GetPluginLanguageFileSources(contentFileProvider, 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);
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
localPluginFileSources
.Concat(pluginLangFileSources)
.Concat(userLangFileSources);
}
/// <summary>
/// Loads the suplimentary localaization files via the file provider.
/// </summary>
/// <remarks>
/// locates all *.xml files in the lang folder of any sub folder of the one provided.
/// e.g /app_plugins/plugin-name/lang/*.xml
/// </remarks>
private static IEnumerable<LocalizedTextServiceSupplementaryFileSource> GetPluginLanguageFileSources(
IFileProvider fileProvider, string folder, bool overwriteCoreKeys)
{
IEnumerable<IFileInfo> pluginFolders = fileProvider
.GetDirectoryContents(folder)
.Where(x => x.IsDirectory);
foreach (IFileInfo pluginFolder in pluginFolders)
{
// get the full virtual path for the plugin folder
var pluginFolderPath = WebPath.Combine(folder, pluginFolder.Name);
// loop through the lang folder(s)
// - there could be multiple on case sensitive file system
foreach (var langFolder in GetLangFolderPaths(fileProvider, pluginFolderPath))
{
// request all the files out of the path, these will have physicalPath set.
IEnumerable<FileInfo> localizationFiles = fileProvider
.GetDirectoryContents(langFolder)
.Where(x => !string.IsNullOrEmpty(x.PhysicalPath))
.Where(x => x.Name.InvariantEndsWith(".xml"))
.Select(x => new FileInfo(x.PhysicalPath));
foreach (FileInfo file in localizationFiles)
{
yield return new LocalizedTextServiceSupplementaryFileSource(file, overwriteCoreKeys);
}
}
}
}
private static IEnumerable<string> GetLangFolderPaths(IFileProvider fileProvider, string path)
{
IEnumerable<IFileInfo> directories = fileProvider.GetDirectoryContents(path).Where(x => x.IsDirectory);
foreach (IFileInfo directory in directories)
{
var virtualPath = WebPath.Combine(path, directory.Name);
if (directory.Name.InvariantEquals("lang"))
{
yield return virtualPath;
}
foreach (var nested in GetLangFolderPaths(fileProvider, virtualPath))
{
yield return nested;
}
}
}
}
}