Permalink
Browse files

Format saved files according to default gettext style.

As a stop-gap measure for the infamous bug #25, run saved PO files
through msgcat to format them in the exact same way msgmerge would by
default. This makes the problem much less serious, as Poedit will now do
the right thing for most people. It still doesn't respect less usual
formatting settings (--indent, --width, --no-wrap) of existing files.

Fixes #348. Also see #25.
  • Loading branch information...
1 parent 3b7d54a commit e7bce76a1b50dcd3af56993a745cf373b01aa715 @vslavik committed May 6, 2010
Showing with 35 additions and 11 deletions.
  1. +3 −1 NEWS
  2. +32 −10 src/catalog.cpp
View
4 NEWS
@@ -26,7 +26,9 @@ Version 1.5 (not yet released)
- improved source files viewer (#346)
- included outdated documentation was replaced with online wiki docs (#343)
- more keyboard navigation shortcuts
-- saving PO files no longer reformats source code references (#323)
+- saving PO files no longer reformats source code references (#323);
+ moreover, they are always formatted according to the default style
+ used by GNU gettext tools (#348)
Version 1.4.6
View
@@ -1334,15 +1334,12 @@ bool Catalog::Save(const wxString& po_file, bool save_mo)
}
m_header.Charset = charset;
+ const wxString po_file_temp = po_file + _T(".temp.po");
+ if ( wxFileExists(po_file_temp) )
+ wxRemoveFile(po_file_temp);
wxTextFile f;
- if (!wxFileExists(po_file) || !f.Open(po_file, wxConvISO8859_1))
- if (!f.Create(po_file))
- return false;
- for (int j = f.GetLineCount() - 1; j >= 0; j--)
- f.RemoveLine(j);
-
-
- wxCSConv encConv(charset);
+ if (!f.Create(po_file_temp))
+ return false;
SaveMultiLines(f, m_header.Comment);
f.AddLine(_T("msgid \"\""));
@@ -1418,10 +1415,35 @@ bool Catalog::Save(const wxString& po_file, bool save_mo)
f.AddLine(deletedItem.GetDeletedLines()[j]);
}
-
- f.Write(crlf, encConv);
+ f.Write(crlf, wxCSConv(charset));
f.Close();
+ // Now that the file was written, run msgcat to re-format it according
+ // to the usual format. This is a good enough fix for #25 until proper
+ // preservation of formatting is implemented.
+ if ( ExecuteGettext
+ (
+ wxString::Format(_T("msgcat --force-po -o \"%s\" \"%s\""),
+ po_file.c_str(),
+ po_file_temp.c_str())
+ )
+ && wxFileExists(po_file)
+ )
+ {
+ wxRemoveFile(po_file_temp);
+ }
+ else
+ {
+ if ( !wxRemoveFile(po_file) || !wxRenameFile(po_file_temp, po_file) )
+ {
+ wxLogWarning(_("Couldn't save file %s."), po_file.c_str());
+ }
+ else
+ {
+ wxLogWarning(_("There was a problem formatting the file nicely (but it was saved all right)."));
+ }
+ }
+
/* Poedit < 1.3.0 used to save additional info in .po.poedit file. It's
not used anymore, so delete the file if it exists: */
if (wxFileExists(po_file + _T(".poedit")))

0 comments on commit e7bce76

Please sign in to comment.