0

enter image description hereI am unsure if this is way beyond what Latex can do but is there a way to translate a documents plain text into another language without having to do it paragraph by paragraph?

I have had a look into Tortoise Tagger and omegaT but can't get my head around it.

\documentclass{article} \usepackage{blindtext} \begin{document} \blindtext \vspace{1cm} Can this be translated into another language? (Supposing of course it was in English for example into Spanish), As well as keeping some maths work. $5+4=9$ \end{document} 

Any help would be appreciated even if it is no this it not what Latex is even trying to do. Thanks

Edit to try and explain what I was after.

\documentclass{article} \begin{document} %What I want to write: This is how to Add $5+4$%%%%. input %%% Some code then... to jak dodać $5+4$ %What it prints in Polish $5+4=9$ \end{document} 
7
  • 4
    no you can't translate free text with LaTeX. Commented Jan 2, 2023 at 19:20
  • 1
    Depending on what you want to do exactly there may be some options. Can you clarify, what do you mean with "do it paragraph by paragraph", i.e., what is your current workflow? More in general, do you want the output to be a .tex file with the plain text in another language, or can the source code be in one language and the pdf output in another language, or would it be ok to just translate the pdf directly? Are you willing to add some extra code to the file, like \begin{translate}translate this part\end{translate}? On how many files do you want to do this? Commented Jan 3, 2023 at 9:19
  • Thanks, I was perhaps just thinking if I made a instruction file for some maths worksheet, for example, I could write in English, then maybe insert code like you indicated above to change to other languages when printing off the PDF but the whole file not to be altered by the translation code, i.e any diagrams etc would not be affected. I hope this is clear, if not I will try and mock up an example now and post that if you think it could be possible. @Marijn Commented Jan 3, 2023 at 17:30
  • Hm it is somewhat less clear now - do you want to provide the translations yourself or do you want the paragraphs to be automatically translated? Commented Jan 3, 2023 at 19:09
  • 1
    If you want to provide the translations manually then something like tex.stackexchange.com/a/60795 seems appropriate. Commented Jan 3, 2023 at 19:19

2 Answers 2

3

Here is a proof of concept. The idea is:

  1. surround parts of the document that you want to translate with \begin{translate} and \end{translate}
  2. collect the contents of this environment in a file
  3. escape all single \ as double \\ to avoid shell control sequences that confuse the translation tool in the next step
  4. call a translate tool with this file and write the translation to another file
  5. input the other file in your LaTeX document

The implementation of step 2 was provided by Ulrike Fisher in https://tex.stackexchange.com/a/2574/.

For step 4 I used translate-shell for Linux which supports Google Translate, Bing, Apertium, Yandex, and a number of spell checkers which are not translation engines (spell, aspell, hunspell). For step 3 I used sed.

The target and source languages are given as an optional argument to the translate environment for the source (default English) and a mandatory argument for the target.

MWE:

