5

I have numerous documents in English which will be translated into Chinese. I use ctex package for Chinese typesetting and XeLaTeX for compiling. There are some strings repeating throughout the document (note, attention, feedback, etc.) which I would like to have translated into Chinese automatically using the translations package.

I check if ctex is loaded with \IfPackageLoaded, and then use \selectlanguage{chinese}.

MWE:

mypreamble.sty

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{mypreamble}[test]

%\usepackage[english]{babel}
%\babelprovide{chinese}
\usepackage{translations}

\DeclareLanguage{chinese}

\newcommand*\location{\GetTranslation{mycountry}}

% translations for 'mycountry':
\DeclareTranslationFallback {mycountry}{Default}
\DeclareTranslation{English}{mycountry}{Country}
\DeclareTranslation{chinese} {mycountry}{国家}

% code required for checking if ctex is loaded
\usepackage{ltxcmds}

\makeatletter
\newcommand{\IfPackageLoaded}[3]{\ltx@ifpackageloaded{#1}{#2}{#3}}
\makeatother

main.tex

\documentclass{article}

\usepackage{mypreamble}

% comment out for English, uncomment for Chinese
\usepackage{ctex}

\begin{document}

\IfPackageLoaded{ctex}{\selectlanguage{chinese}}{\selectlanguage{english}}

\tableofcontents
\listoftables

\section{Intro}
\location

\end{document}

Below is what I want and get if I do not add babel (which is required by translations). However, it also gives me an error Package babel Error: You haven't defined the language chinese yet.

enter image description here

If I add babel by uncommenting the two strings in preamble, the error disappears; but then babel messes up with ctex's translation of standard strings (Contents, List of Tables, etc.) into Chinese:

enter image description here

Any help to resolve this dilemma will be very much appreciated!

1 Answer 1

4

It does look like you would need

\usepackage[english]{babel}
\babelprovide[import]{chinese}

to avoid the error. However babel-zh.ini (as well as other Chinese locales, e.g. babel-zh-Hant-HK.ini, babel-zh-Hans.ini etc) doesn't define keys in the [captions] section, so \chinesecontentsname, \chineselisttablename etc are undefined. AFAIK there isn't a way for babel to automatically go look up these defined terms from ctex.

(In TL 2020 babel-ja.ini and babel-ko.ini do define key-value pairs in [captions], so you would not have this problem if you were working with \babel[import]{japanese} or \babel[import]{korean}. But still not the case for babel-zh.ini in TL2020.)

You can define these commands yourself in your main.tex or mypreamble.sty:

\renewcommand{\chinesecontentsname}{目录}
\renewcommand{\chineselisttablename}{表格}

Or if you prefer, you can make a copy of babel-zh.ini (it's in /usr/local/texlive/2020basic/texmf-dist/tex/generic/babel/locale/zh/), rename it to babel-zh-myownhacks.ini, and edit the [captions] section:

[captions]
preface = 前言
ref = 参考文献
abstract = 摘要
...

and then load it in your .tex/.sty via

\babelprovide[import=zh-myownhacks]{chinese}
2
  • A copy of babel-zh.ini worked without a hitch. Added it to a subfolder as suggested here to avoid cluttering my root directory. Thanks a lot!
    – f-hollow
    Jul 31, 2020 at 7:41
  • I cannot find an option to import my custom .ini file from a different directory. Not sure how to add a path to it. Does anybody know how to do it?
    – f-hollow
    Nov 2, 2020 at 4:27

You must log in to answer this question.

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