+
+
+docstring const i18npreamble(docstring const & templ, Language const * lang,
+ Encoding const & enc, bool const polyglossia,
+ bool const need_fixedwidth)
+{
+ if (templ.empty())
+ return templ;
+
+ string preamble = polyglossia ?
+ subst(to_utf8(templ), "$$lang", lang->polyglossia()) :
+ subst(to_utf8(templ), "$$lang", lang->babel());
+
+ string const langenc = lang->encoding()->iconvName();
+ string const texenc = lang->encoding()->latexName();
+ string const bufenc = enc.iconvName();
+ Encoding const * testenc(&enc);
+ bool lang_fallback = false;
+ bool ascii_fallback = false;
+ if (need_fixedwidth && !enc.hasFixedWidth()) {
+ if (lang->encoding()->hasFixedWidth()) {
+ testenc = lang->encoding();
+ lang_fallback = true;
+ } else {
+ // We need a fixed width encoding, but both the buffer
+ // encoding and the language encoding are variable
+ // width. As a last fallback, try to convert to pure
+ // ASCII using the LaTeX commands defined in unicodesymbols.
+ testenc = encodings.fromLyXName("ascii");
+ if (!testenc)
+ return docstring();
+ ascii_fallback = true;
+ }
+ }
+ // First and second character of plane 15 (Private Use Area)
+ string const s1 = "\xf3\xb0\x80\x80"; // U+F0000
+ string const s2 = "\xf3\xb0\x80\x81"; // U+F0001
+ // FIXME UNICODE
+ // lyx::regex is not unicode-safe.
+ // Should use QRegExp or (boost::u32regex, but that requires ICU)
+ static regex const reg("_\\(([^\\)]+)\\)");
+ smatch sub;
+ while (regex_search(preamble, sub, reg)) {
+ string const key = sub.str(1);
+ docstring const name = lang->translateLayout(key);
+ // Check whether name can be encoded in the buffer encoding
+ bool encodable = true;
+ for (size_t i = 0; i < name.size() && encodable; ++i)
+ if (!testenc->encodable(name[i]))
+ encodable = false;
+ string translated;
+ if (encodable && !lang_fallback)
+ translated = to_utf8(name);
+ else if (ascii_fallback)
+ translated = to_ascii(testenc->latexString(name).first);
+ else
+ translated = "\\inputencoding{" + texenc + "}"
+ + s1 + langenc + s2 + to_utf8(name)
+ + s1 + bufenc + s2;
+ preamble = subst(preamble, sub.str(), translated);
+ }
+ return from_utf8(preamble);
+}
+