hypeserver / react-date-range

A React component for choosing dates and date ranges.
MIT License
2.59k stars 672 forks source link

Translations? #369

Closed piekczyk closed 4 years ago

piekczyk commented 4 years ago

Subject of the issue

Describe your issue here.

Is there a way to easily change translations?

Environment

Package Version: 1.0.3 React version: 3.4.0 Node version: 12 Browser: Brave

chrisclausen90 commented 4 years ago

@piekczyk

import { de, enGB } from 'date-fns/locale';

and use property local

<DateRangePicker ranges={[{ startDate: new Date(startDate), endDate: new Date(endDate), key: 'selection', }]} locale={de} />

date-fns is generating the translations for the datepicker. date-fns is a peer dependency.

mr-burhanuddin commented 1 year ago

I am using DateRangePicker

https://codesandbox.io/s/great-browser-h9ghcp?file=/src/components/Calendar.jsx

only the calender Is Getting Translated Not the Left Menu of Today,thisWeek Etc is There A Way to Fix that

mattvb91 commented 11 months ago

@mr-burhanuddin did you manage to find a fix?

mr-burhanuddin commented 11 months ago

@mr-burhanuddin did you manage to find a fix? @mattvb91 Yes !

Here is A Custom Hook **import { useEffect } from "react"; import { useTranslation } from "react-i18next";

function useTranslateCalendar(runTranslation: boolean): void { const { t } = useTranslation();

useEffect(() => { if (runTranslation) { const inputRanges = document.querySelectorAll(".rdrInputRanges"); inputRanges.forEach((inputRange) => { const spans = inputRange.querySelectorAll("span"); spans.forEach((span) => { const value = span.textContent; const translatedValue = typeof value === "string" ? ${t(value.replace(/\s/g, "").toLowerCase())} : ""; span.textContent = translatedValue; }); });

  const labelElements = document.querySelectorAll(".rdrStaticRangeLabel");
  labelElements.forEach((element) => {
    const value = element.textContent;

    const translatedValue =
      typeof value === "string"
        ? `${t(value.replace(/\s/g, "").toLowerCase())}`
        : "";
    element.textContent = translatedValue;
  });
}

}, [runTranslation, t]); }

export default useTranslateCalendar;**

Add The Keys Like this in translation.json files

"today": "Today", "yesterday": "Yesterday", "thisweek": "This Week", "lastweek": "Last Week", "thismonth": "This Month", "lastmonth": "Last Month", "daysuptotoday": "Days Upto Today", "daysstartingtoday": "Days Starting Today",

And Call The Hook it will Work

mattvb91 commented 11 months ago

Awesome stuff thank you

keremciu commented 8 months ago

you don't need to hack anything, just create your own daterangepicker.

https://github.com/hypeserver/react-date-range/blob/master/src/components/DateRangePicker/index.js#L21

daterange picker is the composition of daterange and definedrange, create your custom ranges https://github.com/hypeserver/react-date-range/tree/master/src/components/DefinedRange#example-custom-range-labels

if you want the existing ones, you can copy from here https://github.com/hypeserver/react-date-range/blob/master/src/defaultRanges.js#L44 to change it to your version