fecha-locales

i18n locales for fecha.js

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
fecha-locales
0.2.18 years ago8 years agoMinified + gzip package size for fecha-locales in KB

Readme

  • 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-supporti18n support provided by fecha.js.
Installation Npm
+beginsrc bash
npm install --save fecha-locales
+endsrc
Browser
+beginsrc html
+endsrc
Usage
Npm Import/require the locale(s) you need, and put the one you want to use as fecha.i18n
+beginsrc javascript
import fr from 'fecha-locales/locales/fr';
fecha.i18n = fr;
+endsrc
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.~
+beginsrc javascript
// In html
// In app.js fecha.i18n = fechaLocales.;
+endsrc
Credits All the fecha-locales are built from https://github.com/moment/momentmoment.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/~ but it is present in moment-locales All the moment-locales couldn't get converted to fecha. So the ones which failed to convert are placed as ~fecha-locales/locales/~, but please don't use them. If you need a locale which is present as ~fecha-locales/locales/~, please do one of these:
  • 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 ~~ to see which properties you need to fix. For example, if you're fixing ~es~ locale, you need to look for these messages:
#+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/.js~ file - 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/.js~ file for clues. For ~es~ locale, following should fix the building of locale:
#+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/.js~ shall be there. - 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.