Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A top-level locale option #384

Open
ericemc3 opened this issue May 12, 2021 · 10 comments · May be fixed by #2079
Open

A top-level locale option #384

ericemc3 opened this issue May 12, 2021 · 10 comments · May be fixed by #2079
Labels
enhancement New feature or request

Comments

@ericemc3
Copy link

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!

@Fil Fil added the enhancement New feature or request label May 12, 2021
@mbostock mbostock changed the title A global locale option A top-level locale option May 12, 2021
@mbostock
Copy link
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
Copy link
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
Copy link
Contributor

Fil commented May 13, 2021

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
Copy link

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

@Fil
Copy link
Contributor

Fil commented May 7, 2022

@rusosnith
Copy link

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

that would be really handy

observablehq/feedback#411 (comment)

@oluckyman
Copy link

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
Copy link
Contributor

Fil commented Jan 25, 2023

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
Copy link

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants