update localization (still WIP)
This commit is contained in:
@@ -26,7 +26,7 @@ app.UseZero();
|
||||
app.UseRouting();
|
||||
|
||||
app.UseAuthentication();
|
||||
app.UseAuthorization();
|
||||
app.UseAuthorization();
|
||||
|
||||
app.MapRazorPages();
|
||||
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
using System.IO;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Newtonsoft.Json;
|
||||
using System.Text;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace zero.Localization;
|
||||
|
||||
public class ConfigurationLocalizer : Localizer
|
||||
{
|
||||
private IConfiguration _configuration;
|
||||
|
||||
|
||||
public ConfigurationLocalizer(IConfiguration configuration, IOptionsMonitor<LocalizationOptions> options) : base()
|
||||
{
|
||||
_configuration = configuration;
|
||||
}
|
||||
|
||||
|
||||
protected override Translation LoadTranslation(string key)
|
||||
{
|
||||
string normalizedKey = key.Replace(".", ":");
|
||||
IConfigurationSection section = _configuration.GetSection($"Zero:Localization:{normalizedKey}");
|
||||
|
||||
if (section == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return new()
|
||||
{
|
||||
Key = key,
|
||||
Value = section.Value
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
using System.IO;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Newtonsoft.Json;
|
||||
using System.Text;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace zero.Localization;
|
||||
|
||||
public class FileLocalizer : Localizer
|
||||
{
|
||||
private List<Translation> _fileList;
|
||||
private IWebHostEnvironment _env;
|
||||
|
||||
|
||||
public FileLocalizer(IWebHostEnvironment env, IOptionsMonitor<LocalizationOptions> options) : base()
|
||||
{
|
||||
_env = env;
|
||||
LoadIntoCache(options.CurrentValue.FilePath);
|
||||
options.OnChange((opts, _) => LoadIntoCache(opts.FilePath));
|
||||
}
|
||||
|
||||
|
||||
protected void LoadIntoCache(string filePath)
|
||||
{
|
||||
string path = Path.Combine(_env.ContentRootPath, filePath);
|
||||
|
||||
if (!File.Exists(path))
|
||||
{
|
||||
_fileList = new();
|
||||
}
|
||||
else
|
||||
{
|
||||
Dictionary<string, string> texts = JsonConvert.DeserializeObject<Dictionary<string, string>>(File.ReadAllText(path, Encoding.Latin1));
|
||||
_fileList = texts.Select(kvp => new Translation() { Key = kvp.Key, Value = kvp.Value }).ToList();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected override Translation LoadTranslation(string key)
|
||||
{
|
||||
//return new Translation()
|
||||
//{
|
||||
// Value = "{@" + key + "}"
|
||||
//};
|
||||
|
||||
return _fileList.FirstOrDefault(x => x.Key == key);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
using System.Globalization;
|
||||
using Microsoft.Extensions.Localization;
|
||||
|
||||
namespace zero.Localization;
|
||||
|
||||
public class StringLocalizer : IStringLocalizer
|
||||
{
|
||||
private readonly ILocalizer _localizer;
|
||||
|
||||
public StringLocalizer(ILocalizer localizer)
|
||||
{
|
||||
_localizer = localizer;
|
||||
}
|
||||
|
||||
|
||||
public IEnumerable<LocalizedString> GetAllStrings(bool includeParentCultures)
|
||||
{
|
||||
return new List<LocalizedString>();
|
||||
}
|
||||
|
||||
public LocalizedString this[string name] => new(name, GetString(name), false);
|
||||
|
||||
|
||||
public LocalizedString this[string name, params object[] arguments] =>
|
||||
new(name, string.Format(GetString(name), arguments), false);
|
||||
|
||||
|
||||
// public IEnumerable<LocalizedString> GetAllStrings(bool includeAncestorCultures)
|
||||
// {
|
||||
// return _stringProvider.GetAllStrings(CultureInfo.CurrentCulture.Name);
|
||||
// }
|
||||
|
||||
|
||||
public IStringLocalizer WithCulture(CultureInfo culture)
|
||||
{
|
||||
CultureInfo.DefaultThreadCurrentCulture = culture;
|
||||
return new StringLocalizer(_localizer);
|
||||
}
|
||||
|
||||
private string GetString(string name)
|
||||
{
|
||||
string cultureName = CultureInfo.CurrentCulture.Name;
|
||||
string value = _localizer.Text(name);
|
||||
|
||||
if (string.IsNullOrEmpty(value))
|
||||
{
|
||||
// string[] parts = cultureName.Split('-');
|
||||
// if (parts[0] != cultureName)
|
||||
// {
|
||||
// value = _localizer.Text(name, parts[0]);
|
||||
// }
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,12 @@
|
||||
using FluentValidation;
|
||||
using Microsoft.AspNetCore.Mvc.DataAnnotations;
|
||||
using Microsoft.AspNetCore.Mvc.Localization;
|
||||
using Microsoft.AspNetCore.Mvc.Razor;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using Microsoft.Extensions.Localization;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace zero.Localization;
|
||||
|
||||
@@ -12,8 +18,17 @@ internal class ZeroLocalizationModule : ZeroModule
|
||||
|
||||
services.AddScoped<ICultureResolver, CultureResolver>();
|
||||
services.AddScoped<ICultureService, CultureService>();
|
||||
services.AddScoped<ILocalizer, FileLocalizer>();
|
||||
services.AddScoped<ILocalizer, ConfigurationLocalizer>();
|
||||
services.AddScoped<IStringLocalizer, StringLocalizer>();
|
||||
|
||||
services.Configure<RazorViewEngineOptions>(opts => opts.ViewLocationExpanders.Add(new LanguageViewLocationExpander(LanguageViewLocationExpanderFormat.Suffix)));
|
||||
services.AddSingleton<IHtmlLocalizerFactory, HtmlLocalizerFactory>();
|
||||
services.AddTransient<IHtmlLocalizer, HtmlLocalizer>();
|
||||
services.AddTransient<IViewLocalizer, ViewLocalizer>();
|
||||
|
||||
services.AddLocalization();
|
||||
|
||||
|
||||
services.AddOptions<LocalizationOptions>().Bind(configuration.GetSection("Zero:Localization")).Configure(opts =>
|
||||
{
|
||||
opts.FilePath = "Config/texts.json";
|
||||
|
||||
+3
-1
@@ -32,7 +32,9 @@ public class ZeroBuilder
|
||||
setupAction?.Invoke(_startupOptions);
|
||||
|
||||
services.AddControllers();
|
||||
services.AddRazorPages();
|
||||
Mvc = services.AddRazorPages();
|
||||
|
||||
Mvc.AddDataAnnotationsLocalization();
|
||||
|
||||
services.Configure<AntiforgeryOptions>(opts => opts.Cookie.Name = "zero.antiforgery");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user