Exemplo n.º 1
0
            public void Should_Translate_And_UseArgs_WithParameters()
            {
                var translations = new Dictionary <string, IReadOnlyDictionary <string, string> >
                {
                    ["translation1"] = new Dictionary <string, string>
                    {
                        ["key1"] = "message1 {arg1|case=upper} {arg1|case=lower}"
                    }
                };

                var translator = new MessageTranslator(translations);

                var error = new Error()
                {
                    Messages = new[]
                    {
                        "key1",
                    },
                    Args = new IArg[]
                    {
                        Arg.Text("arg1", "arg1Value"),
                    }
                };

                var result = translator.TranslateMessages("translation1", error);

                result.Messages.Count.Should().Be(1);
                result.Messages.Should().Contain("message1 ARG1VALUE arg1value");

                result.AnyPathPlaceholders.Should().Be(false);
                result.IndexedPathPlaceholders.Should().BeEmpty();
            }
Exemplo n.º 2
0
            public void Should_ThrowException_When_Error_ContainsNullArg()
            {
                var translations = new Dictionary <string, IReadOnlyDictionary <string, string> >
                {
                    ["translation1"] = new Dictionary <string, string>
                    {
                        ["key1"] = "message1"
                    },
                    ["translation2"] = new Dictionary <string, string>
                    {
                        ["key2"] = "message2"
                    }
                };

                var translator = new MessageTranslator(translations);

                var error = new Error()
                {
                    Messages = new[]
                    {
                        "key1",
                    },
                    Args = new IArg[]
                    {
                        Arg.Text("test", "test"),
                        null
                    }
                };

                Action action = () => translator.TranslateMessages("translation1", error);

                action.Should().ThrowExactly <ArgumentNullException>();
            }
Exemplo n.º 3
0
            public void Should_Translate_And_UseArgs_Special_Translation_Recursion()
            {
                var translations = new Dictionary <string, IReadOnlyDictionary <string, string> >
                {
                    ["translation1"] = new Dictionary <string, string>
                    {
                        ["key1"] = "message1 {_translation|key=key1}",
                    }
                };

                var translator = new MessageTranslator(translations);

                var error = new Error()
                {
                    Messages = new[]
                    {
                        "key1",
                    },
                    Args = new IArg[]
                    {
                    }
                };

                var result = translator.TranslateMessages("translation1", error);

                result.Messages.Count.Should().Be(1);
                result.Messages.Should().Contain("message1 message1 {_translation|key=key1}");

                result.AnyPathPlaceholders.Should().Be(false);
                result.IndexedPathPlaceholders.Should().BeEmpty();
            }
Exemplo n.º 4
0
            public void Should_Translate_ReturnSameMessage_When_KeyInTranslation()
            {
                var translations = new Dictionary <string, IReadOnlyDictionary <string, string> >
                {
                    ["translation1"] = new Dictionary <string, string>
                    {
                        ["key1"] = "message1"
                    },
                    ["translation2"] = new Dictionary <string, string>
                    {
                        ["key1"] = "message2"
                    }
                };

                var translator = new MessageTranslator(translations);

                var error = new Error()
                {
                    Messages = new[]
                    {
                        "key123"
                    },
                    Args = new IArg[]
                    {
                    }
                };

                var result = translator.TranslateMessages("translation2", error);

                result.Messages.Count.Should().Be(1);
                result.Messages.Should().Contain("key123");

                result.AnyPathPlaceholders.Should().Be(false);
                result.IndexedPathPlaceholders.Should().BeEmpty();
            }
Exemplo n.º 5
0
            public void Should_ThrowException_When_TranslationNameNotFound()
            {
                var translations = new Dictionary <string, IReadOnlyDictionary <string, string> >
                {
                    ["translation1"] = new Dictionary <string, string>
                    {
                        ["key1"] = "message1"
                    },
                    ["translation2"] = new Dictionary <string, string>
                    {
                        ["key2"] = "message2"
                    }
                };

                var translator = new MessageTranslator(translations);

                var error = new Error()
                {
                    Messages = new[]
                    {
                        "key1"
                    },
                    Args = new IArg[] { }
                };

                Action action = () => translator.TranslateMessages("translation3", error);

                action.Should().ThrowExactly <KeyNotFoundException>();
            }
Exemplo n.º 6
0
            public void Should_ThrowException_When_NullTranslationName()
            {
                var translations = new Dictionary <string, IReadOnlyDictionary <string, string> >
                {
                    ["translation1"] = new Dictionary <string, string>
                    {
                        ["key1"] = "message1"
                    },
                    ["translation2"] = new Dictionary <string, string>
                    {
                        ["key2"] = "message2"
                    }
                };

                var translator = new MessageTranslator(translations);

                var error = new Error()
                {
                    Messages = new[]
                    {
                        "key1"
                    }
                };

                Action action = () => translator.TranslateMessages(null, error);

                action.Should().ThrowExactly <ArgumentNullException>();
            }
