1

Let's say we wish to verbalize the colors used in a document in the current language:

\documentclass[USenglish,ngerman]{article}
\pagestyle{empty}
\usepackage{babel}
\usepackage[x11names]{xcolor}
\usepackage{etoolbox}
\newcommand{\actionColor}{blue}
\newcommand{\actionColorGerman}{blau}
\begin{document}
Die Aktionen werden \ifdefempty{\actionColorGerman}{in Textfarbe}{\actionColorGerman} gesetzt.
\end{document}

yields

Die Aktionen werden blau gesetzt.

(Which means that the actions (whatever an action might mean in my text) are typeset blue.)

The current code is bad because \actionColor and \actionColorGerman are not tied together by a command. For example, if you change/remove one (say, blue), you may forget to change/remove the other (here, blau). Moreover, if you have a dozen of different semantic color macros \doodahXYZColor evaluating to blue and another dozen \gimmickXYZColor evaluating to red, you'd have to add a dozen of translations \doodahXYZColorGerman evaluating to blau and another dozen of translations \gimmickXYZColorGerman evaluating to rot. This is useless duplication.

Has some package already done the exercise of automatically translating the color names? I don't wish to reinvent the bicycle. Ideally, I'd like to have a macro

\colorInCurrentLanguage{…}

that takes an argument (in our MWE, \actionColor) and returns its translation in the current document language (in our MWE, blau).

For my current purposes, translations into German would do.

  • I guess taking a list of color names and copy pasting it into Google translate would be an option? This seems oddly specific. – user202729 Nov 19 '22 at 19:30
  • 1
    q176058 seems like one way to solve this. – Qrrbrbirlbel Nov 19 '22 at 19:45
  • @user202729 No, as some kind of switch statement taking an English color and returning a foreign-language color (and a default string if no translation has been found) would still be needed. Also, translations may vary. And, most important, I don't wish to bloat up the code with strings potentially already stored somewhere deep in babel, xcolor, or related packages. –  Nov 19 '22 at 19:46
  • Grepping for blau in my TeXlive directory the only relevant I can find is https://i.stack.imgur.com/mrXlS.png . So I'd say not particularly usable here. – user202729 Nov 19 '22 at 19:54
  • It's certainly possible, but I wonder why? color is conceptually like a command name we happened to choose \color{blue} rather than \blue but that was an arbitrary choice. You could translate all command names such as \begin (context had that posibility) but why just colors? – David Carlisle Nov 19 '22 at 20:01
  • @DavidCarlisle I my text I wish to explain which parts are typeset in which colors (call it a legend). So, the blue color in LaTeX sources gets shown in the output also as the German text blau; this is what I wish to show to the reader. As the sources don't have verbatim blue each time I use this color but rather a semantic macro for it (e.g., \actionColor), I don't wish to write blau each time either. Rather, \actionColor should be automatically displayed in German on need (say, via \GetTranslation{\actionColor}). –  Nov 19 '22 at 20:21
  • @user202729 i.stack.imgur.com/mrXlS.png is found in mkii an mkiv dirs on my machine. Usable, probably, for outdated ConTeXt versions only. –  Nov 19 '22 at 20:25
  • @Qrrbrbirlbel The package translations doesn't like macros as arguments. Running pdflatex on https://pastebin.com/raw/AdqmEaPp produces Die Aktionen werden “actionColor gesetzt. in the output PDF file. That's NOT what we want :-(. –  Nov 19 '22 at 20:41
  • 1
    @AlbertNash You're going to need \expandafter it seems: Die Aktionen werden \ifdefempty{\actionColor}{in Textfarbe}{\expandafter\GetTranslation\expandafter{\actionColor}} gesetzt. – Qrrbrbirlbel Nov 19 '22 at 20:46
  • @Qrrbrbirlbel This is quite a kludge but, hey, it works! Thanks! It seems that using the translator package would allow for writing \newtranslation[to=German]{blue}{blau} in the preamble and simply \translate{\actionColor} in the document text, without any \expandafter. So why have you suggested translations and not translator? –  Nov 19 '22 at 21:35
  • 1
    @AlbertNash Both packages are mentioned in the linked question. I don't have any experience with any of them. I certainly don't know the advantages or drawbacks of either package. Having to use \expandafter isn't the worst thing (since you can always let a macro do this).maybe the translation package provides more tools and macros to solve this. (Though, I'm surprised it breaks. I've expected it to expand its argument in the first place.) – Qrrbrbirlbel Nov 20 '22 at 02:11

0 Answers0