Skip to content

feat(18n): Support I18 and Refactor Exception #330

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 45 commits into from
Nov 14, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
ead4e94
feat: Add Globalization
zhenlei520 Oct 8, 2022
9398da4
feat: Add Localization
zhenlei520 Oct 11, 2022
762644f
Merge branch 'main' of https://github.com/masastack/MASA.Framework in…
zhenlei520 Oct 11, 2022
3834f69
fix: Fix Localization
zhenlei520 Oct 12, 2022
f11fc51
Merge branch 'main' of https://github.com/masastack/MASA.Framework in…
zhenlei520 Oct 14, 2022
4e2c792
feat(Localization): Add Localization
zhenlei520 Oct 17, 2022
f05376a
feat: Add Localization
zhenlei520 Oct 18, 2022
a9fcf5a
feat(Localization): Add Languages
zhenlei520 Oct 18, 2022
73faaff
chore: global using
zhenlei520 Oct 19, 2022
ab36063
feat: Add I18N
zhenlei520 Oct 19, 2022
30e70d2
feat: Support I18N
zhenlei520 Oct 19, 2022
1dd1f42
feat: AddII18N
zhenlei520 Oct 31, 2022
e64cd7c
rename: Const -> Constant
zhenlei520 Oct 31, 2022
b18b2a5
Merge branch 'main' of https://github.com/masastack/MASA.Framework in…
zhenlei520 Oct 31, 2022
6a28069
chore: format code
zhenlei520 Oct 31, 2022
f7b8925
chore: ErrorCode access level changed to private
zhenlei520 Oct 31, 2022
8f99031
feat(Exception): Add Exception
zhenlei520 Nov 1, 2022
da8c5a6
refactor(Exception): Refactor Exception
zhenlei520 Nov 2, 2022
93651f2
Merge branch 'main' of https://github.com/masastack/MASA.Framework in…
zhenlei520 Nov 2, 2022
d489289
refactor(Exception): Refactor Exception
zhenlei520 Nov 2, 2022
998d2f0
Merge branch 'main' of https://github.com/masastack/MASA.Framework in…
zhenlei520 Nov 2, 2022
53c9b15
feat: Support Exception
zhenlei520 Nov 3, 2022
b949a6d
feat(Exception): Modify Exception
zhenlei520 Nov 3, 2022
c88a0bf
feat(Globalization): Add I18N
zhenlei520 Nov 4, 2022
6917d19
feat(I18n): Support I18n
zhenlei520 Nov 7, 2022
3df9872
feat(I18N): Adjust I18n.Dcc
zhenlei520 Nov 7, 2022
3b69b03
feat: Add I18N on Blazor Server
zhenlei520 Nov 7, 2022
282245f
feat(I18N): support WebAssembly
zhenlei520 Nov 8, 2022
3a0b87b
feat(I18n): Adjust I18n Support Embedded resources
zhenlei520 Nov 8, 2022
3773064
feat: Caller Support I18N
zhenlei520 Nov 9, 2022
dc4d3e6
feat: Support FluentValidation
zhenlei520 Nov 9, 2022
8f3fbaa
Merge branch 'main' of https://github.com/masastack/MASA.Framework in…
zhenlei520 Nov 9, 2022
52aaa75
feat(I18n): Support I18N
zhenlei520 Nov 9, 2022
60aec79
fix: Fix unit test issues
zhenlei520 Nov 10, 2022
10d39ab
rename: I18N -> I18n
zhenlei520 Nov 10, 2022
cce7b86
chore: Modify code bad smell
zhenlei520 Nov 10, 2022
7bc010e
chore: Modify code bad smell
zhenlei520 Nov 10, 2022
aa49bef
chore: deal with bad taste
zhenlei520 Nov 10, 2022
7cec513
rename: I18n -> I18N
zhenlei520 Nov 10, 2022
59d80b2
chore: fix bad code smell
zhenlei520 Nov 10, 2022
9827b81
chore: Adjust the default Culture
zhenlei520 Nov 11, 2022
2eebad0
chore: Optimize the experience
zhenlei520 Nov 11, 2022
85f6841
chore: fix bad code smell
zhenlei520 Nov 11, 2022
93951b9
Merge branch 'main' of https://github.com/masastack/MASA.Framework in…
zhenlei520 Nov 14, 2022
c8b21a3
rename: I18N -> I18n
zhenlei520 Nov 14, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat(Localization): Add Localization
  • Loading branch information
zhenlei520 committed Oct 17, 2022
commit 4e2c7922bc898a763b4afe61d3f533a7aa70ace3
11 changes: 11 additions & 0 deletions Masa.Framework.sln
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{D2E598D5
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masa.Contrib.Globalization.Localization.Tests", "src\Contrib\Globalization\Tests\Masa.Contrib.Globalization.Localization.Tests\Masa.Contrib.Globalization.Localization.Tests.csproj", "{6C732EBE-22CC-4160-B427-F4558C899024}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masa.Contrib.Globalization.Localization.Dcc", "src\Contrib\Globalization\Masa.Contrib.Globalization.Localization.Dcc\Masa.Contrib.Globalization.Localization.Dcc.csproj", "{B0EFDF9E-81FF-4880-992B-8A65A2008717}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -2085,6 +2087,14 @@ Global
{6C732EBE-22CC-4160-B427-F4558C899024}.Release|Any CPU.Build.0 = Release|Any CPU
{6C732EBE-22CC-4160-B427-F4558C899024}.Release|x64.ActiveCfg = Release|Any CPU
{6C732EBE-22CC-4160-B427-F4558C899024}.Release|x64.Build.0 = Release|Any CPU
{B0EFDF9E-81FF-4880-992B-8A65A2008717}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B0EFDF9E-81FF-4880-992B-8A65A2008717}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B0EFDF9E-81FF-4880-992B-8A65A2008717}.Debug|x64.ActiveCfg = Debug|Any CPU
{B0EFDF9E-81FF-4880-992B-8A65A2008717}.Debug|x64.Build.0 = Debug|Any CPU
{B0EFDF9E-81FF-4880-992B-8A65A2008717}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B0EFDF9E-81FF-4880-992B-8A65A2008717}.Release|Any CPU.Build.0 = Release|Any CPU
{B0EFDF9E-81FF-4880-992B-8A65A2008717}.Release|x64.ActiveCfg = Release|Any CPU
{B0EFDF9E-81FF-4880-992B-8A65A2008717}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -2375,6 +2385,7 @@ Global
{F9E3EBD9-EE96-40F7-BDC7-818B88557D6B} = {5B129E6F-8CB7-4E2E-9F3E-C59CF22C9CA9}
{D2E598D5-5E44-4275-AF03-4BAD07A0E13B} = {5B129E6F-8CB7-4E2E-9F3E-C59CF22C9CA9}
{6C732EBE-22CC-4160-B427-F4558C899024} = {D2E598D5-5E44-4275-AF03-4BAD07A0E13B}
{B0EFDF9E-81FF-4880-992B-8A65A2008717} = {5B129E6F-8CB7-4E2E-9F3E-C59CF22C9CA9}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {40383055-CC50-4600-AD9A-53C14F620D03}
Expand Down
2 changes: 2 additions & 0 deletions Masa.Framework.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/UserDictionary/Words/=Localizer/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=MasaConfigurationProvider/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Masa/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Microsoft.Extensions.DependencyInjection;