\documentclass{article} \usepackage{xcolor} \usepackage{shellesc} \usepackage{environ} \newwrite\myexport \makeatletter \NewEnviron{translate}[2][en]{% % step 2 \toks@=\expandafter{\BODY}% \immediate\openout\myexport=totrans.tex \immediate\write\myexport{\the\toks@} \immediate\closeout\myexport % step 3 \ShellEscape{sed -i -e 's/\\/\\\\/g' totrans.tex} % step 4 \ShellEscape{trans -b -i totrans.tex -o translated.tex #1:#2} % step 5 \input{translated.tex} } \makeatother \begin{document} \begin{translate}[en]{es} Can this be translated into another language? (Supposing of course it was in English for example into Spanish), As well as keeping some maths work. The snippets can span multiple paragraphs. \end{translate} \section{Addition in Polish} \begin{translate}{pl} Addition in blue: \color{blue} This is how to Add $5+4$%%%%. input \end{translate} $5+4=9$ \end{document} 

Result:

enter image description here

Disclaimer: saving the contents of an enviroment to a file in this way may break if the environment has some complex processing (for example if there is verbatim content). Also the translation may affect macro names or arguments, although Google Translate does seem to leave them alone (see the \color{blue} part in the MWE which is not translated). Of course step 3 above may also be a source of problems, although the doubled \\ gets back-converted during translation (i.e., blue: \\color{blue} is translated as niebiesko: \color{blue}).

One mistake here is that $5+4$ is mysteriously translated as 5 $ + 4 $ which messes up the spacing. You may need to experiment in how to enter the input such that the code survives the translation properly.

8
  • Thanks @marijn, I really appreciate your help. I thought it worked yesterday, but I can't get it to work today, let alone alter it. I don't want to keep pestering you on here which might annoy people, do you know of another site where I can perhaps DM people about problems I have with this code to avoid getting this thread blocked. As what I can see it should be working I just am not skilled enough to figure you what I am not seeing. Commented Jan 6, 2023 at 21:26
  • 1
    @Paul we are not so strict on this site when it comes to blocking a conversation, as long as you keep it civil. So we can continue the trouble-shooting process here in the comments, and if it becomes a long exchange then the site will automatically offer to create a chatroom for an extended conversation. Once everything is solved we can remove the messages that are no longer needed. Commented Jan 6, 2023 at 21:39
  • 1
    That probably means that the trans program is not working, which is the translation tool that does the actual work. Did you install this program (the package is called translate-shell), and if so, how (for Mac it is available for Homebrew or MacPorts)? Does the program work when you try it manually in the terminal? Same for the sed program which is used one line earlier. The translation tool also requires AWK, which is installed by default on Linux but maybe not on Mac, I'm not sure. Commented Jan 6, 2023 at 21:52
  • 1
    Did you try the exact example in my answer or did you try a different text? If you tried a different text then maybe extracting the paragraph did not work properly. You can open the source file totrans.tex in your editor (after a failed run) and see what is in there. If that looks good then you can try to go in the terminal, navigate to the folder of your .tex file, and then running trans -i totrans.tex en:es (so without the -b flag and without the output file option, change the language setting if needed). This will tell you if anything goes wrong in running trans. Commented Jan 7, 2023 at 7:29
  • 1
    Let us continue this discussion in chat. Commented Jan 7, 2023 at 13:37
1

Another solution that does not rely on any external tool is to define new commands:

% Define these commands at the very beginning of your code \newcommand\en[1]{} % command for English language \newcommand\pl[1]{} % command for Polish language 

and to use them everywhere translation is needed, e.g.

\section{\en{Tutorial}\pl{Seminarium}} 

To switch between English and Polish, simply edit your language commands. For Polish output, use

\newcommand\en[1]{} % all English command contents will be discarded by compiler \newcommand\pl[1]{#1} % all Polish command contents will be read by the compiler 

and for English

\newcommand\en[1]{#1} % all English command contents will be read by compiler \newcommand\pl[1]{} % all Polish command contents will be discarded by the compiler 

Note that some LaTeX packages depend on language. You can use the same trick to import them in one language or another.

\en{\usepackage[english]{babel}} \pl{\usepackage[polish]{babel}} 

Here is a short compilable code example:

\newcommand\en[1]{#1} \newcommand\pl[1]{} \documentclass{article} \en{\usepackage[english]{babel}} \pl{\usepackage[polish]{babel}\usepackage[T1]{fontenc}} \title{\en{My article}\pl{Mój artykuł}} \author{\en{Me}\pl{Ja}} \begin{document} \maketitle \section{\en{Introduction}\pl{Wstęp}} \en{Your introduction goes here!} \pl{Tutaj możesz się przedstawić!} \end{document} 

I have detailed this method in this repository. Hope it may help :)

4
  • Welcome to TeX.SE! Can you show a short compilable code for a fast proof? Commented May 26 at 18:19
  • This is more or less the same approach as in the linked question tex.stackexchange.com/questions/60781/…, maybe it would be better to add this answer there. For the question the original asker specified that they want an automatic translation, instead of providing the translations themselves within the document. Commented May 26 at 20:47
  • Hi Mensch, thanks for your comment. I have edited the post to add a minimal working example. Commented May 28 at 6:34
  • Hi Marijn, thanks I will have a look there :) here I wanted to suggest a work around, but I admit it doesn't truly answer the question. Commented May 28 at 6:37

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.