+ if (!current_lang.empty()
+ && current_lang != openPolyglossiaLang(state)) {
+ string bc = use_polyglossia ?
+ getPolyglossiaBegin(lang_begin_command, current_lang,
+ current_language->polyglossiaOpts())
+ : subst(lang_begin_command, "$$lang", current_lang);
+ os << bc;
+ pending_newline = !localswitch;
+ unskip_newline = !localswitch;
+ if (use_polyglossia)
+ pushPolyglossiaLang(current_lang, localswitch);
+ }
+ } else if (!par_lang.empty()) {
+ // If we are in an environment, we have to close the "outer" language afterwards
+ string const & pol_lang = openPolyglossiaLang(state);
+ if (!style.isEnvironment()
+ || (close_lang_switch
+ && atSameLastLangSwitchDepth(state)
+ && par_lang != outer_lang
+ && (par_lang != pol_lang
+ || (pol_lang != outer_lang
+ && nextpar
+ && style != nextpar->layout())))
+ || (atSameLastLangSwitchDepth(state)
+ && state->lang_switch_depth_.size()
+ && pol_lang != par_lang))
+ {
+ if (use_polyglossia && !localswitch)
+ os << breakln;