Skip to content

Commit

Permalink
[refactor] 重构本地化相关代码
Browse files Browse the repository at this point in the history
[new] 支持静态本地化,在导出数据时将text的key转成对应的language value
  • Loading branch information
pirunxi committed Mar 17, 2024
1 parent 66fc1d0 commit b8bd29f
Show file tree
Hide file tree
Showing 15 changed files with 405 additions and 55 deletions.
14 changes: 10 additions & 4 deletions src/Luban.Core/BuiltinOptionNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,19 @@ public static class BuiltinOptionNames

public const string L10NFamily = "l10n";

public const string TextProviderName = "textProviderName";
public const string L10NProviderName = "provider";

public const string TextProviderFile = "textProviderFile";
public const string L10NTextFilePath = "textFile.path";

public const string TextKeyListFile = "textListFile";
public const string L10NTextFileKeyFieldName = "textFile.keyFieldName";

public const string TextKeyFieldName = "key";
public const string L10NTextFileLanguageFieldName = "textFile.languageFieldName";

public const string L10NConvertTextKeyToValue = "convertTextKeyToValue";

//public const string L10NUnknownTextKeyListOutputFile = "unknownTextKeyListOutputFile";

public const string L10NTextListFile = "textListFile";

public const string TypeMapperType = "type";

Expand Down
209 changes: 209 additions & 0 deletions src/Luban.Core/DataTransformer/DataTransfomerBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
using Luban.Datas;
using Luban.DataVisitors;
using Luban.Types;
using NLog.LayoutRenderers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.AccessControl;
using System.Text;
using System.Threading.Tasks;

namespace Luban.DataTransformer;

public abstract class DataTransfomerBase : IDataTransformer, IDataFuncVisitor2<DType>
{
public DType Transform(DType originalData, TType type)
{
if (originalData == null)
{
return null;
}
return originalData.Apply(this, type);
}

DType IDataFuncVisitor2<DType>.Accept(DBool data, TType type)
{
return data;
}

DType IDataFuncVisitor2<DType>.Accept(DByte data, TType type)
{
return data;
}

DType IDataFuncVisitor2<DType>.Accept(DShort data, TType type)
{
return data;
}

DType IDataFuncVisitor2<DType>.Accept(DInt data, TType type)
{
return data;
}

DType IDataFuncVisitor2<DType>.Accept(DLong data, TType type)
{
return data;
}

DType IDataFuncVisitor2<DType>.Accept(DFloat data, TType type)
{
return data;
}

DType IDataFuncVisitor2<DType>.Accept(DDouble data, TType type)
{
return data;
}

DType IDataFuncVisitor2<DType>.Accept(DEnum data, TType type)
{
return data;
}

DType IDataFuncVisitor2<DType>.Accept(DString data, TType type)
{
return data;
}

DType IDataFuncVisitor2<DType>.Accept(DDateTime data, TType type)
{
return data;
}

DType IDataFuncVisitor2<DType>.Accept(DBean data, TType type)
{
var defFields = data.ImplType.HierarchyFields;
int i = 0;
List<DType> newFields = null;
foreach (var fieldValue in data.Fields)
{
if (fieldValue == null)
{
i++;
continue;
}
var defField = defFields[i];
var fieldType = defField.CType;
DType newFieldValue = fieldValue.Apply(this, fieldType);
if (newFieldValue != fieldValue)
{
if (newFields == null)
{
newFields = new List<DType>(data.Fields);
}
newFields[i] = newFieldValue;
}
++i;
}
return newFields == null ? data : new DBean(data.TType, data.ImplType, newFields);
}

DType IDataFuncVisitor2<DType>.Accept(DArray data, TType type)
{
TType eleType = type.ElementType;
List<DType> newDatas = null;
int index = 0;
foreach (var ele in data.Datas)
{
if (ele == null)
{
++index;
continue;
}
DType newEle = ele.Apply(this, eleType);
if (newEle != ele)
{
if (newDatas == null)
{
newDatas = new List<DType>(data.Datas);
}
newDatas[index] = newEle;
}
++index;
}
return newDatas == null ? data : new DArray(data.Type, newDatas);
}

DType IDataFuncVisitor2<DType>.Accept(DList data, TType type)
{
TType eleType = type.ElementType;
List<DType> newDatas = null;
int index = 0;
foreach (var ele in data.Datas)
{
if (ele == null)
{
++index;
continue;
}
DType newEle = ele.Apply(this, eleType);
if (newEle != ele)
{
if (newDatas == null)
{
newDatas = new List<DType>(data.Datas);
}
newDatas[index] = newEle;
}
++index;
}
return newDatas == null ? data : new DList(data.Type, newDatas);
}

DType IDataFuncVisitor2<DType>.Accept(DSet data, TType type)
{
TType eleType = type.ElementType;
List<DType> newDatas = null;
int index = 0;
foreach (var ele in data.Datas)
{
if (ele == null)
{
++index;
continue;
}
DType newEle = ele.Apply(this, eleType);
if (newEle != ele)
{
if (newDatas == null)
{
newDatas = new List<DType>(data.Datas);
}
newDatas[index] = newEle;
}
++index;
}
return newDatas == null ? data : new DSet(data.Type, newDatas);
}

DType IDataFuncVisitor2<DType>.Accept(DMap data, TType type)
{
TMap mapType = (TMap)type;
bool dirty = false;
foreach (var ele in data.Datas)
{
DType newKey = ele.Key.Apply(this, mapType.KeyType);
DType newValue = ele.Value.Apply(this, mapType.ValueType);
if (newKey != ele.Key || newValue != ele.Value)
{
dirty = true;
break;
}
}
if (!dirty)
{
return data;
}

var newDatas = new Dictionary<DType, DType>();
foreach (var ele in data.Datas)
{
DType newKey = ele.Key.Apply(this, mapType.KeyType);
DType newValue = ele.Value.Apply(this, mapType.ValueType);
newDatas[newKey] = newValue;
}
return new DMap(data.Type, newDatas);
}
}
16 changes: 16 additions & 0 deletions src/Luban.Core/DataTransformer/DataTransformerAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Luban.CustomBehaviour;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Luban.DataTransformer;

