# Automatically Detect Foreign Language for Hyphenation?

Is there a LaTeX package that automatically detects which foreign language a particular part of a multi-language TeX document is so that it can hyphenate it appropriately, or do I have to manually specify the foreign language, using, e.g., the Babel package's command \foreignlanguage{}?

• \foreignlanguage is usually used for up to a couple of words, maybe a sentence. For whole passages I'd use \selectlanguage. Cause you talking about language detection, this usually only makes sense for longer stretches of text in a particular language, not for the odd foreign word (I mean after all, is "die" English or German?). And no, I don't think such a package exists, nor would it vibe well with LaTeX since it's too unclear what it will actually do. Update the dictionary and suddenly the whole text is recognized as a different language? That's not something I'd be comfortable with. Apr 10, 2014 at 20:10
• Yes, the auto-detection would have to apply to whole sentences or paragraphs, not single words. Apr 10, 2014 at 20:44
• Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. Oct 24, 2016 at 19:05

No, but there are things you can do that makes it less of a burden. It's not that common that a document switches language just like that, since that can be confusing not only for LaTeX, but for readers as well. Often the foreign text is divided from the text around it in some way that you have markup for anyway. Then let that markup do the language switch for you as well!

One simple example is italicized foreign words. Say you are writing in English about something Swedish and therefore use some foreign words in your text.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[swedish,american]{babel}
\newcommand\swe[1]{\foreignlanguage{swedish}{\emph{#1}}}

\begin{document}
When celebrating \swe{Valborgsmässoafton} (Walpurgis Night) the Swedes
\dots
\end{document}


The point is that you would have needed markup for those words anyway. Another common example is quotes in a foreign language. The package csquotes has some readymade commands for that. You can with advantage use these in your own shorter macros that are for the languages you happen to use, like \swe above.