0

I have my moment date in English. I'm trying to add moment.locale to have my date in french but it doesn't work. should have created a variable to do that? Or just add moment.locale() somewhere ?

 <View style={{flex: 1}}>
        <Text style={{color: 'white', textAlign: 'center', marginTop:20}}>
              
              {moment(element.dateMatch).format('LLLL')}
            </Text>
        </View>

with this code i have the date in english .

Sarun UK
  • 6,210
  • 7
  • 23
  • 48
Wendy Montagnon
  • 155
  • 1
  • 10

3 Answers3

5

This is what I have at the top of the file where I initialize my locales

import moment from 'moment';
import 'moment/locale/fr';
import 'moment/locale/nl';

Each supported language should be explicitly included, as react-native bundles are completely built upfront.

A little bit further the locale can be activated by setting it to fr (or any other imported locale):

moment.locale('fr');
Thomas
  • 7,933
  • 4
  • 37
  • 45
1

You need to do the following, import your locale on app initialisation or wherever you need it.

there is two methods one for older version and one for newer

// From 2.8.1 onward
moment.locale(String);
moment.locale(String[]);
moment.locale(String, Object);

// Deprecated in 2.8.1
moment.lang(String);
moment.lang(String[]);
moment.lang(String, Object);

then you need to import locales, related to your language. I will add an example for french language

moment.locale('fr', {
    months : 'janvier_février_mars_avril_mai_juin_juillet_août_septembre_octobre_novembre_décembre'.split('_'),
    monthsShort : 'janv._févr._mars_avr._mai_juin_juil._août_sept._oct._nov._déc.'.split('_'),
    monthsParseExact : true,
    weekdays : 'dimanche_lundi_mardi_mercredi_jeudi_vendredi_samedi'.split('_'),
    weekdaysShort : 'dim._lun._mar._mer._jeu._ven._sam.'.split('_'),
    weekdaysMin : 'Di_Lu_Ma_Me_Je_Ve_Sa'.split('_'),
    weekdaysParseExact : true,
    longDateFormat : {
        LT : 'HH:mm',
        LTS : 'HH:mm:ss',
        L : 'DD/MM/YYYY',
        LL : 'D MMMM YYYY',
        LLL : 'D MMMM YYYY HH:mm',
        LLLL : 'dddd D MMMM YYYY HH:mm'
    },
    calendar : {
        sameDay : '[Aujourd’hui à] LT',
        nextDay : '[Demain à] LT',
        nextWeek : 'dddd [à] LT',
        lastDay : '[Hier à] LT',
        lastWeek : 'dddd [dernier à] LT',
        sameElse : 'L'
    },
    relativeTime : {
        future : 'dans %s',
        past : 'il y a %s',
        s : 'quelques secondes',
        m : 'une minute',
        mm : '%d minutes',
        h : 'une heure',
        hh : '%d heures',
        d : 'un jour',
        dd : '%d jours',
        M : 'un mois',
        MM : '%d mois',
        y : 'un an',
        yy : '%d ans'
    },
    dayOfMonthOrdinalParse : /\d{1,2}(er|e)/,
    ordinal : function (number) {
        return number + (number === 1 ? 'er' : 'e');
    },
    meridiemParse : /PD|MD/,
    isPM : function (input) {
        return input.charAt(0) === 'M';
    },
    // In case the meridiem units are not separated around 12, then implement
    // this function (look at locale/id.js for an example).
    // meridiemHour : function (hour, meridiem) {
    //     return /* 0-23 hour, given meridiem token and hour 1-12 */ ;
    // },
    meridiem : function (hours, minutes, isLower) {
        return hours < 12 ? 'PD' : 'MD';
    },
    week : {
        dow : 1, // Monday is the first day of the week.
        doy : 4  // Used to determine first week of the year.
    }
});

And then you can just use any function you want. Also you can switch between default en and just defined fr locale.

moment.locale('fr');
moment(1316116057189).fromNow(); // il y a une heure
moment.locale('en');
moment(1316116057189).fromNow(); // an hour ago

original docs

zhisme
  • 2,368
  • 2
  • 19
  • 28
0

You don't necessary need to use moment for this. You can use the toLocaleDateString from the Date function in js.

const options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
console.log(element.dateMatch.toLocaleDateString('fr-FR', options));
// example output: jeudi 20 décembre 2012