dayjs with react-native - how to load all dayjs locales?

Viewed 68

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?

1 Answers

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

Related