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

Adding a locale modifies the window object #1658

Closed
whatisaphone opened this issue Mar 8, 2019 · 0 comments
Closed

Adding a locale modifies the window object #1658

whatisaphone opened this issue Mar 8, 2019 · 0 comments

Comments

@whatisaphone
Copy link
Contributor

We're using react-datepicker in an app where the global context should not be modified. It looks like in v2, non-English locales won't work unless the locales are registered ahead of time on the global window object:

export function registerLocale(localeName, localeData) {
if (!window.__localeData__) {
window.__localeData__ = {};
}
window.__localeData__[localeName] = localeData;
}

This does not align with how date-fns itself handles locales. In their docs, they have this example, where the locale object is passed directly as an argument, without going through an indirection layer:

var distanceInWords = require('date-fns/distance_in_words')
// Require Esperanto locale
var eoLocale = require('date-fns/locale/eo')

var result = distanceInWords(
  new Date(2016, 7, 1),
  new Date(2015, 0, 1),
  {locale: eoLocale} // Pass the locale as an option
)
//=> 'pli ol 1 jaro'

The react-datepicker equivalent would look something like this:

import eoLocale from 'date-fns/locale/eo';
import DatePicker from 'react-datepicker';

const MyComponent = () => (
  <DatePicker
    locale={eoLocale}
  />
);

Allowing that usage would let you manage locales with all the flexibility that date-fns itself allows. Supporting it in a backwards-compatible way would (at first glance) require only one change, here, to something like this:

export function getLocaleObject(localeSpec) {
  if (typeof localeSpec === 'string')
    // Treat it as a locale name registered by registerLocale
    return window.__localeData__ ? window.__localeData__[localeSpec] : null;
  else
    // Treat it as a raw date-fns locale object
    return localeSpec;
}

Can you think of any reason not to support this?

@whatisaphone whatisaphone changed the title Support locales without requiring the global variable Adding a locale modifies the window object Mar 8, 2019
@aij aij closed this as completed in #1660 Mar 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant