- (pit != paragraphs.begin())
- ? (use_prev_env_language ? prev_env_language_
- : priorpit->getParLanguage(bparams))
- : outer_language;
-
- if (par_language->babel() != prev_language->babel()
- // check if we already put language command in TeXEnvironment()
- && !(style.isEnvironment()
- && (pit == paragraphs.begin() ||
- (priorpit->layout() != pit->layout() &&
- priorpit->getDepth() <= pit->getDepth())
- || priorpit->getDepth() < pit->getDepth())))
- {
- if (!lyxrc.language_command_end.empty() &&
- prev_language->babel() != outer_language->babel() &&
- !prev_language->babel().empty())
- {
- os << from_ascii(subst(lyxrc.language_command_end,
- "$$lang",
- prev_language->babel()))
- // the '%' is necessary to prevent unwanted whitespace
- << "%\n";
- texrow.newline();
+ runparams_in.for_search ?
+ languages.getLanguage("ignore")
+ :(priorpar && !priorpar->isPassThru())
+ ? (use_prev_env_language ? state->prev_env_language_
+ : priorpar->getParLanguage(bparams))
+ : outer_language;
+
+ bool const use_polyglossia = runparams.use_polyglossia;
+ string const par_lang = use_polyglossia ?
+ getPolyglossiaEnvName(par_language): par_language->babel();
+ string const prev_lang = use_polyglossia ?
+ getPolyglossiaEnvName(prev_language) : prev_language->babel();
+ string const outer_lang = use_polyglossia ?
+ getPolyglossiaEnvName(outer_language) : outer_language->babel();
+ string const nextpar_lang = nextpar_language ? (use_polyglossia ?
+ getPolyglossiaEnvName(nextpar_language) :
+ nextpar_language->babel()) : string();
+ string lang_begin_command = use_polyglossia ?
+ "\\begin{$$lang}$$opts" : lyxrc.language_command_begin;
+ string lang_end_command = use_polyglossia ?
+ "\\end{$$lang}" : lyxrc.language_command_end;
+ // the '%' is necessary to prevent unwanted whitespace
+ string lang_command_termination = "%\n";
+ bool const using_begin_end = use_polyglossia ||
+ !lang_end_command.empty();
+
+ // For InTitle commands, we need to switch the language inside the command
+ // (see #10849); thus open the command here.
+ if (intitle_command) {
+ parStartCommand(par, os, runparams, style);
+ if (style.isCommand() && style.needprotect)
+ // Due to the moving argument, some fragile
+ // commands (labels, index entries)
+ // are output after this command (#2154)
+ runparams.postpone_fragile_stuff = true;
+ os << '{';
+ }
+
+ // In some insets (such as Arguments), we cannot use \selectlanguage.
+ // Also, if an RTL language is set via environment in polyglossia,
+ // only a nested \\text<lang> command will reset the direction for LTR
+ // languages (see # 10111).
+ bool const in_polyglossia_rtl_env =
+ use_polyglossia
+ && runparams.local_font != nullptr
+ && outer_language->rightToLeft()
+ && !par_language->rightToLeft();
+ bool const localswitch = runparams_in.for_search
+ || text.inset().forceLocalFontSwitch()
+ || (using_begin_end && text.inset().forcePlainLayout())
+ || in_polyglossia_rtl_env;
+ if (localswitch) {
+ lang_begin_command = use_polyglossia ?
+ "\\text$$lang$$opts{" : lyxrc.language_command_local;
+ lang_end_command = "}";
+ lang_command_termination.clear();
+ }
+
+ bool const localswitch_needed = localswitch && par_lang != outer_lang;
+
+ // localswitches need to be closed and reopened at each par
+ if (runparams_in.for_search || ((par_lang != prev_lang || localswitch_needed)
+ // check if we already put language command in TeXEnvironment()
+ && !(style.isEnvironment()
+ && (pit == 0 || (priorpar->layout() != par.layout()
+ && priorpar->getDepth() <= par.getDepth())
+ || priorpar->getDepth() < par.getDepth())))) {
+ if (!localswitch
+ && (!using_begin_end || langOpenedAtThisLevel(state))
+ && !lang_end_command.empty()
+ && prev_lang != outer_lang
+ && !prev_lang.empty()
+ && (!using_begin_end || !style.isEnvironment())) {
+ os << from_ascii(subst(lang_end_command,
+ "$$lang",
+ prev_lang))
+ << lang_command_termination;
+ if (using_begin_end)
+ popLanguageName();