Skip to content

Commit 7ecd204

Browse files
authored
Merge pull request #1271 from ropensci/locale
add locale arg to config() which automatically adds relevant script, …
2 parents 94a0b80 + f77f952 commit 7ecd204

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+176
-13
lines changed

NEWS.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* New "special arguments" `stroke`, `strokes`, `alpha_stroke`, `span`, and `spans` were added for easier control over the stroke (i.e., outline) appearance of various (filled) graphical marks. For an overview, see the **sf** blog post linked to in the bullet point above and the new package demos (list all demos with `demo(package = "plotly")`).
88
* The selection (i.e., linked-brushing) mode can now switch from 'transient' to 'persistent' by holding the 'shift' key. It's still possible to _force_ persistent selection by setting `persistent = TRUE` in `highlight()`, but `persistent = FALSE` (the default) is now recommended since it allows one to switch between [persistent/transient selection](https://plotly-book.cpsievert.me/linking-views-without-shiny.html#transient-versus-persistent-selection) in the browser, rather than at the command line.
99
* The new `partial_bundle()` function makes it easy to leverage [partial bundles of plotly.js](https://github.com/plotly/plotly.js#partial-bundles) for reduced file sizes and faster render times.
10+
* The `config()` function gains a `locale` argument for easily changing localization defaults (see #1270). This makes it possible localize date axes, and in some cases, modebar buttons (see #1270)
1011
* One may now inform `ggplotly()` about the relevant **shiny** output size via `session$clientData`. This ensures `ggplotly()` sizing is closer to **ggplot2** sizing, even on window resize. For an example, run `plotly_example("shiny", "ggplotly_sizing")`.
1112
* Instead of an error, `ggplotly(NULL, "message")` and `plotly_build(NULL, "message")` now returns `htmltools::div("message")`, making it easier to relay messages in shiny when data isn't yet ready to plot (see #1116)
1213
* The `animation_button()` function gains a `label` argument, making it easier to control the label of an animation button generated through the `frame` API (see #1205).

R/layout.R

+30-2
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,42 @@ rangeslider <- function(p, start = NULL, end = NULL, ...) {
9090
#' \url{https://github.com/plotly/plotly.js/blob/master/src/plot_api/plot_config.js}
9191
#' @param collaborate include the collaborate mode bar button (unique to the R pkg)?
9292
#' @param cloud include the send data to cloud button?
93+
#' @param locale locale to use. See [here](https://github.com/plotly/plotly.js/tree/master/dist#to-include-localization) for more info.
9394
#' @author Carson Sievert
9495
#' @export
9596
#' @examples
9697
#'
97-
#' config(plot_ly(), displaylogo = FALSE, collaborate = FALSE)
98+
#' today <- Sys.Date()
99+
#' x <- seq.Date(today, today + 360, by = "day")
100+
#' p <- plot_ly(x = x, y = rnorm(length(x))) %>%
101+
#' add_lines()
102+
#'
103+
#' # remove the plotly logo and collaborate button from modebar
104+
#' config(p, displaylogo = FALSE, collaborate = FALSE)
105+
#'
106+
#' # japanese
107+
#' config(p, locale = "ja")
108+
#' # german
109+
#' config(p, locale = "de")
110+
#' # swiss-german
111+
#' config(p, locale = "de-CH")
112+
#' # spanish
113+
#' config(p, locale = "es")
114+
#' # french
115+
#' config(p, locale = "fr")
116+
#' # chinese
117+
#' config(p, locale = "zh-CN")
98118
#'
99119

100-
config <- function(p, ..., collaborate = TRUE, cloud = FALSE) {
120+
config <- function(p, ..., collaborate = TRUE, cloud = FALSE, locale = NULL) {
121+
122+
if (!is.null(locale)) {
123+
p$dependencies <- c(
124+
p$dependencies,
125+
list(locale_dependency(locale))
126+
)
127+
p$x$config$locale <- locale
128+
}
101129

102130
p$x$config <- modify_list(p$x$config, list(...))
103131

R/plotly.R

+35
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,41 @@ plotlyHtmlwidgetsCSS <- function() {
440440
)
441441
}
442442

443+
locale_dependency <- function(locale) {
444+
if (!is.character(locale) || length(locale) != 1) {
445+
stop("locale must be a character string (vector of length 1)", call. = FALSE)
446+
}
447+
448+
locale_dir <- depPath("plotlyjs", "locales")
449+
locales_all <- sub("\\.js$", "", list.files(locale_dir))
450+
if (!tolower(locale) %in% locales_all) {
451+
stop(
452+
"Invalid locale: '", locale, "'.\n\n",
453+
sprintf("Supported locales include: '%s'", paste(locales_all, collapse = "', '")),
454+
call. = FALSE
455+
)
456+
}
457+
458+
459+
# some locales rely on a base/main locale (e.g. de-CH relies on de)
460+
# https://codepen.io/etpinard/pen/pKvLVX?editors=1010
461+
scripts <- paste0(locale, ".js")
462+
if (grepl("-", locale)) {
463+
locale_main <- strsplit(locale, "-")[[1]][1]
464+
if (locale_main %in% locales_all) {
465+
scripts <- c(scripts, paste0(locale_main, ".js"))
466+
}
467+
}
468+
469+
htmltools::htmlDependency(
470+
name = paste0("plotly-locale-", locale),
471+
version = plotlyMainBundle()$version,
472+
src = list(file = locale_dir),
473+
script = scripts,
474+
all_files = FALSE
475+
)
476+
}
477+
443478
#' Remove TypedArray polyfill
444479
#'
445480
#' By default, plotly.js' TypedArray polyfill is included as a dependency, so

inst/htmlwidgets/lib/plotlyjs/locales/af.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

inst/htmlwidgets/lib/plotlyjs/locales/am.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

inst/htmlwidgets/lib/plotlyjs/locales/ar-dz.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

inst/htmlwidgets/lib/plotlyjs/locales/ar-eg.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

inst/htmlwidgets/lib/plotlyjs/locales/ar.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

inst/htmlwidgets/lib/plotlyjs/locales/az.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

inst/htmlwidgets/lib/plotlyjs/locales/bg.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

inst/htmlwidgets/lib/plotlyjs/locales/bs.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

inst/htmlwidgets/lib/plotlyjs/locales/ca.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

inst/htmlwidgets/lib/plotlyjs/locales/cs.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

inst/htmlwidgets/lib/plotlyjs/locales/da.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

inst/htmlwidgets/lib/plotlyjs/locales/de-ch.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

inst/htmlwidgets/lib/plotlyjs/locales/de.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)