using knitr/rmarkdown to produce outputs in multiple natural languages (from a data.frame)

Viewed 1185

I am trying to produce a document in rmarkdown that can produce outputs in multiple (natural) languages, it should extract the text in one of the translations from a data.frame.

The data.frame should contain a column for each language and translations of the same text on each row, e.g.

EN <- c('title', 'author', 'a sentence')
FR <- c('titre', 'auteur', 'une phrase')
translation <- data.frame(EN, FR, stringsAsFactors = FALSE)

It should be possible to format the text extracted, e.g.

# [desired code here]

Should produce and rmarkdown title.

EDIT: Ideally we would be able to specify the language in the YAML front-matter

2 Answers

You can do this as follows: (please see the ps for more latex-options)

Why? The YAML header is evaluated line by line :) - see here

---
params:
  lang: EN
lang: "`r switch(params$lang, DE = 'de-DE', EN = 'en-US')`"
output: pdf_document
toc: 1
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
lswitch <- function(lang, ...){
  switch(lang, ..., stop("No translation privided"))
}
```

# `r lswitch(params$lang, DE = "Dies ist der Titel", EN = "this is the title")`

`r lswitch(params$lang, 
DE = "Die folgende Abbildung zeigt die Funktion $f(x) = x^2$", 
EN = "The following plot shows the function $f(x) = x^2$"
)
`

```{r plot1_cap, include=FALSE}
plot1_cap <- lswitch(params$lang, DE = "Tolle Abbildung", EN = "Great plot")
```
```{r plot1, fig.cap= plot1_cap}
plot(seq(-5, 5, length.out = 50), seq(-5, 5, length.out = 50)^2, 
     type = "l", xlab = "x", ylab = "f(x)")
```

# `r lswitch(params$lang, DE = "Zweiter Titel", EN = "Second Title")`

`r lswitch(params$lang, 
DE = "Zweiter Abschnitt", 
EN = "Second paragraph"
)
`

This results in

enter image description here

If you change the yaml-header to

---
params:
  lang: DE
lang: "`r switch(params$lang, DE = 'de-DE', EN = 'en-US')`"
output: pdf_document
toc: 1
---

you get

enter image description here

PS: More Latex options

If you want to use a specific language package you can add the following in your YAML header: (see https://stackoverflow.com/a/29163496/3301344)

---
header-includes:
  - \usepackage[ngerman]{babel}
---

For e.g. using chinese have a look at: http://felixfan.github.io/RMarkdown-Chinese-PDF/

As you opened a question here, I also add my answer to you. You can use cat in chunk with results='asis', and compare your table of translated language with the content of the formatted chunk.
This way, the original text is highlighted in the chunk (in rstudio) so that you can easily find your text in your long code. With inline Rcode, everything is the same colour, which is not easy to see.

```{r TransRef}
# Translation reference
tr.orig <- translation[,"EN"]
```


```{r MyFirstParagraph, results='asis', verbatim=TRUE}
cat('
# ', translation[tr.orig == "title", lang], '

', translation[tr.orig == "a sentence", lang], '   

- ', translation[tr.orig == "author", lang], '
', sep = "")
```
Related