]> git.lyx.org Git - features.git/commitdiff
* Introduce polyglossia support (bug 6576).
authorJürgen Spitzmüller <spitz@lyx.org>
Mon, 22 Nov 2010 12:10:16 +0000 (12:10 +0000)
committerJürgen Spitzmüller <spitz@lyx.org>
Mon, 22 Nov 2010 12:10:16 +0000 (12:10 +0000)
Please test this.

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@36433 a592a061-630c-0410-9148-cb99ea01b6c8

13 files changed:
lib/chkconfig.ltx
lib/doc/LaTeXConfig.lyx
src/Buffer.cpp
src/BufferParams.cpp
src/Font.cpp
src/LaTeXFeatures.cpp
src/LaTeXFeatures.h
src/OutputParams.cpp
src/OutputParams.h
src/Paragraph.cpp
src/ParagraphMetrics.cpp
src/insets/InsetQuotes.cpp
src/output_latex.cpp

index 87052e5496ed42f6cf5cfbd768027bccd26c1479..289c30a7d818157d83ddf936e213e0b8961b7e5b 100644 (file)
 \TestPackage{nicefrac}
 \TestPackage{nomencl}
 \TestPackage{pdfcolmk}
+\TestPackage{polyglossia}
 \TestPackage{pdfpages}
 \TestPackage{prettyref}
 \TestPackage{preview}
index c4838371126f28801783cb49451c3b1124fda66b..eace045a007ddd4233e0b426e1e0bcaa036211b8 100644 (file)
@@ -1,5 +1,5 @@
 #LyX 2.0.0svn created this file. For more info see http://www.lyx.org/
-\lyxformat 401
+\lyxformat 408
 \begin_document
 \begin_header
 \textclass article
@@ -37,6 +37,7 @@
 \use_indices false
 \paperorientation portrait
 \suppress_date false
+\use_refstyle 0
 \index Index
 \shortcut idx
 \color #008000
@@ -4276,6 +4277,48 @@ mongolian-babel
  Mongolian.
 \end_layout
 
+\begin_layout Subsection
+polyglossia
+\end_layout
+
+\begin_layout Description
+Found: 
+\begin_inset Info
+type  "package"
+arg   "polyglossia"
+\end_inset
+
+
+\end_layout
+
+\begin_layout Description
+CTAN: 
+\family typewriter
+macros/xetex/latex/polyglossia/
+\end_layout
+
+\begin_layout Description
+Notes: The package 
+\family sans
+polyglossia
+\family default
+ provides a language interface specifically for XeTeX.
+ It aims to supersede the 
+\family sans
+babel
+\family default
+ package by using XeTeX's mutlilingual and multiscript facilities.
+ If installed, 
+\family sans
+polyglossia
+\family default
+ will by used for language handling instead of 
+\family sans
+babel
+\family default
+ when XeTeX is used as output format.
+\end_layout
+
 \begin_layout Subsection
 turkmen
 \end_layout
