| translator | R Documentation |
Structure and manipulate the source text of a project and its translations.
translator(..., id = uuid(), algorithm = algorithms())
is_translator(x)
## S3 method for class 'Translator'
format(x, ...)
## S3 method for class 'Translator'
print(x, ...)
... |
Usage depends on the underlying function.
|
id |
A non-empty and non-NA character string. A globally unique
identifier for the |
algorithm |
A non-empty and non-NA character string equal to |
x |
Any R object. |
A Translator object encapsulates the source text of a project
(or any other context) and all related translations. Under the hood,
Translator objects are collections of Text objects.
These do most of the work. They are treated as lower-level component and in
typical situations, users rarely interact with them.
Translator objects can be saved and exported with
translator_write(). They can be imported back into an R session
with translator_read().
translator() returns an R6 object of class
Translator.
is_translator() returns a logical value.
format() returns a character vector.
print() returns argument x invisibly.
idA non-empty and non-NA character string. A globally unique identifier for the underlying object. Beware of plausible collisions when using user-defined values.
algorithmA non-empty and non-NA character string equal to "sha1",
or "utf8". The algorithm to use when hashing source information for
identification purposes.
hashesA character vector of non-empty and non-NA values, or
NULL. The set of all hash exposed by registered Text
objects. If there is none, hashes is NULL. This is a
read-only field updated whenever field algorithm is updated.
source_textsA character vector of non-empty and non-NA
values, or NULL. The set of all registered source texts. If
there is none, source_texts is NULL. This is a read-only
field.
source_langsA character vector of non-empty and non-NA
values, or NULL. The set of all registered source languages.
This is a read-only field.
If there is none, source_langs is NULL.
If there is one unique value, source_langs is an unnamed
character string.
Otherwise, it is a named character vector.
languagesA character vector of non-empty and non-NA values,
or NULL. The set of all registered languages (codes). If there
is none, languages is NULL. This is a read-only field.
native_languagesA named character vector of non-empty and
non-NA values, or NULL. A map (bijection) of languages (codes)
to native language names. Names are codes and values are native
languages. If there is none, native_languages is NULL.
While users retain full control over native_languages, it is
best to use well-known schemes such as
IETF BCP 47, or
ISO 639-1.
Doing so maximizes portability and cross-compatibility between packages.
Update this field with method $set_native_languages().
See below for more information.
new()Create a Translator object.
Translator$new(id = uuid(), algorithm = algorithms())
idA non-empty and non-NA character string. A globally unique
identifier for the Translator object. Beware of collisions
when using user-defined values.
algorithmA non-empty and non-NA character string equal to "sha1",
or "utf8". The algorithm to use when hashing source information for
identification purposes.
An R6 object of class Translator.
# Consider using translator() instead. tr <- Translator$new()
translate()Translate source text.
Translator$translate( ..., lang = language_get(), source_lang = language_source_get() )
...Any number of vectors containing atomic elements. Each vector is normalized as a paragraph.
Elements are coerced to character values.
NA values and empty strings are discarded.
Multi-line strings are supported and encouraged. Blank lines are interpreted (two or more newline characters) as paragraph separators.
langA non-empty and non-NA character string. The underlying language.
A language is usually a code (of two or three letters) for a native language name. While users retain full control over codes, it is best to use language codes stemming from well-known schemes such as IETF BCP 47, or ISO 639-1 to maximize portability and cross-compatibility.
source_langA non-empty and non-NA character string. The
language of the source text. See argument lang for more
information.
See normalize() for further details on how ... is normalized.
A character string. If there is no corresponding translation,
the value passed to method $set_default_value() is
returned. NULL is returned by default.
tr <- Translator$new()
tr$set_text(en = "Hello, world!", fr = "Bonjour, monde!")
tr$translate("Hello, world!", lang = "en") ## Outputs "Hello, world!"
tr$translate("Hello, world!", lang = "fr") ## Outputs "Bonjour, monde!"
get_translation()Extract a translation or a source text.
Translator$get_translation(hash = "", lang = "")
hashA non-empty and non-NA character string. The unique identifier of the requested source text.
langA non-empty and non-NA character string. The underlying language.
A language is usually a code (of two or three letters) for a native language name. While users retain full control over codes, it is best to use language codes stemming from well-known schemes such as IETF BCP 47, or ISO 639-1 to maximize portability and cross-compatibility.
A character string. If there is no corresponding translation,
the value passed to method $set_default_value() is
returned. NULL is returned by default.
tr <- Translator$new()
tr$set_text(en = "Hello, world!")
# Consider using translate() instead.
tr$get_translation("256e0d7", "en") ## Outputs "Hello, world!"
get_text()Extract a source text and its translations.
Translator$get_text(hash = "")
hashA non-empty and non-NA character string. The unique identifier of the requested source text.
A Text object, or NULL.
tr <- Translator$new()
tr$set_text(en = "Hello, world!")
tr$get_translation("256e0d7", "en") ## Outputs "Hello, world!"
set_text()Register a source text.
Translator$set_text(..., source_lang = language_source_get())
...Passed as is to text().
source_langPassed as is to text().
A NULL, invisibly.
tr <- Translator$new() tr$set_text(en = "Hello, world!", location())
set_texts()Register one or more source texts.
Translator$set_texts(...)
...Any number of Text objects.
This method calls merge_texts() to merge all values
passed to ... together with previously registered
Text objects. The underlying registered source texts,
translations, and Location objects won't be
duplicated.
A NULL, invisibly.
# Set source language.
language_source_set("en")
tr <- Translator$new()
# Create Text objects.
txt1 <- text(
location("a", 1L, 2L, 3L, 4L),
en = "Hello, world!",
fr = "Bonjour, monde!")
txt2 <- text(
location("b", 5L, 6L, 7L, 8L),
en = "Farewell, world!",
fr = "Au revoir, monde!")
tr$set_texts(txt1, txt2)
rm_text()Remove a registered source text.
Translator$rm_text(hash = "")
hashA non-empty and non-NA character string identifying the source text to remove.
A NULL, invisibly.
tr <- Translator$new()
tr$set_text(en = "Hello, world!")
tr$rm_text("256e0d7")
set_native_languages()Map a language code to a native language name.
Translator$set_native_languages(...)
...Any number of named, non-empty, and non-NA character
strings. Names are codes and values are native languages. See
field native_languages for more information.
A NULL, invisibly.
tr <- Translator$new() tr$set_native_languages(en = "English", fr = "Français") # Remove existing entries. tr$set_native_languages(fr = NULL)
set_default_value()Register a default value to return when there is no corresponding translations for the requested language.
Translator$set_default_value(value = NULL)
valueA NULL or a non-NA character string. It can be empty.
The former is returned by default.
This modifies what methods $translate() and
$get_translation() returns when there is no
translation for lang.
A NULL, invisibly.
tr <- Translator$new()
tr$set_default_value("<unavailable>")
find_source(),
translator_read(),
translator_write()
# Set source language.
language_source_set("en")
# Create a Translator object.
# This would normally be done automatically
# by find_source(), or translator_read().
tr <- translator(
id = "test-translator",
en = "English",
es = "Español",
fr = "Français",
text(
location("a", 1L, 2L, 3L, 4L),
en = "Hello, world!",
fr = "Bonjour, monde!"),
text(
location("b", 1L, 2L, 3L, 4L),
en = "Farewell, world!",
fr = "Au revoir, monde!"))
is_translator(tr)
# Translator objects has a specific format.
# print() calls format() internally, as expected.
print(tr)
## ------------------------------------------------
## Method `Translator$new`
## ------------------------------------------------
# Consider using translator() instead.
tr <- Translator$new()
## ------------------------------------------------
## Method `Translator$translate`
## ------------------------------------------------
tr <- Translator$new()
tr$set_text(en = "Hello, world!", fr = "Bonjour, monde!")
tr$translate("Hello, world!", lang = "en") ## Outputs "Hello, world!"
tr$translate("Hello, world!", lang = "fr") ## Outputs "Bonjour, monde!"
## ------------------------------------------------
## Method `Translator$get_translation`
## ------------------------------------------------
tr <- Translator$new()
tr$set_text(en = "Hello, world!")
# Consider using translate() instead.
tr$get_translation("256e0d7", "en") ## Outputs "Hello, world!"
## ------------------------------------------------
## Method `Translator$get_text`
## ------------------------------------------------
tr <- Translator$new()
tr$set_text(en = "Hello, world!")
tr$get_translation("256e0d7", "en") ## Outputs "Hello, world!"
## ------------------------------------------------
## Method `Translator$set_text`
## ------------------------------------------------
tr <- Translator$new()
tr$set_text(en = "Hello, world!", location())
## ------------------------------------------------
## Method `Translator$set_texts`
## ------------------------------------------------
# Set source language.
language_source_set("en")
tr <- Translator$new()
# Create Text objects.
txt1 <- text(
location("a", 1L, 2L, 3L, 4L),
en = "Hello, world!",
fr = "Bonjour, monde!")
txt2 <- text(
location("b", 5L, 6L, 7L, 8L),
en = "Farewell, world!",
fr = "Au revoir, monde!")
tr$set_texts(txt1, txt2)
## ------------------------------------------------
## Method `Translator$rm_text`
## ------------------------------------------------
tr <- Translator$new()
tr$set_text(en = "Hello, world!")
tr$rm_text("256e0d7")
## ------------------------------------------------
## Method `Translator$set_native_languages`
## ------------------------------------------------
tr <- Translator$new()
tr$set_native_languages(en = "English", fr = "Français")
# Remove existing entries.
tr$set_native_languages(fr = NULL)
## ------------------------------------------------
## Method `Translator$set_default_value`
## ------------------------------------------------
tr <- Translator$new()
tr$set_default_value("<unavailable>")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.