void ParseHeader(POSingularEntry entry) { if (!_flags.HasFlag(Flags.SkipInfoHeaders)) { _catalog.Headers = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase); } _catalog.HeaderComments = entry.Comments; if (string.IsNullOrEmpty(entry.Translation)) { return; } foreach (var line in entry .Translation.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries) .Select(s => s.Trim())) { var index = line.IndexOf(':'); if (index <= 0) { AddWarning(DiagnosticCodes.MalformedHeaderItem, line); continue; } var key = line.Substring(0, index).TrimEnd(); var value = line.Remove(0, index + 1).TrimStart(); if (_flags.HasFlag(Flags.ReadContentTypeHeaderOnly) && !string.Equals(key, "content-type", StringComparison.OrdinalIgnoreCase)) { continue; } switch (key.ToLowerInvariant()) { case "content-type": ParseEncoding(line, value); break; case "language": ParseLanguage(line, value); break; case "plural-forms": ParsePluralForms(line, value); break; } if (!_flags.HasFlag(Flags.SkipInfoHeaders)) { if (_catalog.Headers.TryGetValue(key, out string existingValue)) { AddWarning(DiagnosticCodes.DuplicateHeaderKey, key); } _catalog.Headers[key] = value; } } }
public DebugView(POSingularEntry entry) { if (entry == null) { throw new ArgumentNullException(nameof(entry)); } _entry = entry; }
private bool TryReadEntry(bool allowEmptyId, out IPOEntry result) { if (_line == null) { result = null; return(false); } var entryLocation = new TextLocation(_lineIndex, _columnIndex); List <POComment> comments = _commentBuffer != null?ParseComments() : null; Dictionary <int, string> translations = null; string id = null, pluralId = null, contextId = null; IPOEntry entry = null; EntryTokens expectedTokens = EntryTokens.Id | EntryTokens.PluralId | EntryTokens.ContextId; do { EntryTokens token = DetectEntryToken(out int tokenLength) & expectedTokens; if (token == EntryTokens.None) { if (!(expectedTokens == EntryTokens.Translation && entry is POPluralEntry)) { AddError(DiagnosticCodes.UnexpectedToken, new TextLocation(_lineIndex, _columnIndex)); result = null; return(false); } else { break; } } _columnIndex += tokenLength; switch (token) { case EntryTokens.Id: _columnIndex = FindNextTokenInLine(requireWhiteSpace: true); if (_columnIndex < 0 || !TryReadString(out id)) { result = null; return(false); } expectedTokens &= ~EntryTokens.Id; expectedTokens |= EntryTokens.Translation; break; case EntryTokens.PluralId: _columnIndex = FindNextTokenInLine(requireWhiteSpace: true); if (_columnIndex < 0 || !TryReadString(out pluralId)) { result = null; return(false); } expectedTokens &= ~EntryTokens.PluralId; break; case EntryTokens.ContextId: _columnIndex = FindNextTokenInLine(requireWhiteSpace: true); if (_columnIndex < 0 || !TryReadString(out contextId)) { result = null; return(false); } expectedTokens &= ~EntryTokens.ContextId; break; case EntryTokens.Translation: var originalColumnIndex = _columnIndex; TryReadPluralIndex(out int?pluralIndex); _columnIndex = FindNextTokenInLine(requireWhiteSpace: true); if (_columnIndex < 0 || !TryReadString(out string value)) { result = null; return(false); } if (!allowEmptyId && entry == null && id == string.Empty) { AddError(Resources.InvalidEntryKey, entryLocation); result = null; return(false); } // plural if (pluralId != null) { if (pluralIndex != null) { if (pluralIndex < 0 || pluralIndex >= _catalog.PluralFormCount) { AddWarning(DiagnosticCodes.InvalidPluralIndex, new TextLocation(_lineIndex, originalColumnIndex)); break; } } else { AddWarning(DiagnosticCodes.MissingPluralIndex, new TextLocation(_lineIndex, originalColumnIndex)); pluralIndex = 0; } if (entry == null) { entry = new POPluralEntry(new POKey(id, pluralId, contextId)) { Comments = comments, }; translations = new Dictionary <int, string>(); } if (translations.ContainsKey(pluralIndex.Value)) { AddWarning(DiagnosticCodes.DuplicatePluralForm, entryLocation, pluralIndex.Value); } translations[pluralIndex.Value] = value; expectedTokens = EntryTokens.Translation; } // singular else { if (pluralIndex != null) { if (pluralIndex != 0) { AddError(DiagnosticCodes.InvalidPluralIndex, new TextLocation(_lineIndex, originalColumnIndex)); break; } else { AddWarning(DiagnosticCodes.UnnecessaryPluralIndex, new TextLocation(_lineIndex, originalColumnIndex)); } } entry = new POSingularEntry(new POKey(id, null, contextId)) { Comments = comments, Translation = value }; expectedTokens = EntryTokens.None; } break; } SeekNextToken(); }while (_line != null && expectedTokens != EntryTokens.None); if (entry is POPluralEntry pluralEntry) { var n = _catalog.PluralFormCount; for (var i = 0; i < n; i++) { if (translations.TryGetValue(i, out string value)) { pluralEntry.Add(value); } else { pluralEntry.Add(null); AddWarning(DiagnosticCodes.MissingPluralForm, entryLocation, i); } } } if (entry == null) { AddError(DiagnosticCodes.IncompleteEntry, entryLocation); result = null; return(false); } result = entry; return(true); }