Skip to content

Commit dd75178

Browse files
authored
fix(core): resolve type mismatch in useGetLocale hook (#6920)
1 parent 79fe2c3 commit dd75178

3 files changed

Lines changed: 22 additions & 5 deletions

File tree

.changeset/heavy-berries-appear.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"@refinedev/core": patch
3+
---
4+
5+
- Throw an error in `useGetLocale` if it is called without an i18n Provider.
6+
- This ensures the hook's return type matches that of `i18nProvider.getLocale`.
7+
- `useTranslation().getLocale` which is from `useGetLocale` now returns a string.
8+
9+
[Resolves #6812](https://github.com/refinedev/refine/issues/6812)

packages/core/src/hooks/i18n/useGetLocale.spec.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import { useGetLocale } from "@hooks";
44
import { TestWrapper } from "@test";
55

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

10-
expect(result.current()).toBe(undefined);
10+
expect(result).toThrow(
11+
"useGetLocale cannot be called without i18n provider being defined.",
12+
);
1113
});
1214

1315
it("should get locale value from i18nProvider getLocale method", () => {

packages/core/src/hooks/i18n/useGetLocale.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { useCallback, useContext } from "react";
22

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

5-
export type UseGetLocaleType = () => () => string | undefined;
5+
export type UseGetLocaleType = () => () => string;
66

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

16-
return useCallback(() => i18nProvider?.getLocale(), []);
16+
if (!i18nProvider) {
17+
throw new Error(
18+
"useGetLocale cannot be called without i18n provider being defined.",
19+
);
20+
}
21+
22+
return useCallback(() => i18nProvider.getLocale(), []);
1723
};

0 commit comments

Comments
 (0)