public void Should_Translate_WithPathArgs_And_Leave_When_MissingPlaceholders() { var errorMessages = new[] { "message1", "message2 {_name} {_path}", "message3", "message4 {_name}", "message5 {_path}", }; var indexedPathsPlaceholders = new Dictionary <int, IReadOnlyList <ArgPlaceholder> >() { [1] = new[] { ParameterlessNamePlaceholders, }, [4] = new[] { ParameterlessPathPlaceholders } }; var results = MessageTranslator.TranslateMessagesWithPathPlaceholders("some.path", errorMessages, indexedPathsPlaceholders); results.Count.Should().Be(5); results[0].Should().Be("message1"); results[1].Should().Be("message2 path {_path}"); results[2].Should().Be("message3"); results[3].Should().Be("message4 {_name}"); results[4].Should().Be("message5 some.path"); }
public void Should_ThrowException_When_NullIndexedPathHolders() { var errorMessages = new[] { "message1", "message2 {_path}" }; Action action = () => MessageTranslator.TranslateMessagesWithPathPlaceholders("path", errorMessages, null); action.Should().ThrowExactly <NullReferenceException>(); }
public IReadOnlyDictionary <string, IReadOnlyList <string> > GetMessages(Dictionary <string, List <int> > errors, string translationName = null) { var results = new Dictionary <string, IReadOnlyList <string> >(errors.Count); translationName = translationName ?? nameof(Translation.English); foreach (var pair in errors) { var path = pair.Key; var errorsIds = pair.Value; var capacity = _cache.GetMessageAmount(errorsIds); if (capacity == 0) { continue; } var allMessages = new string[capacity]; var index = 0; for (var i = 0; i < errorsIds.Count; ++i) { var errorId = errorsIds[i]; IReadOnlyList <string> messages; if (!_cache.ContainsPathArgs(translationName, errorId)) { messages = _cache.GetMessages(translationName, errorId); } else if (_cache.IsMessageWithPathArgsCached(translationName, path, errorId)) { messages = _cache.GetMessagesWithPathArgs(translationName, path, errorId); } else { var cachedMessages = _cache.GetMessages(translationName, errorId); var indexedPathPlaceholders = _cache.GetIndexedPathPlaceholders(translationName, errorId); messages = MessageTranslator.TranslateMessagesWithPathPlaceholders(path, cachedMessages, indexedPathPlaceholders); } CopyMessages(messages, allMessages, ref index); } results.Add(path, allMessages); } return(results); }
public void Should_ThrowException_When_ErrorMessages_IsNull() { var indexedPathsPlaceholders = new Dictionary <int, IReadOnlyList <ArgPlaceholder> >() { [1] = new[] { ParameterlessNamePlaceholders } }; Action action = () => MessageTranslator.TranslateMessagesWithPathPlaceholders("path", null, indexedPathsPlaceholders); action.Should().ThrowExactly <NullReferenceException>(); }
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); }
public void Should_Translate_WithNameArg(string path, ArgPlaceholder placeholder, string message, string expectedTranslatedMessage) { var indexedPathsPlaceholders = new Dictionary <int, IReadOnlyList <ArgPlaceholder> >() { [0] = new[] { placeholder } }; var results = MessageTranslator.TranslateMessagesWithPathPlaceholders(path, new[] { message }, indexedPathsPlaceholders); results.Count.Should().Be(1); results[0].Should().Be(expectedTranslatedMessage); }
public void Should_Translate_WithPathArgs_WithParameters() { var errorMessages = new[] { "message1", "message2 {_name|format=titleCase} {_path}", "message3 >{_name}<", "message4 '{_name|format=titleCase}'", "message5 {_path}", }; var indexedPathsPlaceholders = new Dictionary <int, IReadOnlyList <ArgPlaceholder> >() { [1] = new[] { TitleCaseNamePlaceholders, ParameterlessPathPlaceholders }, [2] = new[] { ParameterlessNamePlaceholders }, [3] = new[] { TitleCaseNamePlaceholders }, [4] = new[] { ParameterlessPathPlaceholders } }; var results = MessageTranslator.TranslateMessagesWithPathPlaceholders("some.path.veryImportantPath", errorMessages, indexedPathsPlaceholders); results.Count.Should().Be(5); results[0].Should().Be("message1"); results[1].Should().Be("message2 Very Important Path some.path.veryImportantPath"); results[2].Should().Be("message3 >veryImportantPath<"); results[3].Should().Be("message4 'Very Important Path'"); results[4].Should().Be("message5 some.path.veryImportantPath"); }