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}

Paul A
  • 1,055
  • 4
  • 14
  • 3
    no you can't translate free text with LaTeX. – Ulrike Fischer Jan 02 '23 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? – Marijn Jan 03 '23 at 09: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 – Paul A Jan 03 '23 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? – Marijn Jan 03 '23 at 19:09
  • 1
    If you want to provide the translations manually then something like https://tex.stackexchange.com/a/60795/ seems appropriate. – Marijn Jan 03 '23 at 19:19
  • Yes sorry that did make it less clear. I want the text automatically demonstrated, I just used google translate to get the Polish text, which is what I want to avoid having to do. Sorry for the confusion. – Paul A Jan 03 '23 at 19:23

1 Answers1

2

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.

Marijn
  • 37,699
  • 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. – Paul A Jan 06 '23 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. – Marijn Jan 06 '23 at 21:39
  • thanks for your patience, I am getting a latex error: file 'translated.tex' not found. but I don't think I got that last night. – Paul A Jan 06 '23 at 21:46
  • 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. – Marijn Jan 06 '23 at 21:52
  • awk, translate-shell and gnu-sed are in my brew list formulae in my terminal, I have tried to update everything but it still is not working. Could it be a Mac problem? – Paul A Jan 06 '23 at 22:32
  • 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. – Marijn Jan 07 '23 at 07:29
  • Hi @Marijn I have edited post with a screen shot of my desktop with my terminal, console and editor. – Paul A Jan 07 '23 at 08:21
  • 1