R Markdown Website not respecting language in _site.yml

The following _site.yml creates a website with an <html> tag that does not specify a language. (Accessibility checkers will throw a complaint about this.)

name: "My Website!"
output_dir: "docs"
include: [".nojekyll"]
output:
  html_document:
    lang: en
    css: style.css
    include:
      in_header: header.html
      after_body: footer.html

If within an individual .Rmd file a language is specified in the YAML, the correct HTML is created.

For example, within the .Rmd you use:

---
title: "Title of Page"
author: "Cool R Markdown User"
lang: en
---

The rendered document then contains: <html lang="en" xml:lang="en">

Is this a feature, or a bug? (Perhaps it's good practice to specify a language for each page specifically.)

You need to do exactly the same for Rmarkdown website as you do for a standard Rmarkdown file.
lang: en must be passed at the first level of the YAML - this is how variables are passed to templates. (See for example : https://bookdown.org/yihui/rmarkdown-cookbook/html-template.html)

So in _site.yml, keep the same level as you did in your yaml header

name: "My Website!"
output_dir: "docs"
include: [".nojekyll"]
output:
  html_document:
    css: style.css
    include:
      in_header: header.html
      after_body: footer.html
lang: en

lang is not an option of html_document, so you should not pass it under output specificication. For example using your Rmd example, adding output format and options would be this way

---
title: "Title of Page"
author: "Cool R Markdown User"
output: 
    html_document: 
       toc: true
lang: en
---

Hope it helps.

Perhaps I'm missing something, but I had tried that before posting originally, and now again, but that has no effect.

In an attempt to replicate, I fired up RStudio, created a new project using the "Simple R Markdown Website" project type, and only modified the project to add the lang line to the YAML like so:

name: "my-website"
lang: en
navbar:
  title: "My Website"
  left:
    - text: "Home"
      href: index.html
    - text: "About"
      href: about.html

The resulting HTML does not include the language information in the <html> tag.

Hum.. it worked for me. Let my retry

Thanks a bunch. Just so there's no confusion about what I did: https://github.com/daviddalpiaz/rmd-lang-test

Also my session information in case that matters:

> xfun::session_info()
R version 4.0.2 (2020-06-22)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Catalina 10.15.6, RStudio 1.3.1073

Locale: en_US.UTF-8 / en_US.UTF-8 / en_US.UTF-8 / C / en_US.UTF-8 / en_US.UTF-8

Package version:
  askpass_1.1      base64enc_0.1.3  compiler_4.0.2   curl_4.3         digest_0.6.25    evaluate_0.14   
  glue_1.4.1       graphics_4.0.2   grDevices_4.0.2  highr_0.8        htmltools_0.5.0  jsonlite_1.7.0  
  knitr_1.29       magrittr_1.5     markdown_1.1     methods_4.0.2    mime_0.9         openssl_1.4.2   
  packrat_0.5.0    rlang_0.4.7      rmarkdown_2.3    rsconnect_0.8.16 rstudioapi_0.11  stats_4.0.2     
  stringi_1.4.6    stringr_1.4.0    sys_3.4          tinytex_0.25     tools_4.0.2      utils_4.0.2     
  xfun_0.16        yaml_2.2.1      

1 Like

Yes you're right. It does not set for all the page. Thank you for the GH reprex and Sorry... :grimacing:
I had a lang: en in the Rmd file directly so I was mislead.

Currently, it seems this need to be set in all pages. I'll reopen the GH issue to verify is this is on purpose. it could work that way curently if pandoc variable are passed only at the document base.

Haha, no worries. I actually had the same issue with it being set in an Rmd file when I was doing some debugging on my end. Thanks again!

But know that you can pass pandoc argurments through the pandoc_args argument in output format function. That means variables can be set this way though this argument. You can define the output globally in _site.yml. This should work:

name: "my-website"
output:
  html_document:
    pandoc_args: !expr rmarkdown::pandoc_variable_arg("lang", "en")
navbar:
  title: "My Website"
  left:
    - text: "Home"
      href: index.html
    - text: "About"
      href: about.html

rmarkdown::pandoc_variable_arg("lang", "en") is equivalent to "--variable lang=en" so you can also use

output:
  html_document:
    pandoc_args: ["--variable", "lang=en"]

This is a way to globally set the variable.

Would this work as you expect ?

Yes! That's essentially the behavior I expected. Thanks!

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.