-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextLocalizeAdapter.cs
62 lines (54 loc) · 1.92 KB
/
TextLocalizeAdapter.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
using System;
using TriInspector;
using UnityEngine;
using UnityEngine.Scripting;
namespace CodeWriter.ViewBinding.Applicators.Adapters
{
[AddComponentMenu("View Binding/Adapters/[Binding] Text Localize Adapter")]
public class TextLocalizeAdapter : SingleResultAdapterBase<string, TextLocalizeAdapter.ViewVariableStringLocalized>
{
[Space]
[Required]
[SerializeField]
private string format = "";
[Required]
[ShowInInspector]
[ViewContextCollection]
public ViewContextBase[] ExtraContexts
{
get => result.extraContexts;
set => result.extraContexts = value;
}
protected override string Adapt()
{
return format;
}
[Serializable, Preserve]
public class ViewVariableStringLocalized : ViewVariable<string, ViewVariableStringLocalized>
{
[SerializeField]
internal ViewContextBase[] extraContexts = null;
[Preserve]
public ViewVariableStringLocalized()
{
}
public override void AppendValueTo(ref ValueTextBuilder builder)
{
var formatTextBuilder = new ValueTextBuilder(ValueTextBuilder.DefaultCapacity);
var localizedTextBuilder = new ValueTextBuilder(ValueTextBuilder.DefaultCapacity);
try
{
formatTextBuilder.AppendFormat(Value, extraContexts);
var localizedString = BindingsLocalization.Localize(ref formatTextBuilder);
localizedTextBuilder.AppendFormat(localizedString, extraContexts);
builder.Append(localizedTextBuilder.AsSpan());
}
finally
{
formatTextBuilder.Dispose();
localizedTextBuilder.Dispose();
}
}
}
}
}