2023-12-04 13:17:13 +00:00
|
|
|
using Microsoft.CodeAnalysis;
|
2023-01-03 18:45:08 +00:00
|
|
|
using System.Linq;
|
2023-10-05 10:41:00 +00:00
|
|
|
using System.Text;
|
2023-01-03 18:45:08 +00:00
|
|
|
|
2024-02-11 02:09:18 +00:00
|
|
|
namespace Ryujinx.UI.LocaleGenerator
|
2023-01-03 18:45:08 +00:00
|
|
|
{
|
|
|
|
[Generator]
|
|
|
|
public class LocaleGenerator : IIncrementalGenerator
|
|
|
|
{
|
|
|
|
public void Initialize(IncrementalGeneratorInitializationContext context)
|
|
|
|
{
|
2024-12-20 19:27:11 +00:00
|
|
|
var localeFile = context.AdditionalTextsProvider.Where(static x => x.Path.EndsWith("locales.json"));
|
2023-01-03 18:45:08 +00:00
|
|
|
|
2024-12-20 19:27:11 +00:00
|
|
|
IncrementalValuesProvider<string> contents = localeFile.Select((text, cancellationToken) => text.GetText(cancellationToken)!.ToString());
|
2023-01-03 18:45:08 +00:00
|
|
|
|
|
|
|
context.RegisterSourceOutput(contents, (spc, content) =>
|
|
|
|
{
|
2024-12-20 19:27:11 +00:00
|
|
|
var lines = content.Split('\n').Where(x => x.Trim().StartsWith("\"ID\":")).Select(x => x.Split(':')[1].Trim().Replace("\"", string.Empty).Replace(",", string.Empty));
|
|
|
|
|
2023-10-05 10:41:00 +00:00
|
|
|
StringBuilder enumSourceBuilder = new();
|
|
|
|
enumSourceBuilder.AppendLine("namespace Ryujinx.Ava.Common.Locale;");
|
|
|
|
enumSourceBuilder.AppendLine("internal enum LocaleKeys");
|
|
|
|
enumSourceBuilder.AppendLine("{");
|
2023-01-03 18:45:08 +00:00
|
|
|
foreach (var line in lines)
|
|
|
|
{
|
2023-10-05 10:41:00 +00:00
|
|
|
enumSourceBuilder.AppendLine($" {line},");
|
2023-01-03 18:45:08 +00:00
|
|
|
}
|
2023-10-05 10:41:00 +00:00
|
|
|
enumSourceBuilder.AppendLine("}");
|
2023-01-03 18:45:08 +00:00
|
|
|
|
2023-10-05 10:41:00 +00:00
|
|
|
spc.AddSource("LocaleKeys", enumSourceBuilder.ToString());
|
2023-01-03 18:45:08 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|