jbkunst / highcharter

R wrapper for highcharts
http://jkunst.com/highcharter/
Other
718 stars 147 forks source link

setting language options #724

Closed GregorDeCillia closed 2 years ago

GregorDeCillia commented 2 years ago

Hello,

I am currently writing an R package which uses highcharter extensively. Let me firs say a big thank you for all the hard work you put into this project.

In my package, I create charts in german or english based on a parameter supplied by the user

myPackage::my_barchart(data, ..., language = c("english", "german")) {
  language <- match.arg(language)
  # ...
  highcharter::hchart(data, type = "bar")
}

For this, I need to update the language options https://api.highcharts.com/highcharts/lang to display certain parts of the chart differently. My current strategy is to overwrite options("highcharter.lang") based on this code. https://github.com/jbkunst/highcharter/blob/f70f8e8982f276b0ed7ae63eeb47f3f1fbdb96aa/R/zzz.R#L34-L79

This means that depending on the language parameter the option will be overwritten with different values

options(highcharter.language = switch(
  language,
  english = english_options,
  german = german_options
)))

This works fine but I wanted to ask if this is the recommended approach. It kind of feels like monkeypatching and I'm worried about backwards compability.

jbkunst commented 2 years ago

Hi @GregorDeCillia

That's the way. Somewhere must be the dirty work.

Could you share your work german_options? I would like to create a R/langs.R file with a function to change and support other languages.

GregorDeCillia commented 2 years ago

Thank you @jbkunst ! I would be very happy about an exported function hc_global_language() that allows to attach the options to the htmlwidgets object.

Here is what I'm using at the moment. The project is sill under development and I will post updates as they come along.

set_language_options_german <- function() {
  options(highcharter.lang = list(
    contextButtonTitle = "Chart context menu",  ## TODO
    decimalPoint = ",",
    downloadCSV = "csv Datei herunterladen",
    downloadJPEG = "jpeg Bilddatei herunerladen",
    downloadPDF = "pdf Dokument herunterladen",
    downloadPNG = "png Bilddatei herunterladen",
    downloadSVG = "svg Vektorgrafik herunterladen",
    downloadXLS = "xls herunterladen",
    drillUpText = "\u25C1 Zurück zu {series.name}",
    exitFullscreen = "Vollbildmodus verlassen",
    exportData = list(
      annotationHeader = "Anmerkungen",
      categoryDatetimeHeader = "Datum",
      categoryHeader = "Kategorie"
    ),
    hideData = "Tabelle ausblenden",
    invalidDate = NULL,
    loading = "Landen...",
    months = c(
      "J\u00e4nner", "Februar", "M\u00e4rz", "April", "Mai", "Juni", "Juli",
      "August", "September", "Oktober", "November", "Dezember"
    ),
    # navigation
    noData = "Keine Daten verfügbar",
    numericSymbolMagnitude = 1000,
    numericSymbols = c("Tsd", "Mio", "Mrd", "Bio", "Brd", "E"),
    printChart = "Grafik ausdrucken",
    resetZoom = "Zoom zurücksetzen",
    resetZoomTitle = "Reset zoom level 1:1",  ## TODO
    shortMonths = c(
      "J\u00e4n", "Feb", "M\u00e4r", "Apr", "Mai", "Jun",
      "Jul", "Aug", "Sep", "Okt", "Nov", "Dez"
    ),
    shortWeekdays = c("Sa", "So", "Mo", "Di", "Mi", "Do", "Fr"),
    thousandsSep = " ",
    viewData = "Tabelle anzeigen",
    viewFullscreen = "Im Vollbildmodus anzeigen",
    weekdays = c(
      "Sonntag", "Montag", "Dienstag", "Mittwoch",
      "Donnerstag", "Freitag", "Samstag"
    ),
    ## more options
    rangeSelectorFrom = "Von",
    rangeSelectorTo = "Bis",
    rangeSelectorZoom = "Zeige" ## opinionated
  ))
}