diff --git a/zero.Raven.IdentityDemo/Program.cs b/zero.Raven.IdentityDemo/Program.cs index fb470d6b..7660a69a 100644 --- a/zero.Raven.IdentityDemo/Program.cs +++ b/zero.Raven.IdentityDemo/Program.cs @@ -26,7 +26,7 @@ app.UseZero(); app.UseRouting(); app.UseAuthentication(); -app.UseAuthorization(); +app.UseAuthorization(); app.MapRazorPages(); diff --git a/zero/Localization/ConfigurationLocalizer.cs b/zero/Localization/ConfigurationLocalizer.cs new file mode 100644 index 00000000..657535c5 --- /dev/null +++ b/zero/Localization/ConfigurationLocalizer.cs @@ -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 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 + }; + } +} \ No newline at end of file diff --git a/zero/Localization/FileLocalizer.cs b/zero/Localization/FileLocalizer.cs deleted file mode 100644 index c09a93b5..00000000 --- a/zero/Localization/FileLocalizer.cs +++ /dev/null @@ -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 _fileList; - private IWebHostEnvironment _env; - - - public FileLocalizer(IWebHostEnvironment env, IOptionsMonitor 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 texts = JsonConvert.DeserializeObject>(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); - } -} \ No newline at end of file diff --git a/zero/Localization/StringLocalizer.cs b/zero/Localization/StringLocalizer.cs new file mode 100644 index 00000000..c32610f0 --- /dev/null +++ b/zero/Localization/StringLocalizer.cs @@ -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 GetAllStrings(bool includeParentCultures) + { + return new List(); + } + + 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 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; + } +} \ No newline at end of file diff --git a/zero/Localization/ZeroLocalizationModule.cs b/zero/Localization/ZeroLocalizationModule.cs index 7ed73963..10c71232 100644 --- a/zero/Localization/ZeroLocalizationModule.cs +++ b/zero/Localization/ZeroLocalizationModule.cs @@ -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(); services.AddScoped(); - services.AddScoped(); + services.AddScoped(); + services.AddScoped(); + services.Configure(opts => opts.ViewLocationExpanders.Add(new LanguageViewLocationExpander(LanguageViewLocationExpanderFormat.Suffix))); + services.AddSingleton(); + services.AddTransient(); + services.AddTransient(); + + services.AddLocalization(); + + services.AddOptions().Bind(configuration.GetSection("Zero:Localization")).Configure(opts => { opts.FilePath = "Config/texts.json"; diff --git a/zero/ZeroBuilder.cs b/zero/ZeroBuilder.cs index 88c432b2..7edf5d29 100644 --- a/zero/ZeroBuilder.cs +++ b/zero/ZeroBuilder.cs @@ -32,7 +32,9 @@ public class ZeroBuilder setupAction?.Invoke(_startupOptions); services.AddControllers(); - services.AddRazorPages(); + Mvc = services.AddRazorPages(); + + Mvc.AddDataAnnotationsLocalization(); services.Configure(opts => opts.Cookie.Name = "zero.antiforgery");