mirror of https://github.com/Squidex/squidex.git
7 changed files with 101 additions and 92 deletions
@ -0,0 +1,69 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System.Globalization; |
|||
|
|||
namespace Squidex.Infrastructure.Commands |
|||
{ |
|||
public sealed class CommandRequest |
|||
{ |
|||
public IAggregateCommand Command { get; } |
|||
|
|||
public string Culture { get; } |
|||
|
|||
public string CultureUI { get; } |
|||
|
|||
public CommandRequest(IAggregateCommand command, string culture, string cultureUI) |
|||
{ |
|||
Command = command; |
|||
|
|||
Culture = culture; |
|||
CultureUI = cultureUI; |
|||
} |
|||
|
|||
public static CommandRequest Create(IAggregateCommand command) |
|||
{ |
|||
return new CommandRequest(command, |
|||
CultureInfo.CurrentCulture.Name, |
|||
CultureInfo.CurrentUICulture.Name); |
|||
} |
|||
|
|||
public void ApplyContext() |
|||
{ |
|||
var culture = GetCulture(Culture); |
|||
|
|||
if (culture != null) |
|||
{ |
|||
CultureInfo.CurrentCulture = culture; |
|||
} |
|||
|
|||
var uiCulture = GetCulture(CultureUI); |
|||
|
|||
if (uiCulture != null) |
|||
{ |
|||
CultureInfo.CurrentUICulture = uiCulture; |
|||
} |
|||
} |
|||
|
|||
private static CultureInfo? GetCulture(string name) |
|||
{ |
|||
if (string.IsNullOrWhiteSpace(name)) |
|||
{ |
|||
return null; |
|||
} |
|||
|
|||
try |
|||
{ |
|||
return CultureInfo.GetCultureInfo(name); |
|||
} |
|||
catch (CultureNotFoundException) |
|||
{ |
|||
return null; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -1,55 +0,0 @@ |
|||
// ==========================================================================
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|||
// All rights reserved. Licensed under the MIT license.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using System.Globalization; |
|||
using System.Runtime.Serialization; |
|||
|
|||
namespace Squidex.Infrastructure.Orleans |
|||
{ |
|||
[Serializable] |
|||
public class GrainContext : ISerializable |
|||
{ |
|||
public CultureInfo Culture { get; private set; } |
|||
|
|||
public CultureInfo CultureUI { get; private set; } |
|||
|
|||
private GrainContext() |
|||
{ |
|||
} |
|||
|
|||
protected GrainContext(SerializationInfo info, StreamingContext context) |
|||
{ |
|||
Culture = CultureInfo.GetCultureInfo(info.GetString(nameof(Culture))!); |
|||
CultureUI = CultureInfo.GetCultureInfo(info.GetString(nameof(CultureUI))!); |
|||
} |
|||
|
|||
public virtual void GetObjectData(SerializationInfo info, StreamingContext context) |
|||
{ |
|||
info.AddValue(nameof(Culture), Culture.Name); |
|||
info.AddValue(nameof(CultureUI), CultureUI.Name); |
|||
} |
|||
|
|||
public static GrainContext Create() |
|||
{ |
|||
return new GrainContext |
|||
{ |
|||
Culture = CultureInfo.CurrentCulture, |
|||
CultureUI = CultureInfo.CurrentUICulture |
|||
}; |
|||
} |
|||
|
|||
public void Use() |
|||
{ |
|||
if (Culture != null) |
|||
{ |
|||
CultureInfo.CurrentCulture = Culture; |
|||
CultureInfo.CurrentUICulture = CultureUI; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue