-
Notifications
You must be signed in to change notification settings - Fork 863
/
Localize.cs
65 lines (60 loc) · 1.82 KB
/
Localize.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
63
64
65
/* http://www.zkea.net/
* Copyright (c) ZKEASOFT. All rights reserved.
* http://www.zkea.net/licenses */
using Easy.Extend;
using Easy.Options;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Extensions.DependencyInjection;
namespace Easy.Modules.MutiLanguage
{
public class Localize : ILocalize
{
private readonly IServiceProvider _serviceProvider;
public Localize(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
private string cultureCode;
public string CultureCode
{
get
{
return cultureCode ?? (cultureCode = _serviceProvider.GetService<IOptions<CultureOption>>().Value.Code);
}
}
private ILanguageService languageService;
private ILanguageService LanguageService
{
get
{
return languageService ?? (languageService = _serviceProvider.GetService<ILanguageService>());
}
}
public string Get(string content)
{
return GetOrNull(content) ?? content;
}
public string Get(string content, string culture)
{
return GetOrNull(content, culture) ?? content;
}
public string GetOrNull(string content)
{
return GetOrNull(content, CultureCode);
}
public string GetOrNull(string content, string culture)
{
var lanContent = LanguageService.Get(content, culture);
if (lanContent != null && lanContent.LanValue.IsNotNullAndWhiteSpace())
{
return lanContent.LanValue;
}
return null;
}
}
}