Skip to content

Commit

Permalink
refactor: modify LocaleProvider and move dayjs langs to locales.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
BartoszBBitnoise committed Feb 9, 2024
1 parent 48dcfb9 commit 97fadef
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
11 changes: 5 additions & 6 deletions src/context/LocaleProvider/LocaleProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { useCallback, useContext, useEffect, useState } from "react";
import dayjs from "dayjs";
import en from "dayjs/locale/en";
import pl from "dayjs/locale/pl";
import de from "dayjs/locale/de";
import { LangCodes } from "@/types/global";
import { localeContext } from "./localeContext";
import { locales } from "./locales";
Expand All @@ -15,9 +12,11 @@ const LocaleProvider = ({ children, lang }: LocaleProviderProps) => {
const locale = locales.find((l) => {
return l.id === localLang;
});
locale?.id === "en" && dayjs.locale({ ...en });
locale?.id === "pl" && dayjs.locale({ ...pl });
locale?.id === "de" && dayjs.locale({ ...de });

if (typeof locale?.dayjsTranslations === "object") {
dayjs.locale({ ...locale.dayjsTranslations });
}

return locale || locales[0];
}, [localLang]);

Expand Down
12 changes: 9 additions & 3 deletions src/context/LocaleProvider/locales.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import enDayjsTranslations from "dayjs/locale/en";
import plDayjsTranslations from "dayjs/locale/pl";
import deDayjsTranslations from "dayjs/locale/de";
import { en } from "@/locales/en";
import { pl } from "@/locales/pl";
import { de } from "@/locales/de";
Expand All @@ -8,18 +11,21 @@ export const locales: LocaleType[] = [
id: "en",
name: "ENGLISH",
lang: en,
translateCode: "en-GB"
translateCode: "en-GB",
dayjsTranslations: enDayjsTranslations
},
{
id: "pl",
name: "POLISH",
lang: pl,
translateCode: "pl-PL"
translateCode: "pl-PL",
dayjsTranslations: plDayjsTranslations
},
{
id: "de",
name: "GERMAN",
lang: de,
translateCode: "de-DE"
translateCode: "de-DE",
dayjsTranslations: deDayjsTranslations
}
];
1 change: 1 addition & 0 deletions src/context/LocaleProvider/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ export type LocaleType = {
name: string;
lang: TranslationType;
translateCode: string;
dayjsTranslations: string | ILocale | undefined;
};

0 comments on commit 97fadef

Please sign in to comment.