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

Export list of locales available, and a function to require an Array of locales #1041

Closed
Tracked by #2
niftylettuce opened this issue Sep 10, 2020 · 10 comments · Fixed by #1047
Closed
Tracked by #2

Export list of locales available, and a function to require an Array of locales #1041

niftylettuce opened this issue Sep 10, 2020 · 10 comments · Fixed by #1047
Labels

Comments

@niftylettuce
Copy link

Can you please export the list of locales available? This would be a much better approach than having to manually write it out or programmatically use fs.readdirSync to get the full list. It would also be nice to have an option to load all locales (require them all, or a list passed) at once.

@iamkun
Copy link
Owner

iamkun commented Sep 11, 2020

That sounds cool.
Maybe we could add a JSON output file in each npm release. like

root_path/locale.json
[
  {
    "key": "ca",
    "name": "English (Canada)"
  },
 {
    "key": "en-gb",
    "name": "English (United Kingdom)"
  }, 
  ...
]

@iamkun
Copy link
Owner

iamkun commented Sep 17, 2020

🎉 This issue has been resolved in version 1.8.36 🎉

The release is available on:

Your semantic-release bot 📦🚀

@iamkun
Copy link
Owner

iamkun commented Sep 17, 2020

we have one now https://unpkg.com/dayjs/locale.json

@blaze-nitd
Copy link

how to require them all?

@justingolden21
Copy link

+1 to requiring them all

My current code @blaze-nitd
// https://github.com/iamkun/dayjs/issues/1041
// https://unpkg.com/dayjs@1.10.7/locale.json
// commented out locales do not exist in current dayjs
import 'dayjs/locale/af';
import 'dayjs/locale/am';
import 'dayjs/locale/ar';
import 'dayjs/locale/az';
import 'dayjs/locale/be';
import 'dayjs/locale/bg';
import 'dayjs/locale/bi';
import 'dayjs/locale/bm';
import 'dayjs/locale/bn';
import 'dayjs/locale/bo';
import 'dayjs/locale/br';
import 'dayjs/locale/bs';
import 'dayjs/locale/ca';
import 'dayjs/locale/cs';
import 'dayjs/locale/cv';
import 'dayjs/locale/cy';
import 'dayjs/locale/da';
import 'dayjs/locale/de';
import 'dayjs/locale/dv';
import 'dayjs/locale/el';
import 'dayjs/locale/en';
import 'dayjs/locale/eo';
import 'dayjs/locale/es';
import 'dayjs/locale/eu';
import 'dayjs/locale/fa';
import 'dayjs/locale/fi';
import 'dayjs/locale/fo';
import 'dayjs/locale/fr';
import 'dayjs/locale/fy';
import 'dayjs/locale/ga';
import 'dayjs/locale/gd';
import 'dayjs/locale/gl';
// import 'dayjs/locale/gom';
import 'dayjs/locale/gu';
import 'dayjs/locale/he';
import 'dayjs/locale/hi';
import 'dayjs/locale/hr';
import 'dayjs/locale/ht';
import 'dayjs/locale/hu';
// import 'dayjs/locale/hy';
import 'dayjs/locale/id';
import 'dayjs/locale/is';
import 'dayjs/locale/it';
import 'dayjs/locale/ja';
import 'dayjs/locale/jv';
import 'dayjs/locale/ka';
import 'dayjs/locale/kk';
import 'dayjs/locale/km';
import 'dayjs/locale/kn';
import 'dayjs/locale/ko';
import 'dayjs/locale/ku';
import 'dayjs/locale/ky';
import 'dayjs/locale/lb';
import 'dayjs/locale/lo';
import 'dayjs/locale/lt';
import 'dayjs/locale/lv';
import 'dayjs/locale/me';
import 'dayjs/locale/mi';
import 'dayjs/locale/mk';
import 'dayjs/locale/ml';
import 'dayjs/locale/mn';
import 'dayjs/locale/mr';
import 'dayjs/locale/ms';
import 'dayjs/locale/mt';
import 'dayjs/locale/my';
import 'dayjs/locale/nb';
import 'dayjs/locale/ne';
import 'dayjs/locale/nl';
import 'dayjs/locale/nn';
// import 'dayjs/locale/oc';
// import 'dayjs/locale/pa';
import 'dayjs/locale/pl';
import 'dayjs/locale/pt';
import 'dayjs/locale/ro';
import 'dayjs/locale/ru';
import 'dayjs/locale/rw';
import 'dayjs/locale/sd';
import 'dayjs/locale/se';
import 'dayjs/locale/si';
import 'dayjs/locale/sk';
import 'dayjs/locale/sl';
import 'dayjs/locale/sq';
import 'dayjs/locale/sr';
import 'dayjs/locale/ss';
import 'dayjs/locale/sv';
import 'dayjs/locale/sw';
import 'dayjs/locale/ta';
import 'dayjs/locale/te';
import 'dayjs/locale/tet';
import 'dayjs/locale/tg';
import 'dayjs/locale/th';
import 'dayjs/locale/tk';
// import 'dayjs/locale/tl';
import 'dayjs/locale/tlh';
import 'dayjs/locale/tr';
import 'dayjs/locale/tzl';
import 'dayjs/locale/tzm';
// import 'dayjs/locale/ug';
import 'dayjs/locale/uk';
import 'dayjs/locale/ur';
import 'dayjs/locale/uz';
import 'dayjs/locale/vi';
// import 'dayjs/locale/x';
import 'dayjs/locale/yo';
import 'dayjs/locale/zh';
import 'dayjs/locale/et';

@titanism
Copy link
Contributor

const locales = require('dayjs/locale.json')
for (const locale of locales) {
  require(`dayjs/locale/${locale.key}`);
}

@titanism
Copy link
Contributor

Note that this approach won't work with ESM, instead you'd need to use dynamic imports or require them all manually.

@titanism
Copy link
Contributor

@iamkun can we publish dayjs/locale/index.js which would require/export all of the locales, and that way if you require('dayjs/locale') it would require all of them? Need an approach that works easily for CJS and ESM.

@titanism
Copy link
Contributor

v1.0.0 of dayjs-with-plugins now has all plugins and locales added out of the box, no need to use dayjs.extend nor dayjs.locale https://github.com/ladjs/dayjs-with-plugins

Ref: https://github.com/ladjs/dayjs-with-plugins/releases/tag/v1.0.0

@iamkun ping us if you add something that lets us easily require all locales and plugins! thanks for building + maintaining this 🙏

@titanism
Copy link
Contributor

titanism commented Jul 1, 2022

dayjs-with-plugins v1.0.1 is fixed, v1.0.0 is deprecated to anyone reading. Also v1.0.1 has ESM support too since we just did the simple approach and require'd everything at the top level.

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

Successfully merging a pull request may close this issue.

5 participants