Пример #1
0
        private void MergeWithEntry(
            string context,
            string msgid,
            string msgidPlural,
            string inputFile,
            int line)
        {
            // Processing
            CatalogEntry entry      = Catalog.FindItem(msgid, context);
            bool         entryFound = entry != null;

            if (!entryFound)
            {
                entry = new CatalogEntry(Catalog, msgid, msgidPlural);
            }

            // Add source reference if it not exists yet
            // Each reference is in the form "path_name:line_number"
            string sourceRef = String.Format("{0}:{1}",
                                             Utils.FileUtils.GetRelativeUri(Path.GetFullPath(inputFile), Path.GetFullPath(Options.OutFile)),
                                             line);

            entry.AddReference(sourceRef); // Wont be added if exists

            if (FormatValidator.IsFormatString(msgid) || FormatValidator.IsFormatString(msgidPlural))
            {
                if (!entry.IsInFormat("csharp"))
                {
                    entry.Flags += ", csharp-format";
                }
                Trace.WriteLineIf(
                    !FormatValidator.IsValidFormatString(msgid),
                    String.Format("Warning: string format may be invalid: '{0}'\nSource: {1}", msgid, sourceRef));
                Trace.WriteLineIf(
                    !FormatValidator.IsValidFormatString(msgidPlural),
                    String.Format("Warning: plural string format may be invalid: '{0}'\nSource: {1}", msgidPlural, sourceRef));
            }

            if (!String.IsNullOrEmpty(msgidPlural))
            {
                if (!entryFound)
                {
                    AddPluralsTranslations(entry);
                }
                else
                {
                    UpdatePluralEntry(entry, msgidPlural);
                }
            }
            if (!String.IsNullOrEmpty(context))
            {
                entry.Context = context;
                entry.AddAutoComment(String.Format("Context: {0}", context), true);
            }

            if (!entryFound)
            {
                Catalog.AddItem(entry);
            }
        }
Пример #2
0
        private void MergeMessage([NotNull] Message message)
        {
            var entry    = Catalog.FindItem(message.Text, message.Context);
            var newEntry = entry == null;

            if (newEntry)
            {
                entry = new CatalogEntry(Catalog, message.Text, message.PluralText);
            }

            // Source reference
            if (message.Source != null)
            {
                var filePath = FileUtils.GetRelativeUri(message.Source.FullPath, Path.GetFullPath(Options.OutputFile));
                entry.AddReference($"{filePath}:{message.LineNumber}");
            }
            // Plural
            if (!string.IsNullOrEmpty(message.PluralText))
            {
                entry.SetTranslations(Enumerable.Repeat("", Catalog.PluralFormsCount).ToArray());
                entry.SetPluralString(message.PluralText);
            }
            // Context
            if (!string.IsNullOrEmpty(message.Context))
            {
                entry.Context = message.Context;
            }
            // Auto comments
            if (!string.IsNullOrEmpty(message.Comment))
            {
                entry.AddAutoComment(message.Comment, true);
            }
            // Preserve previous comments
            if (newEntry && Options.PreserveComments)
            {
                var previousEntry = previousCatalog?.FindItem(entry);
                if (previousEntry != null)
                {
                    // Simple comment
                    if (previousEntry.HasComment && !entry.HasComment)
                    {
                        entry.Comment = previousEntry.Comment;
                    }
                    // Auto comments
                    foreach (var comment in previousEntry.AutoComments)
                    {
                        entry.AddAutoComment(comment, true);
                    }
                }
            }
            // Add entry if it did not exist
            if (newEntry)
            {
                Catalog.AddItem(entry);
            }
        }
Пример #3
0
 public void AddOrModifyBook(string isbn, Book book)
 {
     if (book == null)
     {
         throw new ArgumentNullException("book");
     }
     if (Catalog.ItemExists(isbn))
     {
         Catalog.UpdateItem(isbn,
                            new CatalogItem
         {
             Author      = book.Author,
             Description = book.Description,
             Isbn        = book.Isbn,
             Language    = book.Language,
             Published   = book.Published,
             Summary     = book.Summary,
             Title       = book.Title
         });
     }
     else
     {
         book.Isbn = isbn;
         Catalog.AddItem(
             new CatalogItem
         {
             Author      = book.Author,
             Description = book.Description,
             Isbn        = book.Isbn,
             Language    = book.Language,
             Published   = book.Published,
             Summary     = book.Summary,
             Title       = book.Title
         });
         WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Created;
     }
 }
		public virtual void UpdateCatalog (TranslationProject project, Catalog catalog, IProgressMonitor monitor, string fileName)
		{
			string text = File.ReadAllText (fileName);
			string relativeFileName = MonoDevelop.Core.FileService.AbsoluteToRelativePath (project.BaseDirectory, fileName);
			string fileNamePrefix   = relativeFileName + ":";
			if (String.IsNullOrEmpty (text))
				return;
			
			// Get a list of all excluded regions
			List<Match> excludeMatches = new List<Match> ();
			foreach (Regex regex in excluded) {
				foreach (Match m in regex.Matches (text))
					excludeMatches.Add (m);
			}
			
			// Sort the list by match index
			excludeMatches.Sort (delegate (Match a, Match b) {
				return a.Index.CompareTo (b.Index);
			});
			
			// Remove from the list all regions which start in an excluded region
			int pos=0;
			for (int n=0; n<excludeMatches.Count; n++) {
				Match m = excludeMatches [n];
				if (m.Index < pos) {
					excludeMatches.RemoveAt (n);
					n--;
				} else {
					pos = m.Index + m.Length;
				}
			}
			
			foreach (RegexInfo ri in regexes) {
				int lineNumber = 0;
				int oldIndex  = 0;
				foreach (Match match in ri.Regex.Matches (text)) {
					// Ignore matches inside excluded regions
					bool ignore = false;
					foreach (Match em in excludeMatches) {
						if (match.Index >= em.Index && match.Index < em.Index + em.Length) {
							ignore = true;
							LoggingService.LogDebug ("Excluded Gettext string '{0}' in file '{1}'", match.Groups[ri.ValueGroupIndex].Value, fileName);
							break;
						}
					}
					if (ignore)
						continue;
					
					string mt = match.Groups[ri.ValueGroupIndex].Value;
					if (mt.Length == 0)
						continue;
					
					foreach (TransformInfo ti in transforms)
						mt = ti.Regex.Replace (mt, ti.ReplaceText);
					
					try {
						mt = StringEscaping.UnEscape (ri.EscapeMode, mt);
					} catch (FormatException fex) {
						monitor.ReportWarning ("Error unescaping string '" + mt + "': " + fex.Message);
						continue;
					}
					
					if (mt.Trim().Length == 0)
						continue;
					
					//get the plural string if it's a plural form and apply transforms
					string pt = ri.PluralGroupIndex != -1 ? match.Groups[ri.PluralGroupIndex].Value : null;
					if (pt != null)
						foreach (TransformInfo ti in transforms)
							pt = ti.Regex.Replace (pt, ti.ReplaceText);
					
					//add to the catalog
					CatalogEntry entry = catalog.AddItem (mt, pt);
					lineNumber += GetLineCount (text, oldIndex, match.Index);
					oldIndex = match.Index;
					entry.AddReference (fileNamePrefix + lineNumber);
				}
			}
		}