~ruther/NosSmooth

ref: 18763fe567377741ff1330cb6ae6940cc3bd899b NosSmooth/Data/NosSmooth.Data.NOSFiles/LanguageService.cs -rw-r--r-- 2.8 KiB
18763fe5 — František Boháček tests(game): add raid tests 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//
//  LanguageService.cs
//
//  Copyright (c) František Boháček. All rights reserved.
//  Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Security.Cryptography;
using NosSmooth.Data.Abstractions.Language;
using Remora.Results;

namespace NosSmooth.Data.NOSFiles;

/// <inheritdoc />
internal class LanguageService : ILanguageService
{
    private readonly
        IReadOnlyDictionary<Language, IReadOnlyDictionary<TranslationRoot, IReadOnlyDictionary<string, string>>>
        _translations;

    private readonly LanguageServiceOptions _options;

    /// <summary>
    /// Initializes a new instance of the <see cref="LanguageService"/> class.
    /// </summary>
    /// <param name="translations">The translations.</param>
    /// <param name="options">The options.</param>
    public LanguageService
    (
        IReadOnlyDictionary<Language, IReadOnlyDictionary<TranslationRoot, IReadOnlyDictionary<string, string>>>
            translations,
        LanguageServiceOptions options
    )
    {
        CurrentLanguage = options.Language;
        _translations = translations;
        _options = options;
    }

    /// <inheritdoc/>
    public Language CurrentLanguage { get; set; }

    /// <inheritdoc/>
    public Task<Result<string>> GetTranslationAsync
    (
        TranslationRoot root,
        string key,
        Language? language = default,
        CancellationToken ct = default
    )
    {
        if (!_translations.ContainsKey(language ?? CurrentLanguage))
        {
            return Task.FromResult
            (
                Result<string>.FromError
                    (new NotFoundError($"The requested language {language ?? CurrentLanguage} is not parsed."))
            );
        }

        var translations = _translations[language ?? CurrentLanguage];
        if (!translations.ContainsKey(root))
        {
            return Task.FromResult(Result<string>.FromSuccess(key));
        }

        var keyTranslations = translations[root];
        if (!keyTranslations.ContainsKey(key))
        {
            return Task.FromResult(Result<string>.FromSuccess(key));
        }

        return Task.FromResult(Result<string>.FromSuccess(keyTranslations[key]));
    }

    /// <inheritdoc/>
    public async Task<Result<string>> GetTranslationAsync
    (
        TranslatableString translatableString,
        Language? language = default,
        CancellationToken ct = default
    )
    {
        var translation = await GetTranslationAsync(translatableString.Root, translatableString.Key, language, ct);
        if (!translation.IsSuccess)
        {
            return translation;
        }

        if (_options.FillTranslatableStrings)
        {
            translatableString.Fill(translation.Entity);
        }

        return translation;
    }
}
Do not follow this link