public static class ConfigurationExtensions
{
public static Dictionary<string, string> ConvertToDictionary(this IConfiguration configuration)
{
var data = new Dictionary<string, string>();
GetData(configuration, configuration.GetChildren(), ref data);
return data;
}

private static void GetData(
IConfiguration configuration,
IEnumerable<IConfigurationSection> configurationSections,
ref Dictionary<string, string> dictionary)
{
foreach (var configurationSection in configurationSections)
{
var section = configuration.GetSection(configurationSection.Path);

var childrenSections = section.GetChildren()?.ToList() ?? new List<IConfigurationSection>();

if (!section.Exists() || !childrenSections.Any())
{
var key = section.Path;
if (!dictionary.ContainsKey(key))
{
dictionary.Add(key, configuration[section.Path]);
}
}
else
{
GetData(configuration, childrenSections, ref dictionary);
}
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,9 @@

namespace Microsoft.Extensions.Localization;

/// <summary>
/// 具体资源类下具体语言的资源
/// </summary>
public interface ILocalizationResourceContributor
{
string CultureName { get; }

LocalizedString? GetOrNull(string name);

void ResetResource();
string? GetOrNull(string name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,13 @@ public interface IMasaStringLocalizer
/// <summary>Gets the string resource with the given name.</summary>
/// <param name="name">The name of the string resource.</param>
/// <returns>The string resource as a <see cref="T:Microsoft.Extensions.Localization.LocalizedString" />.</returns>
LocalizedString this[string name] { get; }
string this[string name] { get; }

/// <summary>
/// Gets the string resource with the given name and formatted with the supplied arguments.
/// </summary>
/// <param name="name">The name of the string resource.</param>
/// <param name="arguments">The values to format the string with.</param>
/// <returns>The formatted string resource as a <see cref="T:Microsoft.Extensions.Localization.LocalizedString" />.</returns>
LocalizedString this[string name, params object[] arguments] { get; }

/// <summary>Gets all string resources.</summary>
/// <param name="includeParentCultures">
/// A <see cref="T:System.Boolean" /> indicating whether to include strings from parent cultures.
/// </param>
/// <returns>The strings.</returns>
IEnumerable<LocalizedString> GetAllStrings(bool includeParentCultures);
string this[string name, params object[] arguments] { get; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Microsoft.Extensions.Localization;

public interface IMasaStringLocalizerFactory
{
IMasaStringLocalizer Create<TResourceSource>();

IMasaStringLocalizer Create(Type resourceSource);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Microsoft.Extensions.Localization;

public interface IMasaStringLocalizer<out TResourceSource> : IMasaStringLocalizer
{

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,34 @@ namespace Microsoft.Extensions.Localization;

public class LocalizationResource
{
private List<ILocalizationResourceContributor> _dictionary { get; }
private Dictionary<string, ILocalizationResourceContributor> _dictionary { get; }

public Type ResourceType { get; }

public string? DefaultCultureName { get; }
public string? DefaultCultureName { get; internal set; }

public LocalizationResource(Type resourceType, string? defaultCultureName)
{
_dictionary = new();
_dictionary = new(StringComparer.OrdinalIgnoreCase);
ResourceType = resourceType;
DefaultCultureName = defaultCultureName;
}

public void AddContributor(ILocalizationResourceContributor localizationResourceContributor)
{
if (_dictionary.Any(d => d.CultureName.Equals(localizationResourceContributor.CultureName, StringComparison.OrdinalIgnoreCase)))
if (_dictionary.Any(d => d.Key.Equals(localizationResourceContributor.CultureName, StringComparison.OrdinalIgnoreCase)))
throw new Exception($"The {localizationResourceContributor.CultureName} already exists with {ResourceType.FullName}");

_dictionary.Add(localizationResourceContributor);
_dictionary.Add(localizationResourceContributor.CultureName, localizationResourceContributor);
}

public ILocalizationResourceContributor? GetResourceContributor(CultureInfo? cultureInfo = null)
{
var cultureName = cultureInfo?.Name ?? CultureInfo.CurrentUICulture.Name;

if (_dictionary.ContainsKey(cultureName))
return _dictionary[cultureName];

return null;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ public LocalizationResource Add(Type resourceType, string? defaultCultureName =
return this[resourceType] = new LocalizationResource(resourceType, defaultCultureName);
}

public LocalizationResource Get<TResource>()
public LocalizationResource? GetOrNull(Type resourceType)
{
var resourceType = typeof(TResource);

if (!ContainsKey(resourceType))
throw new Exception("Can not find a resource with given type: " + resourceType.FullName);
return null;

return this[resourceType];
}

public LocalizationResource? GetOrNull<TResource>()
=> GetOrNull(typeof(TResource));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Microsoft.Extensions.Localization;

public class MasaStringLocalizerFactory : IMasaStringLocalizerFactory
{
private readonly IServiceProvider _serviceProvider;

public MasaStringLocalizerFactory(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}

public IMasaStringLocalizer Create<TResourceSource>()
=> _serviceProvider.GetRequiredService<IMasaStringLocalizer<TResourceSource>>();

public IMasaStringLocalizer Create(Type resourceSource)
{
var serviceType = typeof(IMasaStringLocalizer<>).MakeGenericType(resourceSource);
return (IMasaStringLocalizer)_serviceProvider.GetRequiredService(serviceType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,12 @@ public MasaLocalizationOptions()
{
Resources = new ();
}

public void FormatResources()
{
foreach (var resource in Resources)
{
resource.Value.DefaultCultureName ??= DefaultCultureName;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright (c) MASA Stack All rights reserved.
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

global using Microsoft.Extensions.Logging;
global using Microsoft.Extensions.DependencyInjection;
global using System.Globalization;
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,11 @@ public static IServiceCollection AddMasaConfiguration(
configurationBuilder.AddConfiguration(masaConfiguration);

if (sourceConfiguration == null)
services.AddSingleton<IConfiguration>(_ => configurationBuilder.Build());
{
var configurationManager = new ConfigurationManager();
configurationBuilder.AddConfiguration(configurationBuilder.Build());
services.AddSingleton<IConfiguration>(_ => configurationManager);
}

return services;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,35 +25,10 @@ public LocalMasaConfigurationRepository(

private void Initialize(IConfiguration configuration)
{
Dictionary<string, string> data = new();
GetData(configuration, configuration.GetChildren(), ref data);
var data = configuration.ConvertToDictionary();
_data = new Properties(data);
}

private void GetData(IConfiguration configuration, IEnumerable<IConfigurationSection> configurationSections,
ref Dictionary<string, string> dictionary)
{
foreach (var configurationSection in configurationSections)
{
var section = configuration.GetSection(configurationSection.Path);

var childrenSections = section.GetChildren()?.ToList() ?? new List<IConfigurationSection>();

if (!section.Exists() || !childrenSections.Any())
{
var key = section.Path;
if (!dictionary.ContainsKey(key))
{
dictionary.Add(key, configuration[section.Path]);
}
}
else
{
GetData(configuration, childrenSections, ref dictionary);
}
}
}

public override Properties Load()
{
return _data;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Contrib.Globalization.Localization.Dcc;

public class DccLocalizationResourceContributor : ILocalizationResourceContributor
{
private readonly IConfigurationSection _configurationSection;

public string CultureName { get; }

public DccLocalizationResourceContributor(string appId, string configObject, IMasaConfiguration masaConfiguration)
{
_configurationSection = masaConfiguration.ConfigurationApi.Get(appId).GetSection(configObject);
}

public string? GetOrNull(string name)
{
if (_configurationSection.Exists())
{
return _configurationSection.GetValue<string>(name);
}

return null;
}
}
Loading