# Set source language.
language_source_set("en")
# Create Text objects.
txt1 <- text(
location("a", 1L, 2L, 3L, 4L),
location("a", 1L, 2L, 3L, 4L),
location("b", 5L, 6L, 7L, 8L),
location("c", c(9L, 10L), c(11L, 12L), c(13L, 14L), c(15L, 16L)),
en = "Hello, world!",
fr = "Bonjour, monde!",
es = "¡Hola, mundo!")
txt2 <- text(
location("a", 1L, 2L, 3L, 4L),
en = "Hello, world!",
fr = "Bonjour, monde!",
es = "¡Hola, mundo!")
txt3 <- text(
source_lang = "fr2",
location("a", 5L, 6L, 7L, 8L),
en = "Hello, world!",
fr2 = "Bonjour le monde!",
es = "¡Hola, mundo!")
is_text(txt1)
# Texts objects has a specific format.
# print() calls format() internally, as expected.
print(txt1)
print(txt2)
print(txt3)
# Combine Texts objects.
# c() throws an error if they do not have the same
# hash (same souce_text, source_lang, and algorithm).
c(txt1, txt2)
# Text objects with different hashes can be merged.
# This groups Text objects according to their hashes
# and calls c() on each group. It returns a list.
merge_texts(txt1, txt2, txt3)
# Objects can be coerced to a Text object with as_text(). Below is an
# example for call objects. This is for illustration purposes only,
# and the latter should not be used. This method is used internally by
# find_source().
cl <- str2lang("translate('Hello, world!')")
loc <- location("example in class-text", 2L, 32L, 2L, 68L)
as_text(cl, loc)
## ------------------------------------------------
## Method `Text$new`
## ------------------------------------------------
# Consider using text() instead.
txt <- Text$new()
## ------------------------------------------------
## Method `Text$get_translation`
## ------------------------------------------------
txt <- Text$new()
txt$set_translation("en", "Hello, world!")
txt$get_translation("en") ## Outputs "Hello, world!"
txt$get_translation("fr") ## Outputs NULL
## ------------------------------------------------
## Method `Text$set_translation`
## ------------------------------------------------
# Register a pair of source_lang and source_text.
txt <- Text$new()
txt$set_translation("en", "Hello, world!")
txt$source_lang <- "en"
## ------------------------------------------------
## Method `Text$set_translations`
## ------------------------------------------------
txt <- Text$new()
txt$set_translations(en = "Hello, world!", fr = "Bonjour, monde!")
## ------------------------------------------------
## Method `Text$set_locations`
## ------------------------------------------------
txt <- Text$new()
txt$set_locations(
location("a", 1L, 2L, 3L, 4L),
location("a", 1L, 2L, 3L, 4L),
location("b", 5L, 6L, 7L, 8L))
## ------------------------------------------------
## Method `Text$rm_translation`
## ------------------------------------------------
txt <- Text$new()
txt$set_translations(en = "Hello, world!", fr = "Bonjour, monde!")
txt$source_lang <- "en"
# Remove source_lang and source_text.
txt$source_lang <- "fr"
txt$rm_translation("en")
## ------------------------------------------------
## Method `Text$rm_location`
## ------------------------------------------------
txt <- Text$new()
txt$set_locations(
location("a", 1L, 2L, 3L, 4L),
location("b", 5L, 6L, 7L, 8L))
txt$rm_location("a")
Run the code above in your browser using DataLab