TransWikia.com

Glossary with terms and definitions in English and Chinese

TeX - LaTeX Asked on April 21, 2021

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.

{letclearpagerelax
    printglossaries
}

end{document}

main__CN.tex

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

begin{document}

selectlanguage{chinese}
input{glossary}

section*{在中文中}

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

{letclearpagerelax
    printglossaries
}

end{document}

Result

English Chinese
enter image description here enter image description here

One Answer

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}

Correct answer by Venez on April 21, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP