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: AddII18N
  • Loading branch information
zhenlei520 committed Oct 31, 2022
commit 1dd1f429ede1a0e3390b79d2f1c8fd7d1f41f224
22 changes: 22 additions & 0 deletions Masa.Framework.sln
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masa.Contrib.Globalization.
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masa.Contrib.Globalization.I18N.Dcc", "src\Contrib\Globalization\Masa.Contrib.Globalization.I18N.Dcc\Masa.Contrib.Globalization.I18N.Dcc.csproj", "{B0EFDF9E-81FF-4880-992B-8A65A2008717}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masa.Contrib.Globalization.I18N.Blazor", "src\Contrib\Globalization\Masa.Contrib.Globalization.I18N.Blazor\Masa.Contrib.Globalization.I18N.Blazor.csproj", "{C6E0C9C2-F513-42EE-8C6F-ABCAF82490EB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masa.Contrib.Globalization.I18N.Blazor.Tests", "src\Contrib\Globalization\Tests\Masa.Contrib.Globalization.I18N.Blazor.Tests\Masa.Contrib.Globalization.I18N.Blazor.Tests.csproj", "{EEF78DB2-B9D9-4EC9-806F-6E3502FD0831}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -2085,6 +2089,22 @@ Global
{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
{C6E0C9C2-F513-42EE-8C6F-ABCAF82490EB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C6E0C9C2-F513-42EE-8C6F-ABCAF82490EB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C6E0C9C2-F513-42EE-8C6F-ABCAF82490EB}.Debug|x64.ActiveCfg = Debug|Any CPU
{C6E0C9C2-F513-42EE-8C6F-ABCAF82490EB}.Debug|x64.Build.0 = Debug|Any CPU
{C6E0C9C2-F513-42EE-8C6F-ABCAF82490EB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C6E0C9C2-F513-42EE-8C6F-ABCAF82490EB}.Release|Any CPU.Build.0 = Release|Any CPU
{C6E0C9C2-F513-42EE-8C6F-ABCAF82490EB}.Release|x64.ActiveCfg = Release|Any CPU
{C6E0C9C2-F513-42EE-8C6F-ABCAF82490EB}.Release|x64.Build.0 = Release|Any CPU
{EEF78DB2-B9D9-4EC9-806F-6E3502FD0831}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EEF78DB2-B9D9-4EC9-806F-6E3502FD0831}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EEF78DB2-B9D9-4EC9-806F-6E3502FD0831}.Debug|x64.ActiveCfg = Debug|Any CPU
{EEF78DB2-B9D9-4EC9-806F-6E3502FD0831}.Debug|x64.Build.0 = Debug|Any CPU
{EEF78DB2-B9D9-4EC9-806F-6E3502FD0831}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EEF78DB2-B9D9-4EC9-806F-6E3502FD0831}.Release|Any CPU.Build.0 = Release|Any CPU
{EEF78DB2-B9D9-4EC9-806F-6E3502FD0831}.Release|x64.ActiveCfg = Release|Any CPU
{EEF78DB2-B9D9-4EC9-806F-6E3502FD0831}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -2375,6 +2395,8 @@ Global
{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}
{C6E0C9C2-F513-42EE-8C6F-ABCAF82490EB} = {5B129E6F-8CB7-4E2E-9F3E-C59CF22C9CA9}
{EEF78DB2-B9D9-4EC9-806F-6E3502FD0831} = {D2E598D5-5E44-4275-AF03-4BAD07A0E13B}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {40383055-CC50-4600-AD9A-53C14F620D03}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public I18N()
_resource = I18NResourceResourceConfiguration.Resources.GetOrNull<TResourceSource>();
}

public string T(string name)
public virtual string T(string name)
=> T(name, true)!;

/// <summary>
Expand All @@ -29,9 +29,9 @@ public string T(string name)
/// <param name="name">The name of the string resource.</param>
/// <param name="returnKey">Return Key when key does not exist, default: true</param>
/// <returns></returns>
public string? T(string name, bool returnKey)
public virtual string? T(string name, bool returnKey)
{
var resourceContributor = _resource?.GetResourceContributor(CultureInfo.CurrentUICulture);
var resourceContributor = _resource?.GetResourceContributor(GetCultureInfo());
if (resourceContributor != null)
{
return resourceContributor.GetOrNull(name) ?? (returnKey ? name : null);
Expand All @@ -40,21 +40,23 @@ public string T(string name)
return returnKey ? name : null;
}

public string T(string name, params object[] arguments)
public virtual string T(string name, params object[] arguments)
=> T(name, true, arguments)!;

public string? T(string name, bool returnKey, params object[] arguments)
public virtual string? T(string name, bool returnKey, params object[] arguments)
{
ArgumentNullException.ThrowIfNull(name);

var value = this.T(name, returnKey);
if (value != null)
return string.Format(CultureInfo.CurrentUICulture, value, arguments);
return string.Format(GetCultureInfo(), value, arguments);

return null;
}

public void SetCulture(string cultureName, bool useUserOverride = true)
public virtual CultureInfo GetCultureInfo() => CultureInfo.CurrentUICulture;

public virtual void SetCulture(string cultureName, bool useUserOverride = true)
=> SetCulture(new CultureInfo(cultureName, useUserOverride));

public virtual void SetCulture(CultureInfo culture)
Expand Down

This file was deleted.

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

global using Masa.BuildingBlocks.Data;
global using Masa.BuildingBlocks.Globalization.I18N;
global using Masa.BuildingBlocks.Globalization.I18N.Options;
global using Microsoft.Extensions.DependencyInjection.Extensions;
global using Microsoft.Extensions.Options;
global using System.Globalization;
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// 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.I18N.Blazor;

[ExcludeFromCodeCoverage]
public class I18N<TResourceSource> : Masa.BuildingBlocks.Globalization.I18N.I18N<TResourceSource>
{
private const string CultureCookieKey = "Masa_I18nConfig_Culture";
private readonly IJSRuntime _jsRuntime;

public I18N(IJSRuntime jsRuntime) => _jsRuntime = jsRuntime;

const string GetCookieJs =
"(function(name){const reg = new RegExp(`(^| )${name}=([^;]*)(;|$)`);const arr = document.cookie.match(reg);if (arr) {return unescape(arr[2]);}return null;})";

const string SetCookieJs =
"(function(name,value){ var Days = 30;var exp = new Date();exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);document.cookie = `${name}=${escape(value.toString())};path=/;expires=${exp.toUTCString()}`;})";

public override CultureInfo GetCultureInfo()
{
if (_jsRuntime is IJSInProcessRuntime jsInProcess)
{
var cultureName = jsInProcess.Invoke<string>("eval", $"{GetCookieJs}('{CultureCookieKey}')");
return new CultureInfo(cultureName);
}
return CultureInfo.CurrentUICulture;
}

public override void SetCulture(CultureInfo culture)
{
try
{
_jsRuntime.InvokeVoidAsync("eval", $"{SetCookieJs}('{CultureCookieKey}','{culture.Name}')")
.ConfigureAwait(false)
.GetAwaiter()
.GetResult();
}
catch (Exception ex)
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Masa.Contrib.Globalization.I18N\Masa.Contrib.Globalization.I18N.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.JSInterop" Version="$(MicrosoftPackageVersion)" />
</ItemGroup>

</Project>
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.

// ReSharper disable once CheckNamespace

namespace Microsoft.Extensions.DependencyInjection;

public static class ServiceCollectionExtensions
{
public static IServiceCollection AddI18NForBlazor(this IServiceCollection services,
Action<I18NOptions>? action = null,
params LanguageInfo[] languages)
{
services.TryAddTransient(typeof(II18N<>), typeof(Masa.Contrib.Globalization.I18N.Blazor.I18N<>));
services.AddI18N(action, languages);
return services;
}

public static IServiceCollection AddI18NForBlazor(
this IServiceCollection services,
string languageDirectory,
Action<I18NOptions>? action = null,
params LanguageInfo[] languages)
{
services.TryAddTransient(typeof(II18N<>), typeof(I18N<>));
return services.AddI18N(languageDirectory, action, languages);
}

public static IServiceCollection AddI18NForBlazor(
this IServiceCollection services,
string languageDirectory,
string supportCultureName,
Action<I18NOptions>? action = null,
params LanguageInfo[] languages)
{
services.TryAddTransient(typeof(II18N<>), typeof(I18N<>));
services.AddI18N(languageDirectory, supportCultureName, action, languages);
return services;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

global using Masa.BuildingBlocks.Globalization.I18N;
global using Masa.Contrib.Globalization.I18N;
global using Microsoft.Extensions.DependencyInjection;
global using Microsoft.Extensions.DependencyInjection.Extensions;
global using Microsoft.JSInterop;
global using System.Diagnostics.CodeAnalysis;
global using System.Globalization;
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,24 @@

// ReSharper disable once CheckNamespace

using Masa.BuildingBlocks.Globalization.I18N.Options;

namespace Masa.Contrib.Globalization.I18N;

public static class I18NOptionsExtensions
{
public static void UseDcc(
this I18NOptions localization,
this I18NOptions i18NOptions,
params LanguageInfo[] languages)
=> localization.UseDcc(Dcc.Internal.Const.DEFAULT_CONFIG_OBJECT_NAME, Dcc.Internal.Const.SUPPORTED_CULTURES_NAME, languages);
=> i18NOptions.UseDcc(Dcc.Internal.Const.DEFAULT_CONFIG_OBJECT_NAME, Dcc.Internal.Const.SUPPORTED_CULTURES_NAME, languages);

public static void UseDcc(
this I18NOptions localization,
this I18NOptions i18NOptions,
string configObject,
string supportCultureName,
params LanguageInfo[] languages)
=> localization.UseDcc(DccConfig.AppId, configObject, supportCultureName, languages);
=> i18NOptions.UseDcc(DccConfig.AppId, configObject, supportCultureName, languages);

public static void UseDcc(
this I18NOptions localization,
this I18NOptions i18NOptions,
string appId,
string configObject,
string supportCultureName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private static IConfiguration AddJsonConfigurationSource(
}
var configurationBuilder = new ConfigurationBuilder();
var jsonLocalizationConfigurationSource =
new JsonLocalizationConfigurationSource(resourceType, languageDirectory, languages.Select(l => l.Culture),
new JsonConfigurationSource(resourceType, languageDirectory, languages.Select(l => l.Culture),
useMasaConfiguration);
configurationBuilder.Add(jsonLocalizationConfigurationSource);
var localizationConfiguration = configurationBuilder.Build();
Expand Down
Loading