Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/heavy-berries-appear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@refinedev/core": patch
---

- Throw an error in `useGetLocale` if it is called without an i18n Provider.
- This ensures the hook's return type matches that of `i18nProvider.getLocale`.
- `useTranslation().getLocale` which is from `useGetLocale` now returns a string.

[Resolves #6812](https://github.com/refinedev/refine/issues/6812)
8 changes: 5 additions & 3 deletions packages/core/src/hooks/i18n/useGetLocale.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import { useGetLocale } from "@hooks";
import { TestWrapper } from "@test";

describe("useGetLocale", () => {
it("should get undefined value if i18n provider not defined", () => {
const { result } = renderHook(() => useGetLocale());
it("should throw error if i18n provider is not defined", () => {
const result = () => renderHook(() => useGetLocale());

expect(result.current()).toBe(undefined);
expect(result).toThrow(
"useGetLocale cannot be called without i18n provider being defined.",
);
});

it("should get locale value from i18nProvider getLocale method", () => {
Expand Down
10 changes: 8 additions & 2 deletions packages/core/src/hooks/i18n/useGetLocale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useCallback, useContext } from "react";

import { I18nContext } from "@contexts/i18n";

export type UseGetLocaleType = () => () => string | undefined;
export type UseGetLocaleType = () => () => string;

/**
* If you need to know the current locale, refine provides the `useGetLocale` hook.
Expand All @@ -13,5 +13,11 @@ export type UseGetLocaleType = () => () => string | undefined;
export const useGetLocale: UseGetLocaleType = () => {
const { i18nProvider } = useContext(I18nContext);

return useCallback(() => i18nProvider?.getLocale(), []);
if (!i18nProvider) {
throw new Error(
"useGetLocale cannot be called without i18n provider being defined.",
);
}

return useCallback(() => i18nProvider.getLocale(), []);
};
Loading