index c7682d11ebba57a081850b3c538a00fed4938edf..b78363eee40f942b5e67add0b915390187e48228 100644 (file)
@@ -1386,6 +1386,7 @@ void Buffer::writeLaTeXSource(odocstream & os,
                MacroSet parentMacros;
                listParentMacros(parentMacros, features);
 
+               runparams.use_polyglossia = features.usePolyglossia();
                // Write the preamble
                runparams.use_babel = params().writeLaTeX(os, features,
                                                          d->texrow,
index e5c701ac9d7c9e18bd77b48a06e4961a96ceb3db..640f3544d6d0fb3a5c96225881295404eae6b5f5 100644 (file)
@@ -1305,15 +1305,16 @@ bool BufferParams::writeLaTeX(odocstream & os, LaTeXFeatures & features,
 
        ostringstream language_options;
        bool const use_babel = features.useBabel() && !tclass.provides("babel");
-       if (use_babel) {
+       bool const use_polyglossia = features.usePolyglossia();
+       bool const global = lyxrc.language_global_options;
+       if (use_babel || (use_polyglossia && global)) {
                language_options << features.getLanguages();
                if (!language->babel().empty()) {
                        if (!language_options.str().empty())
                                language_options << ',';
                        language_options << language->babel();
                }
-               if (lyxrc.language_global_options
-                   && !features.needBabelLangOptions())
+               if (global && !features.needBabelLangOptions())
                        clsoptions << language_options.str() << ',';
        }
 
@@ -1721,15 +1722,15 @@ bool BufferParams::writeLaTeX(odocstream & os, LaTeXFeatures & features,
        
        // If we use hyperref, jurabib, japanese, or vietnamese, we have to call babel before them.
        if (use_babel
-               && (features.isRequired("jurabib")
-                       || features.isRequired("hyperref")
-                       || features.isRequired("vietnamese")
-                       || features.isRequired("japanese") ) ) {
-                               // FIXME UNICODE
-                               lyxpreamble += from_utf8(features.getBabelPresettings());
-                               lyxpreamble += from_utf8(babelCall(language_options.str(),
-                                                                  features.needBabelLangOptions())) + '\n';
-                               lyxpreamble += from_utf8(features.getBabelPostsettings());
+           && (features.isRequired("jurabib")
+               || features.isRequired("hyperref")
+               || features.isRequired("vietnamese")
+               || features.isRequired("japanese"))) {
+                       // FIXME UNICODE
+                       lyxpreamble += from_utf8(features.getBabelPresettings());
+                       lyxpreamble += from_utf8(babelCall(language_options.str(),
+                                                          features.needBabelLangOptions())) + '\n';
+                       lyxpreamble += from_utf8(features.getBabelPostsettings());
        }
 
        // The optional packages;
@@ -1877,6 +1878,7 @@ bool BufferParams::writeLaTeX(odocstream & os, LaTeXFeatures & features,
                lyxpreamble += from_utf8(features.getBabelPostsettings());
        }
 
+       // FIXME Polyglossia?
        docstring const i18npreamble = features.getTClassI18nPreamble(use_babel);
        if (!i18npreamble.empty())
                lyxpreamble += i18npreamble + '\n';
@@ -1896,6 +1898,29 @@ bool BufferParams::writeLaTeX(odocstream & os, LaTeXFeatures & features,
                os << "\\usepackage{xltxtra}\n";
                texrow.newline();
        }
+       // Polyglossia must be loaded after xltxtra
+       if (use_polyglossia) {
+               // call the package
+               os << "\\usepackage{polyglossia}\n";
+               texrow.newline();
+               // set the main language
+               os << "\\setdefaultlanguage";
+               if (!language->polyglossiaOpts().empty())
+                       os << "[" << from_ascii(language->polyglossiaOpts()) << "]";
+               os << "{" + from_ascii(language->polyglossia()) + "}\n";
+               texrow.newline();
+               // now setup the other languages
+               std::map<std::string, std::string> const polylangs = 
+                       features.getPolyglossiaLanguages();
+               for (std::map<std::string, std::string>::const_iterator mit = polylangs.begin();
+                    mit != polylangs.end() ; ++mit) {
+                       os << "\\setotherlanguage";
+                       if (!mit->second.empty())
+                               os << "[" << from_ascii(mit->second) << "]";
+                       os << "{" << from_ascii(mit->first) << "}\n";
+                       texrow.newline();
+               }
+       }
        return use_babel;
 }
 
index cf8ea617855cc619cc8879948ac0487fb339796f..bf99b3e6582e5158719afdc5102f19d5818b0438 100644 (file)
@@ -272,7 +272,18 @@ int Font::latexWriteStartChanges(odocstream & os, BufferParams const & bparams,
        bool env = false;
 
        int count = 0;
-       if (language()->babel() != base.language()->babel() &&
+
+       // polyglossia or babel?
+       if (runparams.use_polyglossia) {
+               if (!language()->polyglossia().empty()) {
+                       string tmp = "\\text" + language()->polyglossia();
+                       if (!language()->polyglossiaOpts().empty())
+                               tmp += "[" + language()->polyglossiaOpts() + "]";
+                       tmp += "{";
+                       os << from_ascii(tmp);
+                       count += tmp.length();
+               }
+       } else if (language()->babel() != base.language()->babel() &&
            language() != prev.language()) {
                if (language()->lang() == "farsi") {
                        os << "\\textFR{";
index c6516995c9b4d0cbb9742e32f1db6e2680890e0c..530275860bd3afc9a14abf305c1b8f8ecb406179 100644 (file)
@@ -285,10 +285,23 @@ LaTeXFeatures::LaTeXFeatures(Buffer const & b, BufferParams const & p,
 
 bool LaTeXFeatures::useBabel() const
 {
-       return (lyxrc.language_package_selection != LyXRC::LP_NONE) &&
-               ((bufferParams().language->lang() != lyxrc.default_language &&
-                !bufferParams().language->babel().empty()) ||
-               this->hasLanguages());
+       return (lyxrc.language_package_selection != LyXRC::LP_NONE)
+               && !usePolyglossia()
+               && ((bufferParams().language->lang() != lyxrc.default_language
+                   && !bufferParams().language->babel().empty())
+                   || this->hasLanguages());
+}
+
+
+bool LaTeXFeatures::usePolyglossia() const
+{
+       return (lyxrc.language_package_selection == LyXRC::LP_AUTO)
+               && isRequired("xetex")
+               && isAvailable("polyglossia")
+               && ((bufferParams().language->lang() != lyxrc.default_language
+                    && !bufferParams().language->polyglossia().empty())
+                   || this->hasLanguages())
+               && this->hasPolyglossiaLanguages();
 }
 
 
@@ -474,6 +487,19 @@ bool LaTeXFeatures::hasLanguages() const
 }
 
 
+bool LaTeXFeatures::hasPolyglossiaLanguages() const
+{
+       LanguageList::const_iterator const begin = UsedLanguages_.begin();
+       for (LanguageList::const_iterator cit = begin;
+            cit != UsedLanguages_.end();
+            ++cit) {
+               if ((*cit)->polyglossia().empty())
+                       return false;
+       }
+       return true;
+}
+
+
 string LaTeXFeatures::getLanguages() const
 {
        ostringstream languages;
@@ -490,6 +516,20 @@ string LaTeXFeatures::getLanguages() const
 }
 
 
+std::map<std::string, std::string> LaTeXFeatures::getPolyglossiaLanguages() const
+{
+       std::map<std::string, std::string> languages;
+
+       LanguageList::const_iterator const begin = UsedLanguages_.begin();
+       for (LanguageList::const_iterator cit = begin;
+            cit != UsedLanguages_.end();
+            ++cit) {
+               languages[(*cit)->polyglossia()] = (*cit)->polyglossiaOpts();
+       }
+       return languages;
+}
+
+
 set<string> LaTeXFeatures::getEncodingSet(string const & doc_encoding) const
 {
        // This does only find encodings of languages supported by babel, but
@@ -825,7 +865,7 @@ docstring const LaTeXFeatures::getMacros() const
        if (mustProvide("lyxarrow"))
                macros << lyxarrow_def << '\n';
 
-       if (mustProvide("textgreek")) {
+       if (!usePolyglossia() && mustProvide("textgreek")) {
                // Avoid a LaTeX error if times fonts are used and the grtimes
                // package is installed but actual fonts are not (bug 6469).
                if (params_.fontsRoman == "times")
@@ -839,7 +879,7 @@ docstring const LaTeXFeatures::getMacros() const
                        macros << textgreek_def << '\n';
        }
 
-       if (mustProvide("textcyr"))
+       if (!usePolyglossia() && mustProvide("textcyr"))
                macros << textcyr_def << '\n';
 
        if (mustProvide("lyxmathsym"))
index 9bb50f57cfa6dc00485be95e19f369657f7c343c..999c1fb460ff50544178f86453efc462f88868f8 100644 (file)
@@ -101,9 +101,13 @@ public:
        void useLanguage(Language const *);
        ///
        bool hasLanguages() const;
+       /// check if all used languages are supported by polyglossia
+       bool hasPolyglossiaLanguages() const;
        ///
        std::string getLanguages() const;
        ///
+       std::map<std::string, std::string> getPolyglossiaLanguages() const;
+       ///
        std::set<std::string> getEncodingSet(std::string const & doc_encoding) const;
        ///
        void useLayout(docstring const & lyt);
@@ -117,6 +121,8 @@ public:
        BufferParams const & bufferParams() const;
        /// the return value is dependent upon both LyXRC and LaTeXFeatures.
        bool useBabel() const;
+       ///
+       bool usePolyglossia() const;
        /// are we in a float?
        bool inFloat() const { return in_float_; }
        /// are we in a float?
index 211a10c62926d770c3ff198bf6121807c496097d..b575a2915a2cffcc8eeba5a433147e71921727b0 100644 (file)
@@ -21,7 +21,7 @@ namespace lyx {
 OutputParams::OutputParams(Encoding const * enc)
        : flavor(LATEX), math_flavor(NotApplicable), nice(false), moving_arg(false), 
          inulemcmd(false), local_font(0), master_language(0), encoding(enc),
-         free_spacing(false), use_babel(false),
+         free_spacing(false), use_babel(false), use_polyglossia(false),
          use_indices(false), use_japanese(false), linelen(0), depth(0),
          exportdata(new ExportData),
          inComment(false), inTableCell(NO), inFloat(NONFLOAT),
index 9ae7c3721cdbee92ecb846eefc189a91a34f9be1..6653a41a0fd4b12d68ca2c051395317a9446c8e1 100644 (file)
@@ -136,6 +136,10 @@ public:
        */
        bool use_babel;
 
+       /** Do we use polyglossia (instead of babel)?
+       */
+       bool use_polyglossia;
+
        /** Are we generating multiple indices?
        */
        bool use_indices;
index 326c5c98138fab76f05522eaa70bac27ff11b5fd..19caf375469bbe1ead73ff99c67802b633558248 100644 (file)
@@ -2369,12 +2369,16 @@ void Paragraph::latex(BufferParams const & bparams,
                        open_font = false;
                }
 
+               string const running_lang = runparams.use_polyglossia ?
+                       running_font.language()->polyglossia() : running_font.language()->babel();
                // close babel's font environment before opening CJK.
-               if (!running_font.language()->babel().empty() &&
+               string const lang_end_command = runparams.use_polyglossia ?
+                       "\\end{$$lang}" : lyxrc.language_command_end;
+               if (!running_lang.empty() &&
                    font.language()->encoding()->package() == Encoding::CJK) {
-                               string end_tag = subst(lyxrc.language_command_end,
+                               string end_tag = subst(lang_end_command,
                                                        "$$lang",
-                                                       running_font.language()->babel());
+                                                       running_lang);
                                os << from_ascii(end_tag);
                                column += end_tag.length();
                }
index 0a90fb69252449976b930bce5b4a54d82ad0bf3b..a1a01a8c10cc71e94f1ebe37b66109eb73150228 100644 (file)
@@ -234,7 +234,7 @@ int ParagraphMetrics::singleWidth(pos_type pos, Font const & font) const
                                c = par_->transformChar(c, pos);
                } else if (language->lang() == "hebrew" &&
                                Encodings::isHebrewComposeChar(c)) {
-                       return 0;       
+                       return 0;
                }
        }
        return theFontMetrics(font).width(c);
index 95a71abc45ba499e26950e6cffb6c6d1687d432e..4e47361bf752910cdfca7968e7aa5f28b90daac4 100644 (file)
@@ -267,18 +267,20 @@ int InsetQuotes::latex(odocstream & os, OutputParams const & runparams) const
        string qstr;
 
        if (language_ == FrenchQuotes && times_ == DoubleQuotes
-           && prefixIs(runparams.local_font->language()->code(), "fr")) {
+           && prefixIs(runparams.local_font->language()->code(), "fr")
+           && !runparams.use_polyglossia) {
                if (side_ == LeftQuote)
                        qstr = "\\og "; //the spaces are important here
                else
                        qstr = " \\fg{}"; //and here
-       } else if (lyxrc.fontenc == "T1") {
+       } else if (lyxrc.fontenc == "T1" && !runparams.use_polyglossia) {
                qstr = latex_quote_t1[times_][quoteind];
 #ifdef DO_USE_DEFAULT_LANGUAGE
        } else if (doclang == "default") {
 #else
        } else if (!runparams.use_babel) {
 #endif
+               // these are also used by polyglossia
                qstr = latex_quote_ot1[times_][quoteind];
        } else {
                qstr = latex_quote_babel[times_][quoteind];
index ea4d035367b173ddb0498b291e3e3dba099a5f35..c52cff0878a2eb8e395c1fe192a3b803928ce3a5 100644 (file)
@@ -97,6 +97,16 @@ TeXDeeper(Buffer const & buf,
 }
 
 
+string const getPolyglossiaEnvName(Language const * lang)
+{
+       string result = lang->polyglossia();
+       if (result == "arabic")
+               // exceptional spelling; see polyglossia docs.
+               result = "Arabic";
+       return result;
+}
+
+
 ParagraphList::const_iterator
 TeXEnvironment(Buffer const & buf,
               Text const & text,
@@ -131,29 +141,47 @@ TeXEnvironment(Buffer const & buf,
                ? (use_prev_env_language ? prev_env_language_
                                         : priorpit->getParLanguage(bparams))
                : doc_language;
-       if (par_language->babel() != prev_par_language->babel()) {
+       string par_lang = par_language->babel();
+       string prev_par_lang = prev_par_language->babel();
+       string doc_lang = doc_language->babel();
+       string lang_begin_command = lyxrc.language_command_begin;
+       string lang_end_command = lyxrc.language_command_end;
+
+       if (runparams.use_polyglossia) {
+               par_lang = getPolyglossiaEnvName(par_language);
+               prev_par_lang = getPolyglossiaEnvName(prev_par_language);
+               doc_lang = getPolyglossiaEnvName(doc_language);
+               lang_begin_command = "\\begin{$$lang}";
+               lang_end_command = "\\end{$$lang}";
+       }
 
-               if (!lyxrc.language_command_end.empty() &&
-                   prev_par_language->babel() != doc_language->babel() &&
-                   !prev_par_language->babel().empty()) {
+       if (par_lang != prev_par_lang) {
+               if (!lang_end_command.empty() &&
+                   prev_par_lang != doc_lang &&
+                   !prev_par_lang.empty()) {
                        os << from_ascii(subst(
-                               lyxrc.language_command_end,
+                               lang_end_command,
                                "$$lang",
-                               prev_par_language->babel()))
-                          // the '%' is necessary to prevent unwanted whitespace
-                          << "%\n";
+                               prev_par_lang))
+                         // the '%' is necessary to prevent unwanted whitespace
+                         << "%\n";
                        texrow.newline();
                }
 
                if ((lyxrc.language_command_end.empty() ||
-                    par_language->babel() != doc_language->babel()) &&
-                   !par_language->babel().empty()) {
+                   par_lang != doc_lang) &&
+                   !par_lang.empty()) {
                        os << from_ascii(subst(
-                               lyxrc.language_command_begin,
+                               lang_begin_command,
                                "$$lang",
-                               par_language->babel()))
-                          // the '%' is necessary to prevent unwanted whitespace
-                          << "%\n";
+                               par_lang));
+                       if (runparams.use_polyglossia
+                           && !par_language->polyglossiaOpts().empty())
+                                       os << "["
+                                          << from_ascii(par_language->polyglossiaOpts())
+                                          << "]";
+                         // the '%' is necessary to prevent unwanted whitespace
+                       os << "%\n";
                        texrow.newline();
                }
        }
@@ -441,7 +469,22 @@ ParagraphList::const_iterator TeXOnePar(Buffer const & buf,
                                         : priorpit->getParLanguage(bparams))
                : outer_language;
 
-       if (par_language->babel() != prev_language->babel()
+       string par_lang = par_language->babel();
+       string prev_lang = prev_language->babel();
+       string doc_lang = doc_language->babel();
+       string outer_lang = outer_language->babel();
+       string lang_begin_command = lyxrc.language_command_begin;
+       string lang_end_command = lyxrc.language_command_end;
+
+       if (runparams.use_polyglossia) {
+               par_lang = getPolyglossiaEnvName(par_language);
+               prev_lang = getPolyglossiaEnvName(prev_language);
+               doc_lang = getPolyglossiaEnvName(doc_language);
+               outer_lang = getPolyglossiaEnvName(outer_language);
+               lang_begin_command = "\\begin{$$lang}";
+               lang_end_command = "\\end{$$lang}";
+       }
+       if (par_lang != prev_lang
            // check if we already put language command in TeXEnvironment()
            && !(style.isEnvironment()
                 && (pit == paragraphs.begin() ||
@@ -449,13 +492,13 @@ ParagraphList::const_iterator TeXOnePar(Buffer const & buf,
                      priorpit->getDepth() <= pit->getDepth())
                     || priorpit->getDepth() < pit->getDepth())))
        {
-               if (!lyxrc.language_command_end.empty() &&
-                   prev_language->babel() != outer_language->babel() &&
-                   !prev_language->babel().empty())
+               if (!lang_end_command.empty() &&
+                   prev_lang != outer_lang &&
+                   !prev_lang.empty())
                {
-                       os << from_ascii(subst(lyxrc.language_command_end,
+                       os << from_ascii(subst(lang_end_command,
                                "$$lang",
-                               prev_language->babel()))
+                               prev_lang))
                           // the '%' is necessary to prevent unwanted whitespace
                           << "%\n";
                        texrow.newline();
@@ -466,29 +509,30 @@ ParagraphList::const_iterator TeXOnePar(Buffer const & buf,
                // the previous one, if the current language is different than the
                // outer_language (which is currently in effect once the previous one
                // is closed).
-               if ((lyxrc.language_command_end.empty() ||
-                    par_language->babel() != outer_language->babel()) &&
-                   !par_language->babel().empty()) {
+               if ((lang_end_command.empty() ||
+                    par_lang != outer_lang) &&
+                   !par_lang.empty()) {
                        // If we're inside an inset, and that inset is within an \L or \R
                        // (or equivalents), then within the inset, too, any opposite
                        // language paragraph should appear within an \L or \R (in addition
                        // to, outside of, the normal language switch commands).
                        // This behavior is not correct for ArabTeX, though.
-                       if (    // not for ArabTeX
-                                       (par_language->lang() != "arabic_arabtex" &&
-                                        outer_language->lang() != "arabic_arabtex") &&
-                                       // are we in an inset?
-                                       runparams.local_font != 0 &&
-                                       // is the inset within an \L or \R?
-                                       //
-                                       // FIXME: currently, we don't check this; this means that
-                                       // we'll have unnnecessary \L and \R commands, but that
-                                       // doesn't seem to hurt (though latex will complain)
-                                       //
-                                       // is this paragraph in the opposite direction?
-                                       runparams.local_font->isRightToLeft() !=
-                                               par_language->rightToLeft()
-                               ) {
+                       if (!runparams.use_polyglossia &&
+                           // not for ArabTeX
+                           (par_language->lang() != "arabic_arabtex" &&
+                            outer_language->lang() != "arabic_arabtex") &&
+                           // are we in an inset?
+                           runparams.local_font != 0 &&
+                           // is the inset within an \L or \R?
+                           //
+                           // FIXME: currently, we don't check this; this means that
+                           // we'll have unnnecessary \L and \R commands, but that
+                           // doesn't seem to hurt (though latex will complain)
+                           //
+                           // is this paragraph in the opposite direction?
+                           runparams.local_font->isRightToLeft() !=
+                                 par_language->rightToLeft()
+                           ) {
                                // FIXME: I don't have a working copy of the Arabi package, so
                                // I'm not sure if the farsi and arabic_arabi stuff is correct
                                // or not...
@@ -509,11 +553,16 @@ ParagraphList::const_iterator TeXOnePar(Buffer const & buf,
                        // With CJK, the CJK tag has to be closed first (see below)
                        if (runparams.encoding->package() != Encoding::CJK) {
                                os << from_ascii(subst(
-                                       lyxrc.language_command_begin,
+                                       lang_begin_command,
                                        "$$lang",
-                                       par_language->babel()))
+                                       par_lang));
+                               if (runparams.use_polyglossia
+                                   && !par_language->polyglossiaOpts().empty())
+                                               os << "["
+                                                 << from_ascii(par_language->polyglossiaOpts())
+                                                 << "]";
                                   // the '%' is necessary to prevent unwanted whitespace
-                                  << "%\n";
+                               os << "%\n";
                                texrow.newline();
                        }
                }
@@ -566,9 +615,9 @@ ParagraphList::const_iterator TeXOnePar(Buffer const & buf,
                                        // With CJK, the CJK tag had to be closed first (see above)
                                        if (runparams.encoding->package() == Encoding::CJK) {
                                                os << from_ascii(subst(
-                                                       lyxrc.language_command_begin,
+                                                       lang_begin_command,
                                                        "$$lang",
-                                                       par_language->babel()))
+                                                       par_lang))
                                                // the '%' is necessary to prevent unwanted whitespace
                                                << "%\n";
                                                texrow.newline();
@@ -709,6 +758,7 @@ ParagraphList::const_iterator TeXOnePar(Buffer const & buf,
        // needed if we're within an \L or \R that we may have opened above (not
        // necessarily in this paragraph) and are about to close.
        bool closing_rtl_ltr_environment =
+               !runparams.use_polyglossia &&
                // not for ArabTeX
                (par_language->lang() != "arabic_arabtex" &&
                 outer_language->lang() != "arabic_arabtex") &&
@@ -734,7 +784,7 @@ ParagraphList::const_iterator TeXOnePar(Buffer const & buf,
                }
                // when the paragraph uses CJK, the language has to be closed earlier
                if (font.language()->encoding()->package() != Encoding::CJK) {
-                       if (lyxrc.language_command_end.empty()) {
+                       if (lang_end_command.empty()) {
                                // If this is a child, we should restore the
                                // master language after the last paragraph.
                                Language const * const current_language =
@@ -742,18 +792,21 @@ ParagraphList::const_iterator TeXOnePar(Buffer const & buf,
                                        && runparams.master_language)
                                                ? runparams.master_language
                                                : outer_language;
-                               if (!current_language->babel().empty()) {
+                               string const current_lang = runparams.use_polyglossia ?
+                                       getPolyglossiaEnvName(current_language)
+                                       : current_language->babel();
+                               if (!current_lang.empty()) {
                                        os << from_ascii(subst(
-                                               lyxrc.language_command_begin,
+                                               lang_begin_command,
                                                "$$lang",
-                                               current_language->babel()));
+                                               current_lang));
                                        pending_newline = true;
                                }
-                       } else if (!par_language->babel().empty()) {
+                       } else if (!par_lang.empty()) {
                                os << from_ascii(subst(
-                                       lyxrc.language_command_end,
+                                       lang_end_command,
                                        "$$lang",
-                                       par_language->babel()));
+                                       par_lang));
                                pending_newline = true;
                        }
                }
@@ -895,14 +948,25 @@ void latexParagraphs(Buffer const & buf,
        }
        // if "auto begin" is switched off, explicitly switch the
        // language on at start
+       string const mainlang = runparams.use_polyglossia ?
+               getPolyglossiaEnvName(bparams.language)
+               : bparams.language->babel();
+       string const lang_begin_command = runparams.use_polyglossia ?
+               "\\begin{$$lang}" : lyxrc.language_command_begin;
+
        if (maintext && !lyxrc.language_auto_begin &&
-           !bparams.language->babel().empty()) {
+           !mainlang.empty()) {
                // FIXME UNICODE
-               os << from_utf8(subst(lyxrc.language_command_begin,
+               os << from_utf8(subst(lang_begin_command,
                                        "$$lang",
-                                       bparams.language->babel()))
-                       << '\n';
-       texrow.newline();
+                                       mainlang));
+               if (runparams.use_polyglossia
+                   && !bparams.language->polyglossiaOpts().empty())
+                       os << "["
+                           << from_ascii(bparams.language->polyglossiaOpts())
+                           << "]";
+               os << '\n';
+               texrow.newline();
        }
 
        ParagraphList::const_iterator lastpar;
@@ -969,11 +1033,13 @@ void latexParagraphs(Buffer const & buf,
 
        // if "auto end" is switched off, explicitly close the language at the end
        // but only if the last par is in a babel language
-       if (maintext && !lyxrc.language_auto_end && !bparams.language->babel().empty() &&
+       string const lang_end_command = runparams.use_polyglossia ?
+               "\\end{$$lang}" : lyxrc.language_command_end;
+       if (maintext && !lyxrc.language_auto_end && !mainlang.empty() &&
                lastpar->getParLanguage(bparams)->encoding()->package() != Encoding::CJK) {
-               os << from_utf8(subst(lyxrc.language_command_end,
+               os << from_utf8(subst(lang_end_command,
                                        "$$lang",
-                                       bparams.language->babel()))
+                                       mainlang))
                        << '\n';
                texrow.newline();
        }