# How can I retrieve (and conditionally act on) the language of my document?

I have a collection of documents that share a common style file that has all the functions and templating shared by this particular collection. The majority of these are all in the same language, but o handful of them are actually translations of the others. I would like to keep using the same style include file, but a few tidbits (for example in the footer and title sheet) need to be adjusted per-language.

I'm using polyglossia1, and each document has its own language declaration using \setmainlanguage{} (in my case, mostly to Turkish). Is there some way to retrieve what language has been set and conditional output different strings based on which language is the document default?

1 Actually for some reason my style file also has \usepackage[turkish]{babel}, which seems to be should be redundant and potentially a conflict with polyglossia; maybe I should sort out why that got in there while I'm messing with this!

• Both babel and polyglossia have \languagename which holds the currently active language name. And the translations package offers \ifcurrentbaselanguage{<lang>}{<true>}{<false>} for tests Mar 15, 2015 at 11:04

polyglossia stores the main language name in the macro \xpg@main@language, so it's easy to achieve what you want.

Example:

\documentclass{article}

\usepackage{xstring}

\usepackage{fontspec}

\usepackage{polyglossia}
\setmainlanguage{turkish}

\makeatletter
\newcommand{\mainlanguage}{\xpg@main@language}
\makeatother

\begin{document}

This document is written in \mainlanguage.

\IfStrEq{\mainlanguage}{turkish}{Yes, it's Turkish!}{No, it's not Turkish!}

\end{document}


Output:

As a side note, don't use babel if you're using polyglossia already...

• In modern times, you can safely use babel with the new engines. But using babel and polyglossia is bad indeed. Mar 15, 2015 at 10:34
• Both babel and polyglossia have \languagename which holds the currently active language name. No need for \xpg@main@language Mar 15, 2015 at 11:05
• @clemens as you say, \languagename holds the "currently active" language, not the "main" language as requested by the OP... Mar 15, 2015 at 11:13
• But the OP says every of his documents has only one language so I don't see the difference here (although, I'm not sure \languagename can be trusted before begin document...) Mar 15, 2015 at 11:28
• @clemens Actually it's pretty useful to know both options because my documents are not exclusively one language are the other, both directions have bits from other languages. Mar 15, 2015 at 14:53