nsprintf: Easy Internationalized Message Pluralization

Description Usage Arguments Value Author(s) Examples

Description

A single function to facilitate the correct pluralization and internationalization of R messages.

Usage

1
nsprintf(n, msg1, msg2, ..., domain = NULL)

Arguments

n

A numerical value, passed to ngettext.

msg1

A character string containing an English-language message when n == 1, passed to ngettext.

msg2

A character string containing an English-language message when n != 1, passed to ngettext.

...

Additional arguments passed to sprintf.

domain

A character string specifying a gettext domain, passed to ngettext.

Value

A character vector.

Author(s)

Thomas J. Leeper

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
# simple pluralization
nsprintf(0:3, "there is one apple", "there are %d apples")

# additional arguments to `sprintf()`
# count the number of objects in each environment on the search path
envs <- search()
n <- vapply(envs, function(env) length(ls(env)), integer(1))
nsprintf(n, "there is %d variable in %s", 
            "there are %d variables in %s", envs)
            
# nested nsprintf: 
n_objects <- 6
k_chosen <- seq(0, n_objects)
n_combinations <- choose(n_objects, k_chosen)
nsprintf(
  n_combinations, 
  "there is %d combination of %s chosen from a set of size %d", 
  "there are %d combinations of %s chosen from a set of size %d",
  nsprintf(k_chosen, "%d object", "%d objects"),  
  n_objects
)

RL10N/nsprintf documentation built on May 8, 2019, 5:56 a.m.