1

Tried to import dynamically locale following this answer.

import(`dayjs/locale/${currentLocale}`)
    .then(() => {
        dayjs.locale(currentLocale);
    });

Got the error:

Invalid call at line 26: import("dayjs/locale/" + currentLocale)

I guess it more the "web way" - Following this I understand that I need to load all dayjs locales in compile time.
How do I do that?

chenop
  • 4,743
  • 4
  • 41
  • 65

1 Answers1

1

I believe you can can do something like

const locale = DeviceInfo.getDeviceLocale()
dayjs.locale(locale)

and you'll need to import the needed locales in order for them to be included in the bundle

import 'dayjs/locale/te'
import 'dayjs/locale/en'
...etc

this requires using react-native-device-info, you can try to find other ways of getting the locale tho and then set the locale

RodSar
  • 1,211
  • 1
  • 4
  • 14
  • Thanks! the thing is that by default dayjs comes with only 'en' locale - I want to load all dayjs locales prior to choosing the specific one I need – chenop Aug 18 '22 at 10:19
  • oh then I think you'd have to import all of the needed locales so that they get included in the js bundle – RodSar Aug 18 '22 at 11:35
  • thanks - the question is how should I do that? – chenop Aug 18 '22 at 12:42
  • oh, that's just a matter of adding this inside your app ``` import 'dayjs/locale/te' import 'dayjs/locale/en' ...etc ``` – RodSar Aug 18 '22 at 13:47
  • 1
    Thanks! isnt there a syntax something like: import * as days from dayjs/locale/* – chenop Aug 18 '22 at 13:51
  • I'm not sure depends on how the locales are being exported, i came across this discussion, you might be interested in the last couple of comments https://github.com/iamkun/dayjs/issues/1041 – RodSar Aug 18 '22 at 13:58