http://bugzilla.lyx.org/show_bug.cgi?id=3203:
>Yes, LyX treats all text files as utf8, and iconv can choke on non-utf8 text
>interpreted as utf8. I see the same here.
>Since it is not possible to detect the file encoding reliably from the contents
>the only option I see to make this work is to let the user give the encoding
>(e. g. with a combox in the import dialog). The default could be the locale
>encoding or utf8.
Here an idea to fix this bug for most cases: When the UTF-8 import fails
we try to read the file as local-8bit encoded. When this also fails a
message box pops up:
The file is neither UTF-8 nor local-8Bit encoded.
To import the file please change its encoding.
Is from_local8bit the right choice?
Instead of showing the message we could later open an dialog to choose the
encoding. But therefore we need a new dialog and a new conversion function
docstring const from_encoding(std::string const & s);
Or how is the conversion from an arbitrary encoding done?
Peter
Index: src/callback.cpp
===================================================================
--- src/callback.cpp (revision 18205)
+++ src/callback.cpp (working copy)
@@ -398,8 +398,19 @@
copy(ii, end, back_inserter(tmpstr));
#endif
- // FIXME UNICODE: We don't know the encoding of the file
- return normalize_kc(from_utf8(tmpstr));
+ docstring file_content = from_utf8(tmpstr);
+ if (file_content.empty()) {
+ LYXERR(Debug::INFO) << "Could not read file as UTF-8 encoded
file, trying to read it with locale encoding" << endl;
+ file_content = from_local8bit(tmpstr);
+ if (!file_content.empty()) {
+ LYXERR(Debug::INFO) << "File readed as locale-8Bit
encoded file" << endl;
+ } else {
+ LYXERR(Debug::INFO) << "Could also not read file as
locale-8Bit encoded file" << endl;
+ Alert::error(_("Could not read file"),
+ _("The file is neither UTF-8
nor local-8Bit encoded.\nTo import the file please change its encoding."));
+ }
+ }
+ return normalize_kc(file_content);
}