Title: | Internationalisation on the Fly |
---|---|
Description: | Internationalisation ('i18n') support for R, switching languages at runtime, based on the 'i18next' 'JavaScript' library. This provides an alternative approach to internationalisation than supported directly by R. |
Authors: | Rich FitzJohn [aut, cre], i18next [cph], Imperial College of Science, Technology and Medicine [cph] |
Maintainer: | Rich FitzJohn <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.1 |
Built: | 2024-11-25 06:10:33 UTC |
Source: | https://github.com/reside-ic/traduire |
Create a new translator object
i18n(resources, ..., options = NULL)
i18n(resources, ..., options = NULL)
resources |
Path to a json file containing translation
resources. If given in this way, then on-demand translation
loading (via |
... |
Named options passed to |
options |
Options object passed to |
Note that the argument list here will change. The only part of this that we consider stable is that the first argument will represent a resource bundle.
path <- system.file("examples/simple.json", package = "traduire") obj <- traduire::i18n(path) obj$t("hello", language = "fr")
path <- system.file("examples/simple.json", package = "traduire") obj <- traduire::i18n(path) obj$t("hello", language = "fr")
Find likely translation errors using static source code analysis.
We look into a series of R source files and identify all calls to
t_
and examine the key and interpolation data used within
these calls.
lint_translations(path, obj, language = NULL, root = NULL, title = NULL) lint_translations_package(root, language = NULL)
lint_translations(path, obj, language = NULL, root = NULL, title = NULL) lint_translations_package(root, language = NULL)
path |
Path to the translations to test. This can be a
character vector of file names and directory names - directory
names are expanded (non-recursively) to find all |
obj |
A translator object, or 'NULL'. If 'NULL', then the report will indicate only strings that might be translated vs bare strings, rather than check the likely success of translation. |
language |
Optional language to use as the language to lint. Each language must be at present linted separately |
root |
A root directory, below which the files in |
title |
An optional title that will be used in reporting |
This will expand in future and is currently quite limited.
Currently, only calls to t_()
are inspected - not calls to
$t()
in a translator object, not calls to $replace
etc. The package
and name
arguments to t_()
are also currently ignored, which will lead to spurious errors. A
version of this that automatically works for packages will also be
written. The key and interpolation data must (currently) be
literals - you cannot save the key or data as a variable and pass
that by variable name.
The function lint_translations_package
is a wrapper around
lint_translations
to make working with a package a bit
easier. It's a bit flakey at the moment because the translator
will be loaded from the currently installed package, but the usage
will be checked in the source tree. This will be addressed in a
future version (see reside-90)
A lint_translations
object, which can be passed
through to lint_translations_report
Report on translation linting. At present this produces a single-page html report that can be opened in a web browser. Future versions will include reporting to the terminal.
lint_translations_report(x, file = tempfile(fileext = ".html"))
lint_translations_report(x, file = tempfile(fileext = ".html"))
x |
A |
file |
File to save the report in (default is to use a
temporary file with a file extension of |
This function is called for the side effect of viewing a report. It will return (invisibly) the path to the produced report.
Takes a named set of options and optionally an already created options object. If both are set, named options will overwrite any options in the object.
traduire_options(..., options = NULL)
traduire_options(..., options = NULL)
... |
Named options language The default language for the translation default_namespace The default namespace to use. If not
given, then debug Logical, indicating if i18next's debug output should
be turned on. This will result in lots of output via
resource_pattern A pattern to use for on-demand loading of
translation resources. Only works if namespaces A vector of namespaces to load. Namespaces not
listed here may not be loaded as expected (use languages A vector of languages to preload. You can
always add additional languages using the fallback The fallback language to use. The options here
are to use a character string (a single fallback to use for all
languages), a character vector (a series of languages to use in
turn, listed from first to try to last to try) or a named list
of language-fallback mappings, e.g., escape Logical, indicating if the translation output should be, by default, escaped (see the i18next interpolation documentation). The i18next implementation is to prevent xss attacks, and so is disabled by default in traduire. |
options |
traduire_options object |
These options are passed to i18next.
A 'traduire_options' object
opts <- traduire::traduire_options() opts <- traduire::traduire_options(language = "fr") opts <- traduire::traduire_options(default_namespace = "tr", options = opts)
opts <- traduire::traduire_options() opts <- traduire::traduire_options(language = "fr") opts <- traduire::traduire_options(default_namespace = "tr", options = opts)
Register a translator within the traduire
package, and use
it directly. This will allow your code to access translations via
translator_translate
(or more concisely t_
) without
having to pass around a translation object. If called from
package code (and assuming a single translator per package) then
the name
argument can be omitted and will be automatically
converted into package:<packagename
).
translator_register(resources, ..., name = NULL) translator_unregister(name = NULL) translator_translate(..., name = NULL, package = NULL) t_(..., name = NULL, package = NULL) translator(name = NULL, package = NULL) translator_set_language(language, name = NULL, package = NULL) translator_list()
translator_register(resources, ..., name = NULL) translator_unregister(name = NULL) translator_translate(..., name = NULL, package = NULL) t_(..., name = NULL, package = NULL) translator(name = NULL, package = NULL) translator_set_language(language, name = NULL, package = NULL) translator_list()
resources |
Path to a json file containing translation
resources. If given in this way, then on-demand translation
loading (via |
... |
For |
name |
Optional name for the translator. This should be used
only when not using this interface from a package (e.g., from a
shiny application). If using from a package you can omit both
|
package |
Optional name for the package to find a translator
in. This cannot be provided for |
language |
Language to use, passed through to
|
The intention is that this would typically be called from
.onLoad
, something like:
.onLoad <- function(...) { path <- system.file("traduire.json", package = "hello", mustWork = TRUE) traduire::translator_register(path, "en") }
and then used from that package's code as
traduire::t_("key")
The language option for this translator can be changed by
traduire::translator_set_language("es")
Every package's translator object is isolated from every other
package, and if the traduire
functions are called from
your package code, then the correct translator should be found
automatically.
If you need to get a translation for another package, you should
use package
argument, for example:
traduire::t_("key", package = "other")
You can change the language in another package (e.g., using
traduire::change_language("en", package = "other")
) but
should be careful to reset this using the returned reset
function.
It is not possible to unregister a translator in another package, or to overwrite one.
Translators provided in other packages will be listed by
traduire::translator_list
with the prefix package:
(e.g., package:other
) however, you should not access them
directly using name = "package:other"
.
path <- system.file("examples/simple.json", package = "traduire") traduire::translator_register(path, name = "myexample") traduire::t_("hello", language = "fr", name = "myexample") "myexample" %in% traduire::translator_list() traduire::translator_unregister("myexample")
path <- system.file("examples/simple.json", package = "traduire") traduire::translator_register(path, name = "myexample") traduire::t_("hello", language = "fr", name = "myexample") "myexample" %in% traduire::translator_list() traduire::translator_unregister("myexample")