Skip to content

Commit

Permalink
Use culture options in appsetting.
Browse files Browse the repository at this point in the history
  • Loading branch information
Wayne-KTCSZ committed Feb 2, 2024
1 parent 30037cb commit f79929b
Showing 1 changed file with 18 additions and 21 deletions.
39 changes: 18 additions & 21 deletions src/EasyFrameWork/Modules/MutiLanguage/Localize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,31 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Extensions.DependencyInjection;

namespace Easy.Modules.MutiLanguage
{
public class Localize : ILocalize
{
private ILanguageService _languageService;
List<string> cultureCodes = new List<string>();
public Localize(ILanguageService languageService, IOptions<CultureOption> options, IHttpContextAccessor httpContextAccessor)
private readonly IServiceProvider _serviceProvider;
public Localize(IServiceProvider serviceProvider)
{
_languageService = languageService;

var userLan = httpContextAccessor.HttpContext.Request.GetUserLanguages().FirstOrDefault();

if (userLan.IsNotNullAndWhiteSpace())
_serviceProvider = serviceProvider;
}
private string cultureCode;
public string CultureCode
{
get
{
cultureCodes.Add(userLan);
return cultureCode ?? (cultureCode = _serviceProvider.GetService<IOptions<CultureOption>>().Value.Code);
}
if (userLan != options.Value.Code)
}
private ILanguageService languageService;
private ILanguageService LanguageService
{
get
{
cultureCodes.Add(options.Value.Code);
return languageService ?? (languageService = _serviceProvider.GetService<ILanguageService>());
}
}
public string Get(string content)
Expand All @@ -44,20 +49,12 @@ public string Get(string content, string culture)

public string GetOrNull(string content)
{
foreach (var item in cultureCodes)
{
string lanValue = GetOrNull(content, item);
if (lanValue.IsNotNullAndWhiteSpace())
{
return lanValue;
}
}
return null;
return GetOrNull(content, CultureCode);
}

public string GetOrNull(string content, string culture)
{
var lanContent = _languageService.Get(content, culture);
var lanContent = LanguageService.Get(content, culture);
if (lanContent != null && lanContent.LanValue.IsNotNullAndWhiteSpace())
{
return lanContent.LanValue;
Expand Down

0 comments on commit f79929b

Please sign in to comment.