mirror of https://github.com/Squidex/squidex.git
7 changed files with 130 additions and 1 deletions
@ -0,0 +1,39 @@ |
|||||
|
// ==========================================================================
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
||||
|
// All rights reserved. Licensed under the MIT license.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System.Threading; |
||||
|
using System.Threading.Tasks; |
||||
|
using Microsoft.Extensions.Options; |
||||
|
using Squidex.Infrastructure.Tasks; |
||||
|
|
||||
|
namespace Squidex.Infrastructure |
||||
|
{ |
||||
|
public sealed class LanguagesInitializer : IInitializable |
||||
|
{ |
||||
|
private readonly LanguagesOptions options; |
||||
|
|
||||
|
public LanguagesInitializer(IOptions<LanguagesOptions> options) |
||||
|
{ |
||||
|
Guard.NotNull(options, nameof(options)); |
||||
|
|
||||
|
this.options = options.Value; |
||||
|
} |
||||
|
|
||||
|
public Task InitializeAsync(CancellationToken ct = default) |
||||
|
{ |
||||
|
foreach (var kvp in options) |
||||
|
{ |
||||
|
if (!string.IsNullOrWhiteSpace(kvp.Key) && !string.IsNullOrWhiteSpace(kvp.Value)) |
||||
|
{ |
||||
|
Language.AddLanguage(kvp.Key, kvp.Value); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return TaskHelper.Done; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,15 @@ |
|||||
|
// ==========================================================================
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
||||
|
// All rights reserved. Licensed under the MIT license.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System.Collections.Generic; |
||||
|
|
||||
|
namespace Squidex.Infrastructure |
||||
|
{ |
||||
|
public sealed class LanguagesOptions : Dictionary<string, string> |
||||
|
{ |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,61 @@ |
|||||
|
// ==========================================================================
|
||||
|
// Squidex Headless CMS
|
||||
|
// ==========================================================================
|
||||
|
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
||||
|
// All rights reserved. Licensed under the MIT license.
|
||||
|
// ==========================================================================
|
||||
|
|
||||
|
using System.Threading.Tasks; |
||||
|
using Microsoft.Extensions.Options; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Squidex.Infrastructure |
||||
|
{ |
||||
|
public sealed class LanguagesInitializerTests |
||||
|
{ |
||||
|
[Fact] |
||||
|
public async Task Should_add_custom_languages() |
||||
|
{ |
||||
|
var options = Options.Create(new LanguagesOptions |
||||
|
{ |
||||
|
["en-NO"] = "English (Norwegian)" |
||||
|
}); |
||||
|
|
||||
|
var sut = new LanguagesInitializer(options); |
||||
|
|
||||
|
await sut.InitializeAsync(); |
||||
|
|
||||
|
Assert.Equal("English (Norwegian)", Language.GetLanguage("en-NO").EnglishName); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Should_not_add_invalid_languages() |
||||
|
{ |
||||
|
var options = Options.Create(new LanguagesOptions |
||||
|
{ |
||||
|
["en-Error"] = null |
||||
|
}); |
||||
|
|
||||
|
var sut = new LanguagesInitializer(options); |
||||
|
|
||||
|
await sut.InitializeAsync(); |
||||
|
|
||||
|
Assert.False(Language.TryGetLanguage("en-Error", out _)); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Should_not_override_existing_languages() |
||||
|
{ |
||||
|
var options = Options.Create(new LanguagesOptions |
||||
|
{ |
||||
|
["de"] = "German (Germany)" |
||||
|
}); |
||||
|
|
||||
|
var sut = new LanguagesInitializer(options); |
||||
|
|
||||
|
await sut.InitializeAsync(); |
||||
|
|
||||
|
Assert.Equal("German", Language.GetLanguage("de").EnglishName); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue