From f03ef6a9e1b7c96b4430c3316fae6079481e0beb Mon Sep 17 00:00:00 2001 From: Lebaud Antoine Date: Mon, 26 Jun 2023 18:55:13 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8(react)=20expose=20`currentLocale`=20i?= =?UTF-8?q?n=20`CunninghamContext`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Expose `currentLocale` value to let know its subscribers what is the locale value currently in use. --- .../src/components/Provider/index.spec.tsx | 53 ++++++++++++++++++- .../react/src/components/Provider/index.tsx | 9 ++-- 2 files changed, 56 insertions(+), 6 deletions(-) diff --git a/packages/react/src/components/Provider/index.spec.tsx b/packages/react/src/components/Provider/index.spec.tsx index 16f3970..e15ed68 100644 --- a/packages/react/src/components/Provider/index.spec.tsx +++ b/packages/react/src/components/Provider/index.spec.tsx @@ -1,7 +1,12 @@ import { render, screen } from "@testing-library/react"; import React, { PropsWithChildren, useMemo, useState } from "react"; import userEvent from "@testing-library/user-event"; -import { CunninghamProvider, useCunningham } from ":/components/Provider/index"; +import { expect } from "vitest"; +import { + CunninghamProvider, + DEFAULT_LOCALE, + useCunningham, +} from ":/components/Provider/index"; import * as enUS from ":/locales/en-US.json"; import { Button } from ":/components/Button"; @@ -102,4 +107,50 @@ describe("", () => { name: "components.will_never_exist", }); }); + + it("should return current locale", () => { + const locale = "fr-FR"; + const Wrapper = (props: PropsWithChildren) => { + return ( + + {props.children} + + ); + }; + const Wrapped = () => { + const { currentLocale } = useCunningham(); + expect(currentLocale).eq(locale); + return
; + }; + render(, { wrapper: Wrapper }); + }); + + it("should return default locale if no current locale is provided", () => { + const Wrapper = (props: PropsWithChildren) => { + return {props.children}; + }; + const Wrapped = () => { + const { currentLocale } = useCunningham(); + expect(currentLocale).eq(DEFAULT_LOCALE); + return
; + }; + render(, { wrapper: Wrapper }); + }); + + it("should return default locale if the current locale is not supported", () => { + const wrongLocale = "fr_FR"; + const Wrapper = (props: PropsWithChildren) => { + return ( + + {props.children} + + ); + }; + const Wrapped = () => { + const { currentLocale } = useCunningham(); + expect(currentLocale).eq(DEFAULT_LOCALE); + return
; + }; + render(, { wrapper: Wrapper }); + }); }); diff --git a/packages/react/src/components/Provider/index.tsx b/packages/react/src/components/Provider/index.tsx index 3b56913..f1f6474 100644 --- a/packages/react/src/components/Provider/index.tsx +++ b/packages/react/src/components/Provider/index.tsx @@ -15,6 +15,7 @@ const CunninghamContext = createContext< | undefined | { t: (key: string, vars?: Record) => string; + currentLocale: string; } >(undefined); @@ -57,17 +58,14 @@ export const CunninghamProvider = ({ ); const locale = useMemo(() => { - if (!locales[currentLocale]) { - return locales[DEFAULT_LOCALE]; - } - return locales[currentLocale]; + return (locales[currentLocale] && currentLocale) || DEFAULT_LOCALE; }, [currentLocale, locales]); const context = useMemo( () => ({ t: (key: string, vars?: Record) => { let message: string = - findTranslation(key, locale) ?? + findTranslation(key, locales[locale]) ?? findTranslation(key, locales[DEFAULT_LOCALE]) ?? key; @@ -80,6 +78,7 @@ export const CunninghamProvider = ({ return message; }, + currentLocale: locale, }), [currentLocale, locales] );