i18n locales for fecha.js
npm install fecha-locales* fecha-locales
~fecha-locales~ provides different locales to be used for internationalization support in fecha. All the locale modules provided by ~fecha-locales~ can be used in [[https://github.com/taylorhakes/fecha#i18n-support][i18n support provided by fecha.js]].
** Installation
* Npm
#+begin_src bash
npm install --save fecha-locales
#+end_src
* Browser
#+begin_src html
#+end_src
** Usage
* Npm
Import/require the locale(s) you need, and put the one you want to use as fecha.i18n
#+begin_src javascript
import fr from 'fecha-locales/locales/fr';
fecha.i18n = fr;
#+end_src
* Browser
For browser,
- Include fecha-locales before your app (i.e script that is going to use fecha-locales).
- Doing so will export ~window.fechaLocales~ object
- You can add multiple locales
- All the locales you create will be added to ~window.fechaLocales.
- In your app, you can now do ~fecha.i18n = fechaLocales.
#+begin_src javascript
// In html
// In app.js
fecha.i18n = fechaLocales.
#+end_src
** Credits
All the fecha-locales are built from [[https://github.com/moment/moment][moment.js]]. All credit for collecting the locales go to momentjs. This module uses a small build script to convert moment-locales to fecha friendly locales (i.e which can be put simply as value of ~fecha.i18n~)
** FAQ
* The locale I need is not present in ~fecha-locales/locales/
All the moment-locales couldn't get converted to fecha. So the ones which failed to convert are placed as ~fecha-locales/locales/_
- Create an issue and I'll fix it
- Create a pull request with the fix. To create a fix:
- Clone/fork this repository, cd to it, and install npm dependencies
#+begin_src bash
git clone https://github.com/channikhabra/fecha-locales.git
cd fecha-locales
npm install
#+end_src
- Run build-locales script
#+begin_src javascript
npm run build-locales
#+end_src
- Now you'll see some messages in the console which will tell you which properties are facing problems. Search for the ~
#+begin_src bash
Invalid value for monthsShort of "es.js". Please fix it manually.
Invalid value for DoFn of "es.js". Please fix it manually.
#+end_src
- Create ~fecha-locales/locale-builder/fixers/
- Now ~module.exports~ an object from this file, which should contain the keys which are causing problems and put the fixes for them as their values. Make sure you first check ~fecha-locales/locales/_
#+begin_src javascript
// fecha-locales/locale-builder/fixers/es.js
module.exports = {
DoFn: (number) => {
// in moment/locales/es, this is given as %dº, but fecha need it to be a function, so we make it a function instead
return number + 'º';
},
monthsShort: ['enero', 'feb.', 'marzo', 'abr.', 'mayo', 'jun.', 'jul.', 'agosto', 'sept.', 'oct.', 'nov.', 'dic.']
};
#+end_src
- Now run ~npm run build-locales~ again, and ~fecha-locales/locales/
- Create a PR (Thanks for creating the PR, you're awesome!)
* Why did you use esprima and friends to convert moment locales to fecha? Wasn't there an easier way of doing so?
Not sure about easier, but yes this can be done in other ways. I was feeling fancy so I went with esprima. Besides, this is the most explicit and flexible way of conversion imo.