-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathScribanTemplateLocalizer.cs
More file actions
64 lines (51 loc) · 1.87 KB
/
Copy pathScribanTemplateLocalizer.cs
File metadata and controls
64 lines (51 loc) · 1.87 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
62
63
64
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Localization;
using Scriban;
using Scriban.Runtime;
using Scriban.Syntax;
namespace Volo.Abp.TextTemplating.Scriban;
public class ScribanTemplateLocalizer : IScriptCustomFunction
{
private readonly IStringLocalizer _localizer;
public ScribanTemplateLocalizer(IStringLocalizer localizer)
{
_localizer = localizer;
}
public object? Invoke(TemplateContext context, ScriptNode? callerContext, ScriptArray arguments,
ScriptBlockStatement? blockStatement)
{
return GetString(arguments);
}
public ValueTask<object?> InvokeAsync(TemplateContext context, ScriptNode? callerContext, ScriptArray arguments,
ScriptBlockStatement? blockStatement)
{
return new ValueTask<object?>(GetString(arguments));
}
private string GetString(ScriptArray arguments)
{
if (arguments.IsNullOrEmpty())
{
return string.Empty;
}
var name = arguments[0];
if (name == null || name.ToString().IsNullOrWhiteSpace())
{
return string.Empty;
}
var args = arguments.Skip(1).Where(x => x != null && !x.ToString().IsNullOrWhiteSpace()).Cast<object>().ToArray();
return args.Any() ? _localizer[name.ToString()!, args] : _localizer[name.ToString()!];
}
public int RequiredParameterCount => 1;
public int ParameterCount => ScriptFunctionCall.MaximumParameterCount - 1;
public ScriptVarParamKind VarParamKind => ScriptVarParamKind.Direct;
public Type ReturnType => typeof(object);
public ScriptParameterInfo GetParameterInfo(int index)
{
return index == 0
? new ScriptParameterInfo(typeof(string), "template_name")
: new ScriptParameterInfo(typeof(object), "value");
}
}