# Set source language.
language_source_set("en")
# Create a path to a temporary Translator file.
temp_path <- tempfile(pattern = "translator_", fileext = ".yml")
temp_dir <- dirname(temp_path) ## tempdir() could also be used
# Create a Translator object.
# This would normally be done by find_source(), or translator_read().
tr <- translator(
id = "test-translator",
en = "English",
es = "Español",
fr = "Français",
text(
en = "Hello, world!",
fr = "Bonjour, monde!"),
text(
en = "Farewell, world!",
fr = "Au revoir, monde!"))
# Export it. This creates 3 files: 1 Translator file, and 2 translations
# files because two non-source languages are registered. The file for
# language "es" contains placeholders and must be completed.
translator_write(tr, temp_path)
translator_read(temp_path)
# Translations can be read individually.
translations_files <- translations_paths(tr, temp_dir)
translations_read(translations_files[["es"]])
translations_read(translations_files[["fr"]])
# This is rarely useful, but translations can also be exported individually.
# You may use this to add a new language, as long as it has an entry in the
# underlying Translator object (or file).
tr$set_native_languages(el = "Greek")
translations_files <- translations_paths(tr, temp_dir)
translations_write(tr, translations_files[["el"]], "el")
translations_read(file.path(temp_dir, "el.txt"))
Run the code above in your browser using DataLab