There is no inbuilt functionality like this in glossaries
, but we can patch it in using babel
's \languagename
.
- For normal multilanguage description, I patched
\gls@defglossaryentry
to use \@mygls@selectdesc
in the default value for description
.
This macro will try to load the description in the current language and issue a warning if there is none.
Note that if you explicitly set description
in any entry you will overwrite this and the set value will display regardless of \languagename
.
- For abbreviations/acronyms this does not really make sense, I think, since the acronym is in one language.
We can however add translations to the abbreviation and display them if present (no patching of internal commands needed here).
For the displaying part, we define a custom glossary style.
\documentclass[spanish]{article}
\usepackage[T1]{fontenc}
\usepackage{babel}
\usepackage{xparse}
\usepackage{csquotes}
\usepackage[
abbreviations,
shortcuts=abbreviations,
stylemods=longbooktabs,
nonumberlist,
]{glossaries-extra}
\makeglossaries
% Create a custom glossary style for abbreviations, printing a translation if present.
\newglossarystyle{custom-long-booktabs}{%
\setglossarystyle{long-booktabs}%
\renewcommand\glossentry[2]{%
\glsentryitem{##1}\glstarget{##1}{\glossentryname{##1}} &
\glossentrydesc{##1}%
\glsxtrifhasfield{translation-\languagename}{##1}{%
\ \small(\enquote{\csname glsentrytrans\languagename\endcsname{##1}})
}{}%
\glspostdescription\space ##2\tabularnewline
}%
}
\makeatletter
% a macro for selecting the description in the current language
\def\@mygls@selectdesc#1{%
\glsxtrifhasfield{description-\languagename}{#1}{%
\glscurrentfieldvalue
}{%
\GenericWarning{}{LaTeX Warning:
No description for `#1' in language `\languagename'}
}%
}
% replace the default value of description with \@mygls@selectdesc
\patchcmd\gls@defglossaryentry
{\let\@glo@desc\@glsnodesc}
{\def\@glo@desc{\@mygls@selectdesc{#1}}}
{}{}
% Add description keys for the different languages.
\NewDocumentCommand\add@glslangdesckey{m o}{%
\begingroup
\edef\@tempa{%
\endgroup
\noexpand\glsaddkey*{description-#1}%
{\IfValueT{#2}{\expandafter\noexpand\csname glsentrydesc#2\endcsname{\noexpand\glslabel}}}%
\expandafter\noexpand\csname glsentrydesc#1\endcsname
\expandafter\noexpand\csname Glsentrydesc#1\endcsname
\expandafter\noexpand\csname glsdesc#1\endcsname
\expandafter\noexpand\csname Glsdesc#1\endcsname
\expandafter\noexpand\csname GLSdesc#1\endcsname
}%
\@tempa
}
\add@glslangdesckey{english}
\add@glslangdesckey{german}
\add@glslangdesckey{ngerman}[german]
\add@glslangdesckey{spanish}
\add@glslangdesckey{french}
% Add translation keys for the different languages.
\NewDocumentCommand\add@glslangtranskey{m o}{%
\begingroup
\edef\@tempa{%
\endgroup
\noexpand\glsaddkey*{translation-#1}%
{\IfValueT{#2}{\expandafter\noexpand\csname glsentrytrans#2\endcsname{\noexpand\glslabel}}}%
\expandafter\noexpand\csname glsentrytrans#1\endcsname
\expandafter\noexpand\csname Glsentrytrans#1\endcsname
\expandafter\noexpand\csname glstrans#1\endcsname
\expandafter\noexpand\csname Glstrans#1\endcsname
\expandafter\noexpand\csname GLStrans#1\endcsname
}%
\@tempa
}
\add@glslangtranskey{english}
\add@glslangtranskey{german}
\add@glslangtranskey{ngerman}[german]
\add@glslangtranskey{spanish}
\add@glslangtranskey{french}
\makeatother
% Define some glossary entries.
\newglossaryentry{one}{
name=one,
symbol=1,
description-english={the number one},
description-german={die Zahl Eins},
description-spanish={el número uno},
description-french={le numéro un},
}
\newglossaryentry{two}{
name=two,
symbol=2,
description-english={the number two},
description-german={die Zahl Zwei},
description-spanish={el número dos},
description-french={le numéro deux},
}
\newglossaryentry{three}{
name=three,
symbol=3,
description-german={die Zahl Drei},
description-spanish={el número tres},
}
% Define some abbreviations/acronyms.
\newacronym[
translation-english={union of south american nations},
translation-german={Union südamerikanischer Nationen},
translation-french={union des nations sud-américaines},
]{unasur}{UNASUR}{Unión de Naciones Suramericanas}
\newacronym[
translation-german={Nordatlantikpakt-Organisation},
translation-spanish={organización del tratado del atlántico norte (OTAN)},
translation-french={organisation du traité de l'Atlantique nord (OTAN)},
]{nato}{NATO}{north atlantic treaty organization}
\newabbreviation[
translation-english={and so forth},
translation-german={und so weiter},
translation-spanish={y lo demás},
translation-french={et les autres choses manquent},
]{etc}{etc.}{et cetera}
\begin{document}\makeatletter
\verb|\languagename| is \texttt{\languagename}
\gls{one},
\gls{two},
\gls{three}
\as{unasur},
\as{nato},
\as{etc}
\printglossary[type=main, style=tree]
\printglossary[type=abbreviations, style=custom-long-booktabs]
\end{document}