【问题标题】:using knitr/rmarkdown to produce outputs in multiple natural languages (from a data.frame)使用 knitr/rmarkdown 生成多种自然语言的输出(来自 data.frame)
【发布时间】:2017-10-04 07:41:41
【问题描述】:

我正在尝试以rmarkdown 生成一个文档,该文档可以以多种(自然)语言生成输出,它应该从data.frame 中提取其中一个翻译中的文本。

data.frame 应包含每种语言的列以及每行相同文本的翻译,例如

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

应该可以格式化提取的文本,例如

# [desired code here]

应该产生和 rmarkdown 标题。

编辑:理想情况下,我们可以在 YAML 前端指定语言

【问题讨论】:

    标签: r translation knitr r-markdown


    【解决方案1】:

    您可以这样做:(请参阅 ps 了解更多乳胶选项)

    为什么? YAML 标头逐行评估 :) - 请参阅 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"
    )
    `
    

    这会导致

    如果您将 yaml-header 更改为

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

    你得到

    PS:更多乳胶选项

    如果您想使用特定的语言包,您可以在 YAML 标头中添加以下内容:(请参阅https://stackoverflow.com/a/29163496/3301344

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

    例如用中文看看:http://felixfan.github.io/RMarkdown-Chinese-PDF/

    【讨论】:

    • 其实我想出了线的东西,但是将它与rownames结合使用的想法非常优雅
    • 如果我们可以在 YAML 前端定义语言,那就太棒了,有什么想法吗?
    • 谢谢,我查看了文档,但显然不够仔细。我已在编辑中将此添加到问题中。
    • 我收到错误“找不到对象'参数'”
    【解决方案2】:

    当您在这里提出问题时,我也向您添加我的答案。您可以在块中使用catresults='asis',并将您的翻译语言表与格式化块的内容进行比较。
    这样,原始文本会在块中突出显示(在 rstudio 中),以便您可以轻松地在长代码中找到您的文本。使用内联Rcode,一切都是同一种颜色,不容易看到。

    ```{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 = "")
    ```
    

    【讨论】:

      猜你喜欢
      • 2011-03-21
      • 1970-01-01
      • 1970-01-01
      • 2012-06-29
      • 1970-01-01
      • 1970-01-01
      • 2011-02-14
      • 2012-05-12
      • 1970-01-01
      相关资源
      最近更新 更多