Exemplo n.º 7
0
            public void Should_Translate_And_ExtractPathPlaceholders()
            {
                var translations = new Dictionary <string, IReadOnlyDictionary <string, string> >
                {
                    ["translation1"] = new Dictionary <string, string>
                    {
                        ["key1"] = "message1",
                    }
                };

                var translator = new MessageTranslator(translations);

                var error = new Error()
                {
                    Messages = new[]
                    {
                        "key1",
                        "message with path: {_path}",
                        "message with name: {_name}",
                        "message with path and name: {_path} {_name}",
                    },
                    Args = new IArg[]
                    {
                    }
                };

                var result = translator.TranslateMessages("translation1", error);

                result.Messages.Count.Should().Be(4);
                result.Messages.ElementAt(0).Should().Be("message1");
                result.Messages.ElementAt(1).Should().Be("message with path: {_path}");
                result.Messages.ElementAt(2).Should().Be("message with name: {_name}");
                result.Messages.ElementAt(3).Should().Be("message with path and name: {_path} {_name}");

                result.AnyPathPlaceholders.Should().Be(true);
                result.IndexedPathPlaceholders.Count.Should().Be(3);

                result.IndexedPathPlaceholders.Keys.Should().Contain(1);
                result.IndexedPathPlaceholders.Keys.Should().Contain(2);
                result.IndexedPathPlaceholders.Keys.Should().Contain(3);

                result.IndexedPathPlaceholders[1].Count.Should().Be(1);
                result.IndexedPathPlaceholders[1].ElementAt(0).Name.Should().Be("_path");
                result.IndexedPathPlaceholders[1].ElementAt(0).Placeholder.Should().Be("{_path}");
                result.IndexedPathPlaceholders[1].ElementAt(0).Parameters.Should().BeEmpty();

                result.IndexedPathPlaceholders[2].Count.Should().Be(1);
                result.IndexedPathPlaceholders[2].ElementAt(0).Name.Should().Be("_name");
                result.IndexedPathPlaceholders[2].ElementAt(0).Placeholder.Should().Be("{_name}");
                result.IndexedPathPlaceholders[2].ElementAt(0).Parameters.Should().BeEmpty();

                result.IndexedPathPlaceholders[3].Count.Should().Be(2);
                result.IndexedPathPlaceholders[3].ElementAt(0).Name.Should().Be("_path");
                result.IndexedPathPlaceholders[3].ElementAt(0).Placeholder.Should().Be("{_path}");
                result.IndexedPathPlaceholders[3].ElementAt(0).Parameters.Should().BeEmpty();
                result.IndexedPathPlaceholders[3].ElementAt(1).Name.Should().Be("_name");
                result.IndexedPathPlaceholders[3].ElementAt(1).Placeholder.Should().Be("{_name}");
                result.IndexedPathPlaceholders[3].ElementAt(1).Parameters.Should().BeEmpty();
            }
Exemplo n.º 8
0
        private MessageCache BuildMessageCache(MessageTranslator translator, IReadOnlyDictionary <int, IError> errors, IReadOnlyDictionary <string, IReadOnlyList <int> > template)
        {
            ThrowHelper.NullArgument(errors, nameof(errors));
            ThrowHelper.NullArgument(template, nameof(template));
            ThrowHelper.NullInCollection(template.Values.ToArray(), $"{nameof(template)}.{nameof(template.Values)}");

            var uniqueErrorsIds = template.SelectMany(b => b.Value).Distinct().ToArray();

            var cache = new MessageCache();

            foreach (var translationName in TranslationNames)
            {
                foreach (var errorId in uniqueErrorsIds)
                {
                    var translationResult = translator.TranslateMessages(translationName, errors[errorId]);

                    cache.AddMessage(translationName, errorId, translationResult.Messages);

                    if (translationResult.AnyPathPlaceholders)
                    {
                        cache.AddIndexedPathPlaceholders(translationName, errorId, translationResult.IndexedPathPlaceholders);
                    }
                }
            }

            foreach (var translationName in TranslationNames)
            {
                foreach (var templatePair in template)
                {
                    var path = templatePair.Key;

                    foreach (var errorId in templatePair.Value)
                    {
                        if (!cache.ContainsPathArgs(translationName, errorId) || PathHelper.ContainsIndexes(path))
                        {
                            continue;
                        }

                        var cachedMessages      = cache.GetMessages(translationName, errorId);
                        var indexedPlaceholders = cache.GetIndexedPathPlaceholders(translationName, errorId);

                        var errorMessagesWithSpecials = MessageTranslator.TranslateMessagesWithPathPlaceholders(path, cachedMessages, indexedPlaceholders);

                        cache.AddMessageWithPathArgs(translationName, path, errorId, errorMessagesWithSpecials);
                    }
                }
            }

            return(cache);
        }
Exemplo n.º 9
0
            public void Should_Translate_And_UseArgs()
            {
                var translations = new Dictionary <string, IReadOnlyDictionary <string, string> >
                {
                    ["translation1"] = new Dictionary <string, string>
                    {
                        ["key1"] = "message1 {arg1}",
                        ["key2"] = "message2 {arg2}"
                    },
                    ["translation2"] = new Dictionary <string, string>
                    {
                        ["key1"] = "message11 {arg1}",
                        ["key2"] = "message22 {arg1} {arg2}",
                    }
                };

                var translator = new MessageTranslator(translations);

                var error = new Error()
                {
                    Messages = new[]
                    {
                        "key1",
                        "key2",
                    },
                    Args = new IArg[]
                    {
                        Arg.Text("arg1", "arg1Value"),
                        Arg.Text("arg2", "arg2Value")
                    }
                };

                var result = translator.TranslateMessages("translation2", error);

                result.Messages.Count.Should().Be(2);
                result.Messages.Should().Contain("message11 arg1Value");
                result.Messages.Should().Contain("message22 arg1Value arg2Value");

                result.AnyPathPlaceholders.Should().Be(false);
                result.IndexedPathPlaceholders.Should().BeEmpty();
            }