[Tex/LaTex] Custom hyphenation for different languages in XeTeX

hyphenationlanguagespolyglossiaxetex

When I define custom hyphenation with \hyphenation in XeTeX, it seems to only work for the main body of the text, but not for the bits inside \text<language> (as in \textgerman) environments.

I know I can use \- but I would rather have a global solution.

A minimal example:

\documentclass[draft]{article}
\usepackage{xltxtra}
\usepackage{polyglossia}
\setmainlanguage[variant=british]{english}
\setotherlanguage{german}

\begin{document}

\hyphenation{bb-bbb}

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa bbbbb

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \textgerman{bbbbb}

\end{document}

yields this:

\hyphenation failing with \textgerman

Best Answer

The method with hyphenrules indeed doesn't work with Polyglossia (and it should be investigated why). You can do in another way:

\newcommand{\sethyphenation}[3][]{%
  \sbox0{\begin{otherlanguage}[#1]{#2}
    \hyphenation{#3}\end{otherlanguage}}}

\sethyphenation{german}{bb-bbbb}
\sethyphenation[variant=british]{english}{bbb-bbb}

The following document will show different hyphenations of bbbbbb

\documentclass[draft]{article}
\usepackage{fontspec}
\usepackage{polyglossia}
\setmainlanguage[variant=british]{english}
\setotherlanguage{german}

\newcommand{\sethyphenation}[3][]{%
  \sbox0{\begin{otherlanguage}[#1]{#2}
    \hyphenation{#3}\end{otherlanguage}}}

\sethyphenation{german}{bb-bbbb}
\sethyphenation[variant=british]{english}{bbb-bbb}

\begin{document}

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbb

\textgerman{aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa  bbbbbb}

\end{document}

The syntax of \sethyphenation is

\sethyphenation[<options>]{<language>}{<list of words separated by spaces>}
Related Question