0

The truth is that this problem came back to me in some cases. In this case I want to declare the LOCAL CONFIG OBJECT from another file (for code cleaning) but do not know how to do it. Can I have an explanation please?

My situation at the moment is that the file is very long and unreadable. i want to get the "local config object" from another file:

  • local config object from react-native-calendars look like this:

    import { LocaleConfig } from "react-native-calendars";
    LocaleConfig.locales["fr"] = {
     monthNames: [
       "Janvier",
      "Février",
     "Mars",
     "Avril",
     "Mai",
     "Juin",
     "Juillet",
     "Août",
     "Septembre",
     "Octobre",
     "Novembre",
     "Décembre",
    ],
             monthNamesShort: ["Janv.", "Févr.", "Mars", "Avril", "Mai", "Juin", "Juil.", "Août", 
     "Sept.", "Oct.", 
    "Nov.", "Déc."],
    dayNames: ["Dimanche", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi"],
    dayNamesShort: ["shimon.", "Lun.", "Mar.", "Mer.", "Jeu.", "Ven.", "Sam."],
     today: "Aujourd'hui",
      };
     LocaleConfig.defaultLocale = "fr";
    
  • my all file with the component look like:

    import React, { useEffect, useContext, useState } from "react";
     import { StyleSheet, View } from "react-native";
     import { Agenda } from "react-native-calendars";
     import { CalendarItem } from "../customStyles/CalendarItem";
     import { Context as BookingContext } from "../context/booking-context";
     import { LocaleConfig } from "react-native-calendars";
    
     LocaleConfig.locales["fr"] = {
     monthNames: [
     "Janvier",
     "Février",
     "Mars",
     "Avril",
     "Mai",
     "Juin",
     "Juillet",
     "Août",
     "Septembre",
     "Octobre",
     "Novembre",
     "Décembre",
      ],
      monthNamesShort: ["Janv.", "Févr.", "Mars", "Avril", "Mai", "Juin", "Juil.", "Août", "Sept.", 
     "Oct.", 
       "Nov.", "Déc."],
     dayNames: ["Dimanche", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi"],
    dayNamesShort: ["shimon.", "Lun.", "Mar.", "Mer.", "Jeu.", "Ven.", "Sam."],
    today: "Aujourd'hui",
    };
     LocaleConfig.defaultLocale = "fr";
     const UserHistory = () => {
     const { state, getAllMeetings } = useContext(BookingContext);
     const meetings = state.meetings;
     const loadMonthItems = month => {
     console.log("ffdffffffffffffffffffffffffffffffffffffffffffffff", month);
     };
     useEffect(() => {
      getAllMeetings();
    }, []);
    
    const renderItem = item => {
     return <CalendarItem item={item} />;
     };
    
     return (
     <View style={{ flex: 1 }}>
         <Agenda
             items={meetings}
             loadItemsForMonth={loadMonthItems}
             // selected={moment().format("YYYY-MM-DD")}
             selected={"2020-11-22"}
             renderItem={renderItem}
             theme={{
                 "stylesheet.agenda.list": {
                     container: {
                         flexDirection: "column",
                         borderColor: "yellow",
                         borderWidth: 1,
                     },
                     dayNum: {
                         fontSize: 16,
                         fontWeight: "200",
                     },
                     dayText: {
                         fontSize: 14,
    
                         fontFamily: "heebo",
                         color: "green",
                         backgroundColor: "rgba(0,0,0,0)",
                         marginTop: 0,
                     },
                     day: {
                         width: 63,
                         alignItems: "center",
                         justifyContent: "flex-start",
                         marginTop: 32,
    
                         borderColor: "red",
                         borderWidth: 1,
                     },
                     today: {
                         color: "blue",
                     },
                 },
             }}
             style={{
                 borderWidth: 3,
                 borderColor: "gray",
             }}
         />
       </View>
      );
      };
    
      const styles = StyleSheet.create({});
     export default UserHistory;
    
Roei Grinshpan
  • 395
  • 1
  • 5
  • 19

2 Answers2

0

There are few reasons why you would need to declare a localConfig to implement the react-native-calendar. But I can guess through your question that you want to make use of local values such as changing the calendar's language or setting the first day of the week according to the region you are developing the application for. To do that the only thing you need to do is to pass to the calendar component itself the props monthNames,monthNamesShort,dayNames and dayNamesShort with the relevant local values that you want to display. For instance if you wanted to change the calendar's language to german, no need to declare a localConfig file, instead you just need to pass your local values inside the calendar component like

<Calendar
    monthNames: [ 'Januar', 'Februar', 'März', 'April', 'Mai', 'Juni', 'July', 'August', 'September', 'Oktober', 'November', 'Dezember' ],
    monthNamesShort: [ 'Jan.', 'Feb.', 'Mär.', 'Apr.', 'Mai', 'Jun.', 'Jul.', 'Aug.', 'Sept.', 'Okt.', 'Nov.', 'Dez.' ],
    dayNames: ['Sonntag', 'Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag'],
    dayNamesShort: ['So.', 'Mo.', 'Di.', 'Mi.', 'Do.', 'Fr.', 'Sa.'],
    onDateChange:{yourFunction}
/>

For any other local stuff you wanna use, just pass it straight as props to the calendar component, the process is straightforward. The whole explanation about this can be found in the README file under

node- modules -> react-native-calendar -> readme

There you can also find some handy approaches about how to localize your react native calendar without creating extra files.

Himmels DJ
  • 395
  • 5
  • 20
0

To create new config file you just need to create ts or js extension file with function. And call that function in file where you use calendar.

If I correctly understood your question you want to have something like this

import {LocaleConfig} from 'react-native-calendars';

const defineLocale = () => {
  LocaleConfig.locales['ru'] = {
    monthNames: [
      'Январь',
      'Февраль',
      'Март',
      'Апрель',
      'Май',
      'Июнь',
      'Июль',
      'Август',
      'Сентябрь',
      'Октябрь',
      'Ноябрь',
      'Декабрь'
    ],
    monthNamesShort: ['Янв.', 'Фев.', 'Март', 'Апрель', 'Май', 'Июнь', 'Июль.', 'Авг.', 'Сент.', 'Окт.', 'Нояб.', 'Дек.'],
    dayNames: ['Dimanche', 'Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi', 'Samedi'],
    dayNamesShort: ['Dim.', 'Lun.', 'Mar.', 'Mer.', 'Jeu.', 'Ven.', 'Sam.'],
    today: "Aujourd'hui"
  };
  LocaleConfig.locales.en = LocaleConfig.locales[''];
};

export default defineLocale;
Exoriri
  • 327
  • 3
  • 21