# Custom hyphenation for different languages in XeTeX

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:

-
Maybe some of the solutions listed here also work with XeTeX: tex.stackexchange.com/questions/37934/… –  lockstep Mar 6 '12 at 22:55
They surely work. –  egreg Mar 6 '12 at 23:33
I might have done something wrong but I can't get either to work. They seem to depend on babel, while I want to use polyglossia. –  Kamil S. Mar 7 '12 at 9:21

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>}

-
Fantastic, thanks a lot! Would you mind explaining the purpose of \sbox0, please? I can't see a different behaviour without it. –  Kamil S. Mar 7 '12 at 21:30
@KamilS. It's just to ensure that nothing is typeset. I'll ask Arthur Reutenauer to include this facility in Polyglossia. –  egreg Mar 7 '12 at 21:35
That would be great. I'm a linguist and often mix many languages inside one text, so hyphenating them all manually was somewhat tedious. Thanks for the explanation. –  Kamil S. Mar 7 '12 at 21:47