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
.
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:
Any help to resolve this dilemma will be very much appreciated!