/// <summary> /// Loads catalog from .po file. /// </summary> public bool Load(IProgressMonitor monitor, string poFile) { Clear(); isOk = false; fileName = poFile; // Load the .po file: try { CharsetInfoFinder charsetFinder = new CharsetInfoFinder(parentProj, poFile); charsetFinder.Parse(); Charset = charsetFinder.Charset; originalNewLine = charsetFinder.NewLine; } catch (Exception e) { string msg = "Error during getting charset of file '" + poFile + "'."; LoggingService.LogFatalError(msg, e); if (monitor != null) { monitor.ReportError(msg, e); } return(false); } LoadParser parser = new LoadParser(this, poFile, Catalog.GetEncoding(this.Charset)); if (!parser.Parse()) { string msg = string.Format("Error during parsing '{0}' file, file is probably corrupted.", poFile); LoggingService.LogError(msg); if (monitor != null) { monitor.ReportError(msg, null); } return(false); } isOk = true; IsDirty = false; return(true); }
/// <summary> /// Loads catalog from .po file. /// </summary> public bool Load(IProgressMonitor monitor, string poFile) { Clear(); isOk = false; fileName = poFile; // Load the .po file: bool finished = false; try { CharsetInfoFinder charsetFinder = new CharsetInfoFinder(parentProj, poFile); charsetFinder.Parse(); Charset = charsetFinder.Charset; originalNewLine = charsetFinder.NewLine; finished = true; } catch (Exception e) { if (monitor != null) { monitor.ReportError("Error during getting charset of file '" + poFile + "'.", e); } } if (!finished) { return(false); } LoadParser parser = new LoadParser(this, poFile, Catalog.GetEncoding(this.Charset)); if (!parser.Parse()) { // TODO: use loging - GUI! Console.WriteLine("Error during parsing '{0}' file, file is probably corrupted.", poFile); return(false); } isOk = true; IsDirty = false; return(true); }
// Saves catalog to file. //FIXME: escape all the values that the parser unescapes //FIXME: use a StreamWriter instead of a StringBuilder public bool Save(string poFile) { StringBuilder sb = new StringBuilder(); // Update information about last modification time: RevisionDate = Catalog.GetDateTimeRfc822Format(); // Save .po file if (String.IsNullOrEmpty(Charset) || Charset == "CHARSET") { Charset = "utf-8"; } if (!CanEncodeToCharset(Charset)) { // TODO: log that we don't support such encoding, utf-8 would be used Charset = "utf-8"; } Encoding encoding = Catalog.GetEncoding(Charset); if (!String.IsNullOrEmpty(Comment)) { Catalog.SaveMultiLines(sb, Comment, originalNewLine); } sb.AppendFormat("msgid \"\"{0}", originalNewLine); sb.AppendFormat("msgstr \"\"{0}", originalNewLine); string pohdr = GetHeaderString(originalNewLine); pohdr = pohdr.Substring(0, pohdr.Length - 1); Catalog.SaveMultiLines(sb, pohdr, originalNewLine); sb.Append(originalNewLine); foreach (CatalogEntry data in entriesList) { if (data.Comment != String.Empty) { SaveMultiLines(sb, data.Comment, originalNewLine); } foreach (string autoComment in data.AutoComments) { if (String.IsNullOrEmpty(autoComment)) { sb.AppendFormat("#.{0}", originalNewLine); } else { sb.AppendFormat("#. {0}{1}", autoComment, originalNewLine); } } foreach (string reference in data.References) { //store paths as Unix-type paths, but internally use native style string r = reference; if (Platform.IsWindows) { r = r.Replace('\\', '/'); } sb.AppendFormat("#: {0}{1}", r, originalNewLine); } if (!String.IsNullOrEmpty(data.Flags)) { sb.Append(data.Flags); sb.Append(originalNewLine); } FormatMessageForFile(sb, "msgid", data.String, originalNewLine); if (data.HasPlural) { FormatMessageForFile(sb, "msgid_plural", data.PluralString, originalNewLine); for (int n = 0; n < data.NumberOfTranslations; n++) { string hdr = String.Format("msgstr[{0}]", n); FormatMessageForFile(sb, hdr, EnsureCorrectEndings(data.String, data.GetTranslation(n)), originalNewLine); } } else { FormatMessageForFile(sb, "msgstr", EnsureCorrectEndings(data.String, data.GetTranslation(0)), originalNewLine); } sb.Append(originalNewLine); } // Write back deleted items in the file so that they're not lost foreach (CatalogDeletedEntry deletedItem in deletedEntriesList) { if (deletedItem.Comment != String.Empty) { SaveMultiLines(sb, deletedItem.Comment, originalNewLine); } foreach (string autoComment in deletedItem.AutoComments) { sb.AppendFormat("#. {0}{1}", autoComment, originalNewLine); } foreach (string reference in deletedItem.References) { sb.AppendFormat("#: {0}{1}", reference, originalNewLine); } string flags = deletedItem.Flags; if (!String.IsNullOrEmpty(flags)) { sb.Append(flags); sb.Append(originalNewLine); } foreach (string deletedLine in deletedItem.DeletedLines) { sb.AppendFormat("{0}{1}", deletedLine, originalNewLine); } sb.Append(originalNewLine); } bool saved = false; try { //FIXME: use a safe write, i.e. write to another file and move over the original one // Write it as bytes, text writer includes BOF for utf-8, // gettext utils are refusing to work with this byte[] content = encoding.GetBytes(sb.ToString()); File.WriteAllBytes(poFile, content); saved = true; } catch (Exception ex) { LoggingService.LogError("Unhandled error saving Gettext Catalog '{0}': {1}", fileName, ex); } if (!saved) { return(false); } fileName = poFile; IsDirty = false; return(true); }