Bas*_*ast 6 translation r knitr r-markdown
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)
Run Code Online (Sandbox Code Playgroud)
It should be possible to format the text extracted, e.g.
# [desired code here]
Run Code Online (Sandbox Code Playgroud)
Should produce and rmarkdown title.
编辑:理想情况下,我们将能够在 YAML 前端指定语言
您可以按如下方式执行此操作:(请参阅 ps 了解更多乳胶选项)
为什么?YAML 标头是逐行评估的:) - 请参阅此处
---
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"
)
`
Run Code Online (Sandbox Code Playgroud)
这导致
如果将 yaml-header 更改为
---
params:
lang: DE
lang: "`r switch(params$lang, DE = 'de-DE', EN = 'en-US')`"
output: pdf_document
toc: 1
---
Run Code Online (Sandbox Code Playgroud)
你得到
PS:更多乳胶选项
如果您想使用特定的语言包,您可以在 YAML 标头中添加以下内容:(请参阅/sf/answers/2041444751/)
---
header-includes:
- \usepackage[ngerman]{babel}
---
Run Code Online (Sandbox Code Playgroud)
例如使用中文请查看:http://felixfan.github.io/RMarkdown-Chinese-PDF/