| Title: | Shiny Applications Internationalization |
|---|---|
| Description: | It provides easy internationalization of Shiny applications. It can be used as standalone translation package to translate reports, interactive visualizations or graphical elements as well. |
| Authors: | Jakub Nowicki [cre, aut], Dominik KrzemiĆski [aut], Krystian Igras [aut], Jakub Sobolewski [aut], Appsilon Sp. z o.o. [cph] |
| Maintainer: | Jakub Nowicki <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.3.0 |
| Built: | 2026-06-07 09:59:04 UTC |
| Source: | https://github.com/appsilon/shiny.i18n |
Auxiliary shiny.i18n function that searches for all key expressions (e.g.
surrounded by i18n$t() tag in the script).
create_translation_file(path, type = "json", handle = "i18n", output = NULL)create_translation_file(path, type = "json", handle = "i18n", output = NULL)
path |
character with path of the file that needs to be inspected for key translations |
type |
type of the example output file with translations, either "json" or "csv" |
handle |
name of |
output |
if NULL (default) the output will be saved with a default file name ("translation.json" for JSON and "translate_lang.csv" for CSV) |
Creates new i18n Translator object
init_i18n( translation_csvs_path = NULL, translation_json_path = NULL, translation_csv_config = NULL, automatic = FALSE )init_i18n( translation_csvs_path = NULL, translation_json_path = NULL, translation_csv_config = NULL, automatic = FALSE )
translation_csvs_path |
character with path to folder containing csv translation files. See more in Details. |
translation_json_path |
character with path to JSON translation file. See more in Details. |
translation_csv_config |
character with path to configuration file for csv option. |
automatic |
logical flag, indicating if i18n should use an automatic translation API. |
i18n object
## Not run: i18n <- init_i18n(translation_csvs_path = "../csvdata/") i18n <- init_i18n(translation_json_path = "translations.json") ## End(Not run)## Not run: i18n <- init_i18n(translation_csvs_path = "../csvdata/") i18n <- init_i18n(translation_json_path = "translations.json") ## End(Not run)
This is wrapper for gl_translate function from googleLanguageR
package.
translate_with_google_cloud(txt_to_translate, target_lang)translate_with_google_cloud(txt_to_translate, target_lang)
txt_to_translate |
character with text to translate |
target_lang |
character with language code |
Translator R6 Class
Translator R6 Class
This creates shinny.i18n Translator object used for translations.
Now you can surround the pieces of the text you want to translate by
one of the translate statements (ex.: Translator$t("translate me")).
Find details in method descriptions below.
new()
Initialize the Translator with data
Translator$new( translation_csvs_path = NULL, translation_json_path = NULL, translation_csv_config = NULL, separator_csv = ",", automatic = FALSE )
translation_csvs_pathcharacter with path to folder containing csv
translation files. Files must have "translation_" prefix, for example:
translation_<LANG-CODE>.csv.
translation_json_pathcharacter with path to JSON translation file. See more in Details.
translation_csv_configcharacter with path to configuration file for csv option.
separator_csvseparator of CSV values (default ",")
automaticlogical flag, indicating if i18n should use an automatic translation API.
get_languages()
Get all available languages
Translator$get_languages()
get_translations()
Get whole translation matrix
Translator$get_translations()
get_key_translation()
Get active key translation
Translator$get_key_translation()
get_translation_language()
Get current target translation language
Translator$get_translation_language()
translate()
Translates 'keyword' to language specified by 'set_translation_language'
Translator$translate(keyword, session = shiny::getDefaultReactiveDomain())
keywordcharacter or vector of characters with a word or expression to translate
sessionShiny server session (default: current reactive domain)
t()
Wrapper to translate method.
Translator$t(keyword, session = shiny::getDefaultReactiveDomain())
keywordcharacter or vector of characters with a word or expression to translate
sessionShiny server session (default: current reactive domain)
set_translation_language()
Specify language of translation. It must exist in 'languages' field.
Translator$set_translation_language(transl_language)
transl_languagecharacter with a translation language code
parse_date()
Parse date to format described in 'cultural_date_format' field in config.
Translator$parse_date(date)
datedate object to format
parse_number()
Numbers parser. Not implemented yet.
Translator$parse_number(number)
numbernumeric or character with number
character with number formatting
automatic_translate()
Translates 'keyword' to language specified by 'set_translation_language' using cloud service 'api'. You need to set API settings first.
Translator$automatic_translate(keyword, api = "google")
keywordcharacter or vector of characters with a word or expression to translate
apicharacter with the name of the API you want to use. Currently
supported: google.
at()
Wrapper to automatic_translate method
Translator$at(keyword, api = "google")
keywordcharacter or vector of characters with a word or expression to translate
apicharacter with the name of the API you want to use. Currently
supported: google.
use_js()
Call to wrap translation in span object. Used for browser-side translations.
Translator$use_js()
clone()
The objects of this class are cloneable with this method.
Translator$clone(deep = FALSE)
deepWhether to make a deep clone.
## Not run: i18n <- Translator$new(translation_json_path = "translation.json") # translation file i18n$set_translation_language("it") i18n$t("This text will be translated to Italian") ## End(Not run) # Shiny example if (interactive()) { library(shiny) library(shiny.i18n) #to run this example make sure that you have a translation file #in the same path i18n <- Translator$new(translation_json_path = "examples/data/translation.json") i18n$set_translation_language("pl") ui <- fluidPage( h2(i18n$t("Hello Shiny!")) ) server <- function(input, output) {} shinyApp(ui = ui, server = server) }## Not run: i18n <- Translator$new(translation_json_path = "translation.json") # translation file i18n$set_translation_language("it") i18n$t("This text will be translated to Italian") ## End(Not run) # Shiny example if (interactive()) { library(shiny) library(shiny.i18n) #to run this example make sure that you have a translation file #in the same path i18n <- Translator$new(translation_json_path = "examples/data/translation.json") i18n$set_translation_language("pl") ui <- fluidPage( h2(i18n$t("Hello Shiny!")) ) server <- function(input, output) {} shinyApp(ui = ui, server = server) }
It sends a message to session object to update the language in UI elements.
update_lang(language, session = shiny::getDefaultReactiveDomain())update_lang(language, session = shiny::getDefaultReactiveDomain())
language |
character with language code |
session |
Shiny server session (default: current reactive domain) |
usei18n
This is an auxiliary function needed to monitor the state of the UI for live language translations.
usei18n(translator)usei18n(translator)
translator |
shiny.i18 Translator object |
if (interactive()) { library(shiny) library(shiny.i18n) # for this example to run make sure that you have a translation file # in the same path i18n <- Translator$new(translation_json_path = "translation.json") i18n$set_translation_language("en") ui <- fluidPage( usei18n(i18n), actionButton("go", "GO!"), h2(i18n$t("Hello Shiny!")) ) server <- shinyServer(function(input, output, session) { observeEvent(input$go,{ update_lang("pl") }) }) shinyApp(ui = ui, server = server) }if (interactive()) { library(shiny) library(shiny.i18n) # for this example to run make sure that you have a translation file # in the same path i18n <- Translator$new(translation_json_path = "translation.json") i18n$set_translation_language("en") ui <- fluidPage( usei18n(i18n), actionButton("go", "GO!"), h2(i18n$t("Hello Shiny!")) ) server <- shinyServer(function(input, output, session) { observeEvent(input$go,{ update_lang("pl") }) }) shinyApp(ui = ui, server = server) }