Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/rossellhayes/and

Construct natural-language lists with internationalization in R šŸš‚
https://github.com/rossellhayes/and

i18n internationalization r translation

Last synced: 2 months ago
JSON representation

Construct natural-language lists with internationalization in R šŸš‚

Awesome Lists containing this project

README

        

---
output: github_document
---

```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)

library(and)
# pak::pkg_install("GuangchuangYu/badger")
library(badger)
withr::local_language("en_US")
```

# and

`r badge_cran_release(color = "brightgreen")`
`r badge_runiverse()`
`r badge_lifecycle("stable")`
`r badge_license(color = "blueviolet")`
`r badge_github_actions(action = "R-CMD-check")`
[![](https://codecov.io/gh/rossellhayes/and/branch/main/graph/badge.svg)](https://app.codecov.io/gh/rossellhayes/and)
`r badge_dependencies()`

**and** constructs language-aware lists in R. It extends the functionality of functions like `knitr::combine_words()` and `glue::glue_collapse()` to make _and_-separated and _or_-separated lists that automatically conform to the user's R language settings.

## Installation

You can install the released version of **and** from [CRAN](https://cran.r-project.org/package=and) with:

``` {r eval = FALSE}
install.packages("and")
```

or the development version of **and** from [GitHub](https://github.com/rossellhayes/and) with:

``` {r eval = FALSE}
# install.packages("pak")
pak::pkg_install("rossellhayes/and")
```

## Usage

```{r include = FALSE}
library(and)
```

`and()` creates "and"-separated lists from vectors.

```{r}
names <- c("John", "Paul", "George", "Ringo")
and(names)
```

But the Oxford comma is less common in other varieties of English, what happens if I change my R language to British English?

```{r}
Sys.setenv(LANGUAGE = "en_GB")
and(names)
```

What about other languages?

```{r}
Sys.setenv(LANGUAGE = "es")
and(names)
Sys.setenv(LANGUAGE = "eu")
and(names)
Sys.setenv(LANGUAGE = "ko")
and(names)
```

### Handling the nuances

Creating a list is not as simple as putting a different word between the last two items.
For example, in Spanish, the word for _and_ changes if the next word starts with an āŸØiāŸ© or āŸØyāŸ©:

```{r}
princess_bride <- c("Vizzini", "Fezzik", "Inigo Montoya")
Sys.setenv(LANGUAGE = "es")
and(princess_bride)
```

### _or_-separated lists

Everything `and()` can do `or()` can do better. Just use `or()` to create _or_-separated lists with all the same contextual awareness.

```{r}
outcomes <- c("win", "lose", "draw")
Sys.setenv(LANGUAGE = "en_US")
or(outcomes)
Sys.setenv(LANGUAGE = "ja")
or(outcomes)
```

### Hardcoding language

Don't want the language of you string to depend on the user's environment variables?
You can explicitly set the language using the `language` argument.

```{r}
and(names, language = "en_US")
and(names, language = "en_GB")
and(names, language = "es")
and(names, language = "fr")
```

### Languages

```{r include = FALSE}
library(dplyr)
library(knitr)
library(rlang)
library(stringr)
Sys.setenv(LANGUAGE = "en_US")

links <- and_languages %>%
filter(
!str_detect(language, "English \\(.+\\)") |
language == "English (United Kingdom)"
) %>%
mutate(
link = language %>%
str_remove(" \\(.+\\)") %>%
paste0("(https://en.wikipedia.org/wiki/", ., "_language)") %>%
str_replace_all(" ", "_"),
markdown = language %>%
str_remove(" \\(United Kingdom\\)") %>%
paste0("[", ., "]", link),
markdown = case_match(
code,
"en" ~ paste(
markdown,
"(with [Oxford comma](https://en.wikipedia.org/wiki/Serial_comma))"
),
"en_GB" ~ paste(
markdown,
"(without [Oxford comma](https://en.wikipedia.org/wiki/Serial_comma))"
),
.default = markdown
),
code = if_else(code == "en_GB", "`en_GB` et al.*", paste0("`", code, "`"))
) %>%
arrange(language)
```

#### Fully supported languages

```{r echo = FALSE, results = "asis"}
links %>%
filter(support == "full") %>%
select(markdown, code, example_and_4, example_or_4) %>%
set_names(c("Language", "Code", "*and* example", "*or* example")) %>%
kable()
```

\* The following English variants use an Oxford comma in addition to `en_GB`: `r and::and(paste0("\x60", sort(str_subset(and_languages$code, "en_(?!GB)")), "\x60"))`.

#### Partially supported languages

Partially supported languages generally localize `and()` but not `or()`.

```{r echo = FALSE, results = "asis"}
links %>%
filter(support == "partial") %>%
select(markdown, code, example_and_4) %>%
set_names(c("Language", "Code", "*and* example")) %>%
kable()
```

---

Hex sticker image by Flavia Rossell Hayes.

Please note that **and** is released with a [Contributor Code of Conduct](https://contributor-covenant.org/version/2/0/CODE_OF_CONDUCT.html).
By contributing to this project, you agree to abide by its terms.