[Tex/LaTex] Set hyphenation with polyglossia

babelhyphenationpolyglossia

If babel doesn't know how to hyphenate a word, I can inform it of good line breaks with \babelhyphenation:

\documentclass{article}
\usepackage[nynorsk]{babel}
    \babelhyphenation[nynorsk]{fram-halds-skulen}
\begin{document}
framhaldsskulen
\end{document}

But how do I do this with polyglossia? The documentation doesn't say anything about it. \babelhyphenation obviously doesn't work:

\documentclass{article}
\usepackage{polyglossia}
    \setdefaultlanguage{nynorsk}
    \babelhyphenation[nynorsk]{fram-halds-skulen}
\begin{document}
framhaldsskulen
\end{document}

! Undefined control sequence.
l.4 ^^I\babelhyphenation
                        [nynorsk]{fram-halds-skulen}
? 

Best Answer

As usual, in the example the lines are overfull on purpose, so as to force hyphenation.

\documentclass{article}

\usepackage{polyglossia}
\setmainlanguage{nynorsk}

\begin{hyphenrules}{nynorsk}
\hyphenation{fram-halds-skulen}
\end{hyphenrules}

\begin{document}

\parbox{0pt}{\hspace{0pt}framhaldsskulen}

\end{document}

enter image description here

Use the same trick of \begin{hyphenrules} if you have different languages in your document and want to add some hyphenation rules for single words.


A possibly better interface:

\documentclass{article}

\usepackage{polyglossia}

\makeatletter
\newcommand\sethyphenation[3][]{%
  \begingroup
  \ifcsundef{#2@loaded}
    {\xpg@nogloss{#2}}
    {\setkeys{#2}{#1}\xpg@set@language{#2}%
     \hyphenation{#3}}%
  \endgroup
}
\@onlypreamble\sethyphenation
\makeatother


\setmainlanguage{nynorsk}
\setotherlanguage[variant=british]{english}

\sethyphenation{nynorsk}{fram-halds-skulen}

\sethyphenation{english}{tes-t-word}

\begin{document}

\parbox{0pt}{\hspace{0pt}framhaldsskulen testword}

\selectlanguage{english}

\parbox{0pt}{\hspace{0pt}framhaldsskulen testword}

\end{document}

The \sethyphenation command accepts also an optional argument such as variant=british, but at the moment it wouldn't do much good, because the same option to \selectlanguage doesn't seem to have any effect.

enter image description here