[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
public class DataTransformerAttribute : BehaviourBaseAttribute
{
public DataTransformerAttribute(string name) : base(name)
{
}
}
14 changes: 14 additions & 0 deletions src/Luban.Core/DataTransformer/IDataTransformer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Luban.Datas;
using Luban.Types;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Luban.DataTransformer;

public interface IDataTransformer
{
DType Transform(DType originalData, TType type);
}
1 change: 0 additions & 1 deletion src/Luban.Core/DataVisitors/DataActionHelpVisitor2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace Luban.DataVisitors;

Expand Down
2 changes: 1 addition & 1 deletion src/Luban.Core/Defs/Record.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class Record
{
public int AutoIndex { get; set; }

public DBean Data { get; }
public DBean Data { get; set; }

public string Source { get; }

Expand Down
9 changes: 8 additions & 1 deletion src/Luban.Core/GenerationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Luban.DataLoader;
using Luban.Datas;
using Luban.Defs;
using Luban.L10N;
using Luban.RawDefs;
using Luban.Schema;
using Luban.Types;
Expand Down Expand Up @@ -59,13 +60,16 @@ public class GenerationContext

public TimeZoneInfo TimeZone { get; private set; }

public ITextProvider TextProvider { get; private set; }

private readonly Dictionary<string, object> _uniqueObjects = new();

private readonly HashSet<Type> _failedValidatorTypes = new();

public void LoadDatas()
{
s_logger.Info("load datas begin");
TextProvider?.Load();
DataLoaderManager.Ins.LoadDatas(this);
s_logger.Info("load datas end");
}
Expand All @@ -82,6 +86,9 @@ public void Init(GenerationContextBuilder builder)
ExcludeTags = builder.ExcludeTags;
TimeZone = TimeZoneUtil.GetTimeZone(builder.TimeZone);

TextProvider = EnvManager.Current.TryGetOption(BuiltinOptionNames.L10NFamily, BuiltinOptionNames.L10NProviderName, false, out string providerName) ?
L10NManager.Ins.CreateTextProvider(providerName) : null;

ExportTables = Assembly.ExportTables;
ExportTypes = CalculateExportTypes();
ExportBeans = ExportTypes.OfType<DefBean>().ToList();
Expand All @@ -90,7 +97,7 @@ public void Init(GenerationContextBuilder builder)

private bool NeedExportNotDefault(List<string> groups)
{
return groups.Any(g => Target.Groups.Contains(g));
return groups.Any(Target.Groups.Contains);
}

private List<DefTypeBase> CalculateExportTypes()
Expand Down
10 changes: 7 additions & 3 deletions src/Luban.Core/L10N/ITextProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ namespace Luban.L10N;

public interface ITextProvider
{
bool Enable { get; }

void Load();

void ProcessDatas();

bool IsValidKey(string key);

string GetText(string key, string language);
bool TryGetText(string key, out string text);

void AddUnknownKey(string key);

bool ConvertTextKeyToValue { get; }
}
9 changes: 1 addition & 8 deletions src/Luban.Core/L10N/L10NManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,6 @@ public void Init()

public ITextProvider CreateTextProvider(string name)
{
ITextProvider provider = CustomBehaviourManager.Ins.CreateBehaviour<ITextProvider, TextProviderAttribute>(name);
provider.Load();
return provider;
}

public ITextProvider GetOrCreateContextUniqueTextProvider(string name)
{
return (ITextProvider)GenerationContext.Current.GetOrAddUniqueObject($"{BuiltinOptionNames.TextProviderName}.{name}", () => CreateTextProvider(name));
return CustomBehaviourManager.Ins.CreateBehaviour<ITextProvider, TextProviderAttribute>(name);
}
}
10 changes: 10 additions & 0 deletions src/Luban.Core/Pipeline/DefaultPipeline.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Luban.CodeTarget;
using Luban.DataTarget;
using Luban.Defs;
using Luban.L10N;
using Luban.OutputSaver;
using Luban.PostProcess;
using Luban.RawDefs;
Expand Down Expand Up @@ -69,6 +70,7 @@ protected void LoadDatas()
{
_genCtx.LoadDatas();
DoValidate();
ProcessL10N();
}

protected void DoValidate()
Expand All @@ -79,6 +81,14 @@ protected void DoValidate()
s_logger.Info("validation end");
}

protected void ProcessL10N()
{
if (_genCtx.TextProvider != null)
{
_genCtx.TextProvider.ProcessDatas();
}
}

protected void ProcessTargets()
{
var tasks = new List<Task>();
Expand Down
Loading

0 comments on commit b8bd29f

Please sign in to comment.