bitspook / fecha-locales

Locales for internationization support in fecha.js

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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 i18n support provided by fecha.js.

Installation

Npm

npm install --save fecha-locales

Browser

<script src='/path/to/fecha-locales/<locale-name>.js'></script>

Usage

Npm

Import/require the locale(s) you need, and put the one you want to use as `fecha.i18n`

import fr from 'fecha-locales/locales/fr';

fecha.i18n = fr;

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.<localeName>
  • In your app, you can now do fecha.i18n = fechaLocales.<localeName>
// In html
<script src='/path/to/fecha-locales/<locale-name>.js/'></script>
<script src='fecha.js/'></script>
<script src='app.js/'></script>

// In app.js
fecha.i18n = fechaLocales.<locale-name>;

Credits

All the fecha-locales are built from 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/<locale-name> 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/_<locale-name>, but please don’t use them. If you need a locale which is present as fecha-locales/locales/_<locale-name>, 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
      git clone https://github.com/channikhabra/fecha-locales.git
      cd fecha-locales
      npm install
              
    • Run build-locales script
      npm run build-locales
              
    • Now you’ll see some messages in the console which will tell you which properties are facing problems. Search for the <locale-name.js> to see which properties you need to fix. For example, if you’re fixing es locale, you need to look for these messages:
      Invalid value for `monthsShort` of "es.js". Please fix it manually.
      Invalid value for `DoFn` of "es.js". Please fix it manually.
              
    • Create fecha-locales/locale-builder/fixers/<locale-name>.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/_<locale-name>.js file for clues. For es locale, following should fix the building of locale:
      // 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.']
      };
              
    • Now run npm run build-locales again, and fecha-locales/locales/<locale-name>.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.

About

Locales for internationization support in fecha.js


Languages

Language:JavaScript 99.8%Language:HTML 0.2%