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]
);