0

I have two files, main_EN.tex and main.CN.tex, that hold the same content in English and Chinese respectively. They share the same preamble and the glossary file. The glossary is created with the glossaries-extra package. I am new to this package and feel a bit overwhelmed with the number of options and all the intricacies.

So I am not sure how to add to the glossary Chinese definitions for terms and acronyms, as well as Chinese translation for the terms.

Ideally, I would want to have something like this:

\newglossaryentry{charger}
{
    name-english={charger},
    name-chinese={充电器},
    description-english={A device for charging mobile phones},
    description-chinese={给手机充电的设备}
}

\newacronym{usb}{USB}
{
    translation-english={Universal Serial Bus},
    translation-chinese={通用串行总线}
}

Then I want to be able to pull definitions for a selected language.

This thread proposes a way, but I cannot make it work with Chinese, and it looks a bit complicated.

Can you please suggest the best way to do it?

So far, I have tried adding translations with the package translations. See the MWE code below or see the actual MWE.

my-preamble.sty

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{my-preamble}


\usepackage[colorlinks=true]{hyperref}

\usepackage[english]{babel}
% Import local copy of `babel-zh.ini`
\babelprovide[import]{chinese}
\usepackage{translations}
\DeclareLanguage{chinese}

% Allow Chinese typesetting
\usepackage{ctex}

\usepackage[acronym]{glossaries-extra}

\makeglossaries


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Definitions for the translations package %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%\DeclareTranslationFallback{charger}{charger}
\DeclareTranslation{English}{charger}{charger}
\DeclareTranslation{chinese}{charger}{充电器}

%\DeclareTranslationFallback{def-charger}{A device for charging mobile phones}
\DeclareTranslation{English}{def-charger}{A device for charging mobile phones}
\DeclareTranslation{chinese}{def-charger}{给手机充电的设备}

%\DeclareTranslationFallback{def-usb}{Universal Serial Bus}
\DeclareTranslation{English}{def-usb}{Universal Serial Bus}
\DeclareTranslation{chinese}{def-usb}{通用串行总线}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Definitions for the glossaries-extra package %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newglossaryentry{charger}
{
    name={\GetTranslation{charger}},
    description={\GetTranslation{def-charger}}
}

\newacronym{usb}{USB}{\GetTranslation{def-usb}}

main_EN.tex

\documentclass[10pt]{article}
\usepackage{my-preamble}

\begin{document}
\selectlanguage{english}

\section*{In English}

The \gls{charger} has three \glsxtrfull{usb} ports.

\printglossaries
\end{document}

main_CN.tex

\documentclass[10pt]{article}
\usepackage{my-preamble}

\begin{document}
\selectlanguage{chinese}

\section*{在中文中}

\gls{charger}有三个 \glsxtrfull{usb} 端口

\printglossaries
\end{document}

Result

English Chinese
enter image description here enter image description here

As you can see, the sections Glossary and Acronyms show proper translations, but the term label is not replaced with the actual term in the body of the document. I also get multiple "Undefined Control Sequence" errors.

Any help will be very much appreciated!


Edit:

In a comment, Venez proposed an idea to use the iflang package. It works perfectly!

See the MWE code below or see the actual MWE.

my-preamble.sty

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{my-preamble}


\usepackage[colorlinks=true]{hyperref}

\usepackage[english]{babel}
% Import local copy of `babel-zh.ini`
\babelprovide[import]{chinese}
\usepackage{translations}
\DeclareLanguage{chinese}

\usepackage{iflang}

% Allow Chinese typesetting
\usepackage{ctex}

\usepackage[acronym]{glossaries-extra}

\makeglossaries

glossary.tex

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Definitions for the glossaries-extra package %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newglossaryentry{charger}
{
    name={\IfLanguageName{english}
        {charger}
        {充电器}},
    description={\IfLanguageName{english}
        {A device for charging mobile phones}
        {给手机充电的设备}}
}
    
\newacronym{usb}{USB}
{
    \IfLanguageName{english}
        {Universal Serial Bus}
        {通用串行总线}
}

main__EN.tex

\documentclass[10pt]{article}
\usepackage{my-preamble}

\begin{document}

\selectlanguage{english}
\input{glossary}

\section*{In English}

The \gls{charger} has three \glsxtrfull{usb} ports.

{\let\clearpage\relax
    \printglossaries
}

\end{document}

main__CN.tex

\documentclass[10pt]{article}
\usepackage{my-preamble}

\begin{document}

\selectlanguage{chinese}
\input{glossary}

\section*{在中文中}

\gls{charger}有三个 \glsxtrfull{usb} 端口

{\let\clearpage\relax
    \printglossaries
}

\end{document}

Result

English Chinese
enter image description here enter image description here
4
  • 1
    I think you have multiple choices here. 1st: you declare your own fields with their commands (\glsEN VS\glsCN). 2nd: you use separate glossaries for each language with identical labels so you only have to activate the right one in your actually main file. However, that's not really maintenance-friendly from my POV. 3rd: within one glossary you distinguish between two types or categories. Here you need different labels for every entry (e.g. en:charger VS cn:charger). And there are more ... so in the end the question is what's most important for you: a simple glossary or a simple main?
    – Venez
    Mar 18, 2021 at 9:27
  • @Venez Thank you for your suggestions! My priority is to keep the main files simple, so options 1 and 3 are not for me. I thought about having two separate glossaries (reluctant to go this way for maintenance issues as you pointed out) and would have to choose this option if there is no simple solution to keep the EN and CN definitions in the same glossary file.
    – f-hollow
    Mar 18, 2021 at 10:06
  • 1
    One last idea came up my mind, because i used something similar ... you could try the iflang package (see CTAN) to maintain only one glossary, but access both language variations by the same label (e.g. name={\IfLanguageName{english}{then}{else}}).
    – Venez
    Mar 18, 2021 at 16:01
  • @Venez Works like a charm with iflang! Suggest that you post your comment as an answer and I will upvote it. I can delete the edit in my question. Thank you!
    – f-hollow
    Mar 19, 2021 at 7:21

1 Answer 1

1

Following a short discussion (see comments below original question) the answer was a use of the iflang package. So the code below is a reduced MWE.

@f-hollow You do not have to delete your edit, but i propose you add your original code as well. Therefore it should be easy to understand first the problem and then the solution.

\documentclass[10pt]{article}
\usepackage{babel}
\usepackage{ctex} % allows chinese typesetting, but you must have a compatible OS-font 
\usepackage{iflang}
\usepackage[acronym]{glossaries-extra}


\newglossaryentry{charger}
{
    name={\IfLanguageName{english}{charger}{充电器}},
    description={\IfLanguageName{english}{A device for charging mobile phones}{给手机充电的设备}}
}
\newacronym{usb}{USB}
{
    \IfLanguageName{english}{Universal Serial Bus}{通用串行总线}
}

\makeglossaries
\begin{document}
    \selectlanguage{english}
    \section*{In English}   
    The \gls{charger} has three \glsxtrfull{usb} ports.
    
    \printglossaries
\end{document}
1
  • Need to point out that the glossary definitions should be preceded by \selectlanguage, otherwise the compiler always chooses English. In its turn, \selectlanguage must be inside of the document environment. So the order is \begin{document} > \selectlanguage > glossary defs.
    – f-hollow
    Mar 19, 2021 at 9:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .