Skip to content

A top-level locale option #384

Open
@ericemc3

Description

@ericemc3

Wherever English formatting appears by default, a simple possibility of taking into account any other language could/should be proposed.

E.g.: formatting of numbers or dates on the axes.

Suggestion: a high level locale property in a Plot specification, applying to the whole plot. By default, locale is set to "en".

This would work in a similar way to what has very usefully appeared in the Table component.

Note: vega-lite allows you to globally specify a locale for numbers and for dates:

vl.vega.timeFormatLocale(locale);
vl.vega.formatLocale(locale);

PS: Plot is a great library, very intellectually stimulating, and the 21 beautifully written articles in the https://observablehq.com/collection/@observablehq/plot collection should be read by all data scientists!

Activity

changed the title [-]A global locale option[/-] [+]A top-level locale option[/+] on May 12, 2021
mbostock

mbostock commented on May 12, 2021

@mbostock
Member

I propose this is a top-level Plot.plot option, i.e.,

Plot.plot({
  locale: "de",
  marks: 
})

The plot should then inherit this locale rather than the default "en-US". This is possibly a little tricky with things like Plot.formatWeekday since that function currently needs to be supplied the locale upon creation, whereas here we would need to somehow pass the plot’s locale into the format function. One way to do that might be to pass the plot’s locale to the format function instead:

function formatMonth(month = "short") {
  return (i, locale = "en-US") => {
    if (i != null && !isNaN(i = new Date(Date.UTC(2000, +i)))) {
      return i.toLocaleString(locale, {timeZone: "UTC", month});
    }
  };
}

(I think this would also be slower since we couldn’t reuse the same Intl.DateFormatInstance, but we could address that separately using memoization.)

In addition, we could do something like Plot.defaults:

plot = Plot.defaults({locale: "de"})
plot({marks: })
mbostock

mbostock commented on May 12, 2021

@mbostock
Member

And more specifically: let’s not use global mutation, e.g. Plot.setLocale(locale), since this makes it hard to support multiple locales on the page and introduces ordering problems where the behavior of Plot.plot will change before or after a call to Plot.setLocale.

Fil

Fil commented on May 13, 2021

@Fil
Contributor

This would also impact the automatic word "frequency", #395, and maybe other language elements.

(Regarding autodetection of "date"/"year" to omit the axis label, it is based on the field name so I would tend to not consider it in this context.)

rusosnith

rusosnith commented on May 2, 2022

@rusosnith

This would be very important for us spanish users! Date parse as mentioned for axis it's a must!

rusosnith

rusosnith commented on Jul 1, 2022

@rusosnith

MAybe this should be handled on notebook level? as in an Observable configuration itself?

that would be really handy

observablehq/feedback#411 (comment)

oluckyman

oluckyman commented on Jan 25, 2023

@oluckyman

Any suggestion how to set ticks locale now, while the locale option is not implemented?

I am aware that I can set scale.tickFormat, but don't want to copypaste the implementation of multi-scale time format which is used in d3 by default.
Is there a simpler option?

Fil

Fil commented on Jan 25, 2023

@Fil
Contributor

don't want to copypaste

why?

Is there a simpler option?

I don't think so. The only alternative I can think of is to work on implementing this in Plot, so it can be simpler in the future.

oluckyman

oluckyman commented on Jan 25, 2023

@oluckyman

why?

I think it's irrational. Observable + Plot usually work like magic and allow to stay in the flow longer, so any obstacle feels frustrating. But it's not a problem. I'll make my own multi-scale time format to reuse in Plots. Just wondered if there was a solution that I was not aware of.

rusosnith

rusosnith commented on Apr 10, 2024

@rusosnith

I'm back here to add more push to this. So sad to see other-than-english languages are left out of the Plot magic sad emoji

linked a pull request that will close this issue on Jun 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @Fil@mbostock@oluckyman@rusosnith@ericemc3

      Issue actions

        A top-level locale option · Issue #384 · observablehq/plot