Added ResourceGenerator
This commit is contained in:
parent
9412d80555
commit
811041e903
@ -22,6 +22,7 @@
|
||||
<ProjectReference Include="..\ASC.Common\ASC.Common.csproj" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Service Include="{508349b6-6b84-4df5-91f0-309beebad82d}" />
|
||||
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
@ -34,4 +35,17 @@
|
||||
<PackageReference Include="AWSSDK.SimpleEmail" Version="3.3.100.18" />
|
||||
<PackageReference Include="MailKit" Version="2.1.5.1" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Update="Resources\ResourceGenerator.cs">
|
||||
<DependentUpon>ResourceGenerator.tt</DependentUpon>
|
||||
<DesignTime>True</DesignTime>
|
||||
<AutoGen>True</AutoGen>
|
||||
</Compile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Update="Resources\ResourceGenerator.tt">
|
||||
<LastGenOutput>ResourceGenerator.cs</LastGenOutput>
|
||||
<Generator>TextTemplatingFileGenerator</Generator>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
</Project>
|
64
common/ASC.Core.Common/Resources/JsonResourceManager.cs
Normal file
64
common/ASC.Core.Common/Resources/JsonResourceManager.cs
Normal file
@ -0,0 +1,64 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using ASC.Common.Utils;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace ASC.Core.Common.Resources
|
||||
{
|
||||
public class JsonResourceManager
|
||||
{
|
||||
private static string DirName { get; set; }
|
||||
static JsonResourceManager()
|
||||
{
|
||||
DirName = ConfigurationManager.AppSettings["core:resources"];
|
||||
}
|
||||
|
||||
public string FileName { get; }
|
||||
|
||||
public ConcurrentDictionary<string, JObject> KeyValue { get; set; }
|
||||
|
||||
public JsonResourceManager(string fileName)
|
||||
{
|
||||
FileName = Path.GetFileNameWithoutExtension(fileName) + ".json";
|
||||
KeyValue = new ConcurrentDictionary<string, JObject>();
|
||||
}
|
||||
|
||||
public string GetString(string key)
|
||||
{
|
||||
var cultureInfo = Thread.CurrentThread.CurrentCulture;
|
||||
JToken token;
|
||||
|
||||
var resources = KeyValue.GetOrAdd(cultureInfo.Name, FromFile);
|
||||
token = resources.SelectToken(key);
|
||||
if (token != null)
|
||||
{
|
||||
return token.ToString();
|
||||
}
|
||||
|
||||
resources = KeyValue.GetOrAdd(cultureInfo.Parent.Name, FromFile);
|
||||
token = resources.SelectToken(key);
|
||||
if (token != null)
|
||||
{
|
||||
return token.ToString();
|
||||
}
|
||||
|
||||
resources = KeyValue.GetOrAdd("en", FromFile);
|
||||
token = resources.SelectToken(key);
|
||||
if (token != null)
|
||||
{
|
||||
return token.ToString();
|
||||
}
|
||||
|
||||
return key;
|
||||
}
|
||||
|
||||
JObject FromFile(string culture)
|
||||
{
|
||||
var filePath = Path.GetFullPath(Path.Combine(DirName, culture, FileName));
|
||||
if (!File.Exists(filePath)) return new JObject();
|
||||
|
||||
return JObject.Parse(File.ReadAllText(filePath));
|
||||
}
|
||||
}
|
||||
}
|
161
common/ASC.Core.Common/Resources/ResourceGenerator.cs
Normal file
161
common/ASC.Core.Common/Resources/ResourceGenerator.cs
Normal file
@ -0,0 +1,161 @@
|
||||
|
||||
|
||||
|
||||
namespace ASC.Core.Common.Resources
|
||||
{
|
||||
public class Translation
|
||||
{
|
||||
private static JsonResourceManager JsonResourceManager { get; set; }
|
||||
static Translation()
|
||||
{
|
||||
JsonResourceManager = new JsonResourceManager("translation");
|
||||
}
|
||||
|
||||
public static string Title
|
||||
{
|
||||
get
|
||||
{
|
||||
return JsonResourceManager.GetString("title");
|
||||
}
|
||||
}
|
||||
public static ModuleData Module
|
||||
{
|
||||
get
|
||||
{
|
||||
return new ModuleData();
|
||||
}
|
||||
}
|
||||
public class ModuleData
|
||||
{
|
||||
public HomeData Home
|
||||
{
|
||||
get
|
||||
{
|
||||
return new HomeData();
|
||||
}
|
||||
}
|
||||
public class HomeData
|
||||
{
|
||||
|
||||
}
|
||||
public DocumentsData Documents
|
||||
{
|
||||
get
|
||||
{
|
||||
return new DocumentsData();
|
||||
}
|
||||
}
|
||||
public class DocumentsData
|
||||
{
|
||||
public string Title
|
||||
{
|
||||
get
|
||||
{
|
||||
return JsonResourceManager.GetString("module.documents.title");
|
||||
}
|
||||
}
|
||||
public string Description
|
||||
{
|
||||
get
|
||||
{
|
||||
return JsonResourceManager.GetString("module.documents.description");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
public ProjectsData Projects
|
||||
{
|
||||
get
|
||||
{
|
||||
return new ProjectsData();
|
||||
}
|
||||
}
|
||||
public class ProjectsData
|
||||
{
|
||||
public string Title
|
||||
{
|
||||
get
|
||||
{
|
||||
return JsonResourceManager.GetString("module.projects.title");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
public CrmData Crm
|
||||
{
|
||||
get
|
||||
{
|
||||
return new CrmData();
|
||||
}
|
||||
}
|
||||
public class CrmData
|
||||
{
|
||||
public string Title
|
||||
{
|
||||
get
|
||||
{
|
||||
return JsonResourceManager.GetString("module.crm.title");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
public MailData Mail
|
||||
{
|
||||
get
|
||||
{
|
||||
return new MailData();
|
||||
}
|
||||
}
|
||||
public class MailData
|
||||
{
|
||||
public string Title
|
||||
{
|
||||
get
|
||||
{
|
||||
return JsonResourceManager.GetString("module.mail.title");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
public PeopleData People
|
||||
{
|
||||
get
|
||||
{
|
||||
return new PeopleData();
|
||||
}
|
||||
}
|
||||
public class PeopleData
|
||||
{
|
||||
public string Title
|
||||
{
|
||||
get
|
||||
{
|
||||
return JsonResourceManager.GetString("module.People.title");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
public CommunityData Community
|
||||
{
|
||||
get
|
||||
{
|
||||
return new CommunityData();
|
||||
}
|
||||
}
|
||||
public class CommunityData
|
||||
{
|
||||
public string Title
|
||||
{
|
||||
get
|
||||
{
|
||||
return JsonResourceManager.GetString("module.community.title");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
136
common/ASC.Core.Common/Resources/ResourceGenerator.tt
Normal file
136
common/ASC.Core.Common/Resources/ResourceGenerator.tt
Normal file
@ -0,0 +1,136 @@
|
||||
<#@ template debug="false" hostspecific="true" language="C#" #>
|
||||
<#@ output extension=".cs" #>
|
||||
|
||||
<#@ assembly name="System.Core" #>
|
||||
<#@ assembly name="Newtonsoft.Json" #>
|
||||
|
||||
<#@ import namespace="System.IO" #>
|
||||
<#@ import namespace="System.Linq" #>
|
||||
<#@ import namespace="System.Text" #>
|
||||
<#@ import namespace="Newtonsoft.Json.Linq" #>
|
||||
|
||||
namespace ASC.Core.Common.Resources
|
||||
{
|
||||
<#
|
||||
var dirPath = this.Host.ResolvePath("../../../web/ASC.Web.Studio/ClientApp/public/locales/en");
|
||||
var files = Directory.GetFiles(dirPath, "*.json");
|
||||
|
||||
foreach(var filePath in files)
|
||||
{
|
||||
var parsed = JObject.Parse(File.ReadAllText(filePath));
|
||||
var properties = parsed.Properties();
|
||||
var fileName = Path.GetFileNameWithoutExtension(filePath);
|
||||
#>
|
||||
public class <#= ToUpper(fileName)#>
|
||||
{
|
||||
private static JsonResourceManager JsonResourceManager { get; set; }
|
||||
static <#= ToUpper(fileName)#>()
|
||||
{
|
||||
JsonResourceManager = new JsonResourceManager("<#= fileName#>");
|
||||
}
|
||||
|
||||
<# foreach (var prop in properties) {
|
||||
var upperPropName= ToUpper(prop.Name);
|
||||
if(prop.Value is JValue){#>
|
||||
public static string <#= upperPropName#>
|
||||
{
|
||||
get
|
||||
{
|
||||
return JsonResourceManager.GetString("<#= prop.Name#>");
|
||||
}
|
||||
}
|
||||
<# } else if(prop.Value is JObject obj){ #>
|
||||
public static <#= upperPropName#>Data <#= upperPropName#>
|
||||
{
|
||||
get
|
||||
{
|
||||
return new <#= upperPropName#>Data();
|
||||
}
|
||||
}
|
||||
<#=GetProps(2, prop.Name, prop)#>
|
||||
|
||||
<# }
|
||||
}#>
|
||||
}
|
||||
<#}#>
|
||||
}
|
||||
<#
|
||||
string ToUpper(string name)
|
||||
{
|
||||
return name.Substring(0, 1).ToUpper() + name.Substring(1);
|
||||
}
|
||||
|
||||
string GetProps(int level, string propName, JProperty prop)
|
||||
{
|
||||
var obj = prop.Value as JObject;
|
||||
var builder= new StringBuilder();
|
||||
|
||||
builder.Append(' ', level*4);
|
||||
builder.AppendFormat("public class {0}Data", ToUpper(prop.Name));
|
||||
builder.AppendLine();
|
||||
|
||||
builder.Append(' ', level*4);
|
||||
builder.Append("{");
|
||||
builder.AppendLine();
|
||||
|
||||
foreach(var o in obj.Properties())
|
||||
{
|
||||
var oUpperName = ToUpper(o.Name);
|
||||
if(o.Value is JValue)
|
||||
{
|
||||
AddGetProp($"public string {oUpperName}", $"return JsonResourceManager.GetString(\"{propName}.{o.Name}\");");
|
||||
|
||||
level -=1;
|
||||
}
|
||||
else if(o.Value is JObject subobj)
|
||||
{
|
||||
AddGetProp($"public {oUpperName}Data {oUpperName}", $"return new {oUpperName}Data();");
|
||||
|
||||
builder.Append(GetProps(level, propName + "." + o.Name, o));
|
||||
level -=1;
|
||||
}
|
||||
}
|
||||
|
||||
builder.AppendLine();
|
||||
builder.Append(' ', level*4);
|
||||
builder.Append("}");
|
||||
builder.AppendLine();
|
||||
return builder.ToString();
|
||||
|
||||
void AddGetProp(string name, string val)
|
||||
{
|
||||
level +=1;
|
||||
builder.Append(' ', level*4);
|
||||
builder.Append(name);
|
||||
builder.AppendLine();
|
||||
|
||||
builder.Append(' ', level*4);
|
||||
builder.Append("{");
|
||||
builder.AppendLine();
|
||||
|
||||
level +=1;
|
||||
builder.Append(' ', level*4);
|
||||
builder.Append("get");
|
||||
builder.AppendLine();
|
||||
|
||||
builder.Append(' ', level*4);
|
||||
builder.Append("{");
|
||||
builder.AppendLine();
|
||||
|
||||
level +=1;
|
||||
builder.Append(' ', level*4);
|
||||
builder.Append(val);
|
||||
builder.AppendLine();
|
||||
|
||||
level -=1;
|
||||
builder.Append(' ', level*4);
|
||||
builder.Append("}");
|
||||
builder.AppendLine();
|
||||
|
||||
level -=1;
|
||||
builder.Append(' ', level*4);
|
||||
builder.Append("}");
|
||||
builder.AppendLine();
|
||||
}
|
||||
}
|
||||
#>
|
@ -19,7 +19,8 @@
|
||||
"region": "test",
|
||||
"test": true
|
||||
},
|
||||
"products": "../../products"
|
||||
"products": "../../products",
|
||||
"resources": "../ASC.Web.Studio/ClientApp/public/locales"
|
||||
},
|
||||
"license": {
|
||||
"file": {
|
||||
|
@ -16,6 +16,7 @@
|
||||
"core": {
|
||||
"base-domain": "localhost",
|
||||
"machinekey": "1VVAepxpW8f7",
|
||||
"products": "../products"
|
||||
"products": "../products",
|
||||
"resources": "../studio/ClientApp/public/locales"
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user