js-joda/js-joda-locale

Is plugin registration one time?

Closed this issue · 3 comments

I may be doing something wrong, but I kind of expected that once I register the locale plugin that it would be registered for the lifetime of my (browser spa) application?

Package versions:

  • js-joda@1.8.2
  • js-joda-timezone@1.3.0
  • js-joda-locale@1.1.0-pre3

I am working with the en-us prebuilt packge bundled with the locale plugin.

The following code at app startup behaves as expected:

import { use as jsJodaUse, LocalDate } from "js-joda";
import "js-joda-timezone"
import * as jsJodaLocale from "js-joda-locale"; // I have bundled the prebuilt locale as js-joda-locale

const locale: any = jsJodaLocale;
const { DateTimeFormatter, Locale } = jsJodaUse(locale);
let result = LocalDate.now().format((DateTimeFormatter.ofPattern("dd MMMM yyyy") as any).withLocale((Locale as any).US));
console.log(result);

However else where in my app, after the above startup code has executed if I do the following I get an empty string result and it seems like Locale is not known - which is different behavior to if I had never registered the plugin, in which case it would throw with Pattern using (localized) text not implemented, use js-joda-locale plugin!

import { DateTimeFormatter, LocalDate, Locale } from 'js-joda';

...
function format(value: LocalDate) {
  const formatter = (DateTimeFormatter.ofPattern("dd MMMM yyyy") as any).withLocale((Locale as any).US);
  return value.format(formatter);
}

Is the way I am specifying the Locale incorrect in the second sample? Or is there something else I am doing that is wrong?

I can use the following module to get around this issue, but I still feel like maybe I'm doing it wrong?

import { use as jsJodaUse } from "js-joda";
import "js-joda-timezone"
import * as jsJodaLocale from "js-joda-locale";

const locale: any = jsJodaLocale;
const { DateTimeFormatter, Locale } = jsJodaUse(locale);

export { DateTimeFormatter, Locale };

Whenever I need a formatter and locale I import them from this module, instead of directly from js-joda

Hi @simonfox thanks for reporting... sorry for my late answer.

What you are doing in your second seems to be correct, though a lttle confusing... pithu changed the way our plugin mechanism is done for js-joda-timezone, i think we will try to get this into js-joda-locale as well, but for now, this looks ok to me :)

i'll close this, feel free to reopen if you have further questions

Cheers, Pattrick

ok cool thanks @phueper