-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathResourceWriter.cs
More file actions
61 lines (50 loc) · 1.97 KB
/
ResourceWriter.cs
File metadata and controls
61 lines (50 loc) · 1.97 KB
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using Solidoc.Utility;
using YamlDotNet.Serialization;
namespace Solidoc
{
public static class ResourceWriter
{
public static string GetContents()
{
string langauge = ConfigurationManager.Get("Language");
if (string.IsNullOrWhiteSpace(langauge))
{
langauge = "en";
}
string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
path = Path.Combine(path, "i18n", $"{langauge}.yaml");
string contents = File.ReadAllText(path, new UTF8Encoding(false));
return contents;
}
private static string GetTemplate()
{
return ConfigurationManager.Get("I18NTemplate");
}
public static void Run()
{
string contents = GetContents();
var action = new DeserializerBuilder().Build();
var result = action.Deserialize<IDictionary<string, string>>(contents);
var resources = new List<string>();
string template = GetTemplate();
foreach (var item in result)
{
resources.Add($"\t\t/// <summary>{Environment.NewLine}\t\t/// Returns localized string for \"{item.Value}\"{Environment.NewLine}\t\t/// </summary>{Environment.NewLine}\t\tpublic static string {item.Key} => Resource[\"{item.Key}\"];");
}
if (!resources.Any())
{
return;
}
template = template.Replace("{{Resources}}", string.Join(Environment.NewLine.Copy(2), resources));
var directory = new DirectoryInfo(Assembly.GetExecutingAssembly().Location);
string path = Path.Combine(directory.Parent?.Parent?.Parent?.Parent?.FullName, "I18N.cs");
File.WriteAllText(path, template, new UTF8Encoding(false));
}
}
}