Browse Source

Custom languages.

pull/381/head
Sebastian Stehle 7 years ago
parent
commit
6403aeac8c
  1. 2
      src/Squidex.Infrastructure/Language.cs
  2. 39
      src/Squidex.Infrastructure/LanguagesInitializer.cs
  3. 15
      src/Squidex.Infrastructure/LanguagesOptions.cs
  4. 4
      src/Squidex/Config/Domain/InfrastructureServices.cs
  5. 3
      src/Squidex/WebStartup.cs
  6. 7
      src/Squidex/appsettings.json
  7. 61
      tests/Squidex.Infrastructure.Tests/LanguagesInitializerTests.cs

2
src/Squidex.Infrastructure/Language.cs

@ -16,7 +16,7 @@ namespace Squidex.Infrastructure
private static readonly Regex CultureRegex = new Regex("^([a-z]{2})(\\-[a-z]{2})?$", RegexOptions.IgnoreCase);
private static readonly Dictionary<string, Language> AllLanguagesField = new Dictionary<string, Language>(StringComparer.OrdinalIgnoreCase);
private static Language AddLanguage(string iso2Code, string englishName)
internal static Language AddLanguage(string iso2Code, string englishName)
{
return AllLanguagesField.GetOrAdd(iso2Code, englishName, (c, n) => new Language(c, n));
}

39
src/Squidex.Infrastructure/LanguagesInitializer.cs

@ -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;
}
}
}

15
src/Squidex.Infrastructure/LanguagesOptions.cs

@ -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>
{
}
}

4
src/Squidex/Config/Domain/InfrastructureServices.cs

@ -16,6 +16,7 @@ using Squidex.Areas.Api.Controllers.News.Service;
using Squidex.Domain.Apps.Entities.Apps.Diagnostics;
using Squidex.Domain.Apps.Entities.Rules.UsageTracking;
using Squidex.Domain.Users;
using Squidex.Infrastructure;
using Squidex.Infrastructure.Caching;
using Squidex.Infrastructure.Diagnostics;
using Squidex.Infrastructure.EventSourcing.Grains;
@ -57,6 +58,9 @@ namespace Squidex.Config.Domain
services.AddSingletonAs<GrainBootstrap<IUsageTrackerGrain>>()
.AsSelf();
services.AddSingletonAs<LanguagesInitializer>()
.AsSelf();
services.AddSingletonAs<DeepLTranslator>()
.As<ITranslator>();

3
src/Squidex/WebStartup.cs

@ -29,6 +29,7 @@ using Squidex.Config.Startup;
using Squidex.Config.Web;
using Squidex.Domain.Apps.Entities.Assets;
using Squidex.Domain.Apps.Entities.Contents;
using Squidex.Infrastructure;
using Squidex.Infrastructure.Commands;
using Squidex.Infrastructure.Diagnostics;
using Squidex.Infrastructure.Translations;
@ -81,6 +82,8 @@ namespace Squidex
config.GetSection("assets"));
services.Configure<DeepLTranslatorOptions>(
config.GetSection("translations:deepL"));
services.Configure<LanguagesOptions>(
config.GetSection("languages"));
services.Configure<ReadonlyOptions>(
config.GetSection("mode"));
services.Configure<RobotsTxtOptions>(

7
src/Squidex/appsettings.json

@ -32,6 +32,13 @@
"strong": false
},
"languages": {
/*
* Use custom langauges where the key is the language code and the value is the english name.
*/
"custom": ""
},
"ui": {
/*
* Regex suggestions for the UI

61
tests/Squidex.Infrastructure.Tests/LanguagesInitializerTests.cs

@ -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…
Cancel
Save