@prosopo/locale 3.4.3 → 3.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (158) hide show
  1. package/.turbo/turbo-build$colon$cjs.log +15 -13
  2. package/.turbo/turbo-build$colon$tsc.log +8 -5
  3. package/.turbo/turbo-build.log +17 -15
  4. package/CHANGELOG.md +74 -0
  5. package/dist/cjs/i18SharedOptions.cjs +2 -2
  6. package/dist/cjs/i18nBackend.cjs +1 -1
  7. package/dist/cjs/i18nFrontend.cjs +167 -53
  8. package/dist/cjs/i18nMiddleware.cjs +5 -2
  9. package/dist/cjs/index.cjs +5 -4
  10. package/dist/cjs/locales/en/translation.cjs +1 -0
  11. package/dist/cjs/translationKey.cjs +2 -3
  12. package/dist/cjs/translations.cjs +4 -5
  13. package/dist/cjs/translator.cjs +29 -0
  14. package/dist/i18SharedOptions.d.ts +2 -2
  15. package/dist/i18SharedOptions.js +3 -3
  16. package/dist/i18SharedOptions.js.map +1 -1
  17. package/dist/i18nBackend.js +1 -1
  18. package/dist/i18nFrontend.d.ts +2 -2
  19. package/dist/i18nFrontend.d.ts.map +1 -1
  20. package/dist/i18nFrontend.js +167 -47
  21. package/dist/i18nFrontend.js.map +1 -1
  22. package/dist/i18nMiddleware.d.ts.map +1 -1
  23. package/dist/i18nMiddleware.js +3 -1
  24. package/dist/i18nMiddleware.js.map +1 -1
  25. package/dist/index.d.ts +6 -4
  26. package/dist/index.d.ts.map +1 -1
  27. package/dist/index.js +4 -4
  28. package/dist/index.js.map +1 -1
  29. package/dist/loadI18next.d.ts +2 -3
  30. package/dist/loadI18next.d.ts.map +1 -1
  31. package/dist/loadI18next.js.map +1 -1
  32. package/dist/locales/ar/translation.json +1 -0
  33. package/dist/locales/az/translation.json +1 -0
  34. package/dist/locales/cs/translation.json +1 -0
  35. package/dist/locales/de/translation.json +1 -0
  36. package/dist/locales/el/translation.json +1 -0
  37. package/dist/locales/en/translation.js +1 -0
  38. package/dist/locales/en/translation.json +1 -0
  39. package/dist/locales/es/translation.json +1 -0
  40. package/dist/locales/fi/translation.json +1 -0
  41. package/dist/locales/fr/translation.json +1 -0
  42. package/dist/locales/hi/translation.json +1 -0
  43. package/dist/locales/hu/translation.json +1 -0
  44. package/dist/locales/id/translation.json +1 -0
  45. package/dist/locales/it/translation.json +1 -0
  46. package/dist/locales/ja/translation.json +1 -0
  47. package/dist/locales/jv/translation.json +1 -0
  48. package/dist/locales/ko/translation.json +1 -0
  49. package/dist/locales/ml/translation.json +1 -0
  50. package/dist/locales/ms/translation.json +1 -0
  51. package/dist/locales/nl/translation.json +1 -0
  52. package/dist/locales/no/translation.json +1 -0
  53. package/dist/locales/pl/translation.json +1 -0
  54. package/dist/locales/pt/translation.json +1 -0
  55. package/dist/locales/pt-BR/translation.json +1 -0
  56. package/dist/locales/ro/translation.json +1 -0
  57. package/dist/locales/ru/translation.json +1 -0
  58. package/dist/locales/sr/translation.json +1 -0
  59. package/dist/locales/sv/translation.json +1 -0
  60. package/dist/locales/th/translation.json +1 -0
  61. package/dist/locales/tr/translation.json +1 -0
  62. package/dist/locales/uk/translation.json +1 -0
  63. package/dist/locales/vi/translation.json +1 -0
  64. package/dist/locales/zh-CN/translation.json +1 -0
  65. package/dist/tests/i18nFrontend.unit.test.js +218 -173
  66. package/dist/tests/i18nFrontend.unit.test.js.map +1 -1
  67. package/dist/tests/i18nMiddleware.unit.test.js +11 -2
  68. package/dist/tests/i18nMiddleware.unit.test.js.map +1 -1
  69. package/dist/tests/loadI18next.unit.test.js.map +1 -1
  70. package/dist/tests/locale.test-d.js +27 -20
  71. package/dist/tests/locale.test-d.js.map +1 -1
  72. package/dist/tests/translationKey.unit.test.js +11 -33
  73. package/dist/tests/translationKey.unit.test.js.map +1 -1
  74. package/dist/tests/translations.unit.test.js +12 -22
  75. package/dist/tests/translations.unit.test.js.map +1 -1
  76. package/dist/tests/translator.unit.test.d.ts +2 -0
  77. package/dist/tests/translator.unit.test.d.ts.map +1 -0
  78. package/dist/tests/translator.unit.test.js +134 -0
  79. package/dist/tests/translator.unit.test.js.map +1 -0
  80. package/dist/translationKey.d.ts +2 -3
  81. package/dist/translationKey.d.ts.map +1 -1
  82. package/dist/translationKey.js +2 -3
  83. package/dist/translationKey.js.map +1 -1
  84. package/dist/translations.d.ts +3 -2
  85. package/dist/translations.d.ts.map +1 -1
  86. package/dist/translations.js +3 -3
  87. package/dist/translations.js.map +1 -1
  88. package/dist/translator.d.ts +9 -0
  89. package/dist/translator.d.ts.map +1 -0
  90. package/dist/translator.js +29 -0
  91. package/dist/translator.js.map +1 -0
  92. package/dist/types.d.ts +16 -0
  93. package/dist/types.d.ts.map +1 -0
  94. package/dist/types.js +2 -0
  95. package/dist/types.js.map +1 -0
  96. package/package.json +4 -9
  97. package/src/i18SharedOptions.ts +3 -3
  98. package/src/i18nFrontend.ts +240 -78
  99. package/src/i18nMiddleware.ts +6 -1
  100. package/src/index.ts +11 -4
  101. package/src/loadI18next.ts +8 -9
  102. package/src/locales/ar/translation.json +1 -0
  103. package/src/locales/az/translation.json +1 -0
  104. package/src/locales/cs/translation.json +1 -0
  105. package/src/locales/de/translation.json +1 -0
  106. package/src/locales/el/translation.json +1 -0
  107. package/src/locales/en/translation.json +1 -0
  108. package/src/locales/es/translation.json +1 -0
  109. package/src/locales/fi/translation.json +1 -0
  110. package/src/locales/fr/translation.json +1 -0
  111. package/src/locales/hi/translation.json +1 -0
  112. package/src/locales/hu/translation.json +1 -0
  113. package/src/locales/id/translation.json +1 -0
  114. package/src/locales/it/translation.json +1 -0
  115. package/src/locales/ja/translation.json +1 -0
  116. package/src/locales/jv/translation.json +1 -0
  117. package/src/locales/ko/translation.json +1 -0
  118. package/src/locales/ml/translation.json +1 -0
  119. package/src/locales/ms/translation.json +1 -0
  120. package/src/locales/nl/translation.json +1 -0
  121. package/src/locales/no/translation.json +1 -0
  122. package/src/locales/pl/translation.json +1 -0
  123. package/src/locales/pt/translation.json +1 -0
  124. package/src/locales/pt-BR/translation.json +1 -0
  125. package/src/locales/ro/translation.json +1 -0
  126. package/src/locales/ru/translation.json +1 -0
  127. package/src/locales/sr/translation.json +1 -0
  128. package/src/locales/sv/translation.json +1 -0
  129. package/src/locales/th/translation.json +1 -0
  130. package/src/locales/tr/translation.json +1 -0
  131. package/src/locales/uk/translation.json +1 -0
  132. package/src/locales/vi/translation.json +1 -0
  133. package/src/locales/zh-CN/translation.json +1 -0
  134. package/src/tests/i18nFrontend.unit.test.ts +310 -219
  135. package/src/tests/i18nMiddleware.unit.test.ts +21 -2
  136. package/src/tests/loadI18next.unit.test.ts +11 -11
  137. package/src/tests/locale.test-d.ts +36 -32
  138. package/src/tests/translationKey.unit.test.ts +12 -38
  139. package/src/tests/translations.unit.test.ts +18 -31
  140. package/src/tests/translator.unit.test.ts +213 -0
  141. package/src/translationKey.ts +10 -9
  142. package/src/translations.ts +6 -5
  143. package/src/translator.ts +68 -0
  144. package/src/types.ts +42 -0
  145. package/tsconfig.cjs.json +3 -0
  146. package/tsconfig.json +3 -0
  147. package/tsconfig.tsbuildinfo +1 -1
  148. package/dist/cjs/useTranslation.cjs +0 -12
  149. package/dist/tests/useTranslation.unit.test.d.ts +0 -2
  150. package/dist/tests/useTranslation.unit.test.d.ts.map +0 -1
  151. package/dist/tests/useTranslation.unit.test.js +0 -81
  152. package/dist/tests/useTranslation.unit.test.js.map +0 -1
  153. package/dist/useTranslation.d.ts +0 -7
  154. package/dist/useTranslation.d.ts.map +0 -1
  155. package/dist/useTranslation.js +0 -11
  156. package/dist/useTranslation.js.map +0 -1
  157. package/src/tests/useTranslation.unit.test.ts +0 -134
  158. package/src/useTranslation.ts +0 -31
@@ -1,81 +0,0 @@
1
- import { afterEach, describe, expect, test, vi } from "vitest";
2
- const asI18n = (value) => value;
3
- const load = async () => {
4
- vi.resetModules();
5
- const instance = asI18n({ name: "package instance" });
6
- const calls = [];
7
- let initCalls = 0;
8
- vi.doMock("../i18nFrontend.js", () => ({
9
- default: () => {
10
- initCalls += 1;
11
- return instance;
12
- },
13
- }));
14
- vi.doMock("react-i18next", () => ({
15
- useTranslation: (namespace, options) => {
16
- calls.push([namespace, options]);
17
- return { t: (key) => key, i18n: options.i18n };
18
- },
19
- }));
20
- const imported = await import("../useTranslation.js");
21
- return {
22
- useTranslation: imported.default,
23
- instance,
24
- calls,
25
- initCount: () => initCalls,
26
- };
27
- };
28
- afterEach(() => {
29
- vi.resetModules();
30
- vi.doUnmock("../i18nFrontend.js");
31
- vi.doUnmock("react-i18next");
32
- });
33
- describe("useTranslation", () => {
34
- test("always asks for the translation namespace", async () => {
35
- const { useTranslation, calls } = await load();
36
- useTranslation();
37
- expect(calls[0]?.[0]).toBe("translation");
38
- });
39
- test("injects the package's own i18next instance", async () => {
40
- const { useTranslation, calls, instance } = await load();
41
- useTranslation();
42
- expect(calls[0]?.[1].i18n).toBe(instance);
43
- });
44
- test("initialises the frontend instance on use", async () => {
45
- const { useTranslation, initCount } = await load();
46
- useTranslation();
47
- expect(initCount()).toBe(1);
48
- });
49
- test("works with no options at all", async () => {
50
- const { useTranslation, calls } = await load();
51
- useTranslation();
52
- expect(calls[0]?.[1]).toEqual({ i18n: expect.anything() });
53
- });
54
- test("passes caller options through", async () => {
55
- const { useTranslation, calls } = await load();
56
- useTranslation({ nsMode: "fallback", useSuspense: false });
57
- expect(calls[0]?.[1]).toMatchObject({
58
- nsMode: "fallback",
59
- useSuspense: false,
60
- });
61
- });
62
- test("lets the caller override the instance, since options spread last", async () => {
63
- const { useTranslation, calls, instance } = await load();
64
- const other = asI18n({ name: "caller instance" });
65
- useTranslation({ i18n: other });
66
- expect(calls[0]?.[1].i18n).toBe(other);
67
- expect(calls[0]?.[1].i18n).not.toBe(instance);
68
- });
69
- test("returns whatever react-i18next returns", async () => {
70
- const { useTranslation } = await load();
71
- const result = useTranslation();
72
- expect(result.t("WIDGET.SUBMIT")).toBe("WIDGET.SUBMIT");
73
- });
74
- test("re-initialises on every call, matching hook semantics", async () => {
75
- const { useTranslation, initCount } = await load();
76
- useTranslation();
77
- useTranslation();
78
- expect(initCount()).toBe(2);
79
- });
80
- });
81
- //# sourceMappingURL=useTranslation.unit.test.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"useTranslation.unit.test.js","sourceRoot":"","sources":["../../src/tests/useTranslation.unit.test.ts"],"names":[],"mappings":"AAgBA,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAC;AAS/D,MAAM,MAAM,GAAG,CAAC,KAAa,EAAQ,EAAE,CAAC,KAAwB,CAAC;AAEjE,MAAM,IAAI,GAAG,KAAK,IAKf,EAAE;IACJ,EAAE,CAAC,YAAY,EAAE,CAAC;IAElB,MAAM,QAAQ,GAAG,MAAM,CAAC,EAAE,IAAI,EAAE,kBAAkB,EAAE,CAAC,CAAC;IACtD,MAAM,KAAK,GAAkB,EAAE,CAAC;IAChC,IAAI,SAAS,GAAG,CAAC,CAAC;IAElB,EAAE,CAAC,MAAM,CAAC,oBAAoB,EAAE,GAAG,EAAE,CAAC,CAAC;QACtC,OAAO,EAAE,GAAS,EAAE;YACnB,SAAS,IAAI,CAAC,CAAC;YACf,OAAO,QAAQ,CAAC;QACjB,CAAC;KACD,CAAC,CAAC,CAAC;IACJ,EAAE,CAAC,MAAM,CAAC,eAAe,EAAE,GAAG,EAAE,CAAC,CAAC;QACjC,cAAc,EAAE,CACf,SAAiB,EACjB,OAAiD,EACvC,EAAE;YACZ,KAAK,CAAC,IAAI,CAAC,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC,CAAC;YACjC,OAAO,EAAE,CAAC,EAAE,CAAC,GAAW,EAAU,EAAE,CAAC,GAAG,EAAE,IAAI,EAAE,OAAO,CAAC,IAAI,EAAE,CAAC;QAChE,CAAC;KACD,CAAC,CAAC,CAAC;IAEJ,MAAM,QAAQ,GAAG,MAAM,MAAM,CAAC,sBAAsB,CAAC,CAAC;IACtD,OAAO;QACN,cAAc,EA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IAEH,IAAI,CAAC,wCAAwC,EAAE,KAAK,IAAI,EAAE;QACzD,MAAM,EAAE,cAAc,EAAE,GAAG,MAAM,IAAI,EAAE,CAAC;QACxC,MAAM,MAAM,GAAG,cAAc,EAAoC,CAAC;QAClE,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,eAAe,CAAC,CAAC,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC;IACzD,CAAC,CAAC,CAAC;IAEH,IAAI,CAAC,uDAAuD,EAAE,KAAK,IAAI,EAAE;QAGxE,MAAM,EAAE,cAAc,EAAE,SAAS,EAAE,GAAG,MAAM,IAAI,EAAE,CAAC;QACnD,cAAc,EAAE,CAAC;QACjB,cAAc,EAAE,CAAC;QACjB,MAAM,CAAC,SAAS,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAC7B,CAAC,CAAC,CAAC;AACJ,CAAC,CAAC,CAAC"}
@@ -1,7 +0,0 @@
1
- import { type UseTranslationOptions, type UseTranslationResponse } from "react-i18next";
2
- import initializeI18n from "./i18nFrontend.js";
3
- declare function useTranslation(options?: UseTranslationOptions<"translation">): UseTranslationResponse<"translation", "translation"> & {
4
- t: ReturnType<typeof initializeI18n>["t"];
5
- };
6
- export default useTranslation;
7
- //# sourceMappingURL=useTranslation.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"useTranslation.d.ts","sourceRoot":"","sources":["../src/useTranslation.ts"],"names":[],"mappings":"AAcA,OAAO,EACN,KAAK,qBAAqB,EAC1B,KAAK,sBAAsB,EAE3B,MAAM,eAAe,CAAC;AACvB,OAAO,cAAc,MAAM,mBAAmB,CAAC;AAE/C,iBAAS,cAAc,CACtB,OAAO,CAAC,EAAE,qBAAqB,CAAC,aAAa,CAAC,GAC5C,sBAAsB,CAAC,aAAa,EAAE,aAAa,CAAC,GAAG;IACzD,CAAC,EAAE,UAAU,CAAC,OAAO,cAAc,CAAC,CAAC,GAAG,CAAC,CAAC;CAC1C,CAGA;AAED,eAAe,cAAc,CAAC"}
@@ -1,11 +0,0 @@
1
- import initializeI18n from "./i18nFrontend.js";
2
- import { useTranslation } from "react-i18next";
3
- //#region src/useTranslation.ts
4
- function useTranslation$1(options) {
5
- return useTranslation("translation", {
6
- i18n: initializeI18n(),
7
- ...options
8
- });
9
- }
10
- //#endregion
11
- export { useTranslation$1 as default };
@@ -1 +0,0 @@
1
- {"version":3,"file":"useTranslation.js","sourceRoot":"","sources":["../src/useTranslation.ts"],"names":[],"mappings":"AAcA,OAAO,EAGN,cAAc,IAAI,qBAAqB,GACvC,MAAM,eAAe,CAAC;AACvB,OAAO,cAAc,MAAM,mBAAmB,CAAC;AAE/C,SAAS,cAAc,CACtB,OAA8C;IAI9C,MAAM,IAAI,GAAG,cAAc,EAAE,CAAC;IAC9B,OAAO,qBAAqB,CAAC,aAAa,EAAE,EAAE,IAAI,EAAE,GAAG,OAAO,EAAE,CAAC,CAAC;AACnE,CAAC;AAED,eAAe,cAAc,CAAC"}
@@ -1,134 +0,0 @@
1
- // Copyright 2021-2026 Prosopo (UK) Ltd.
2
- //
3
- // Licensed under the Apache License, Version 2.0 (the "License");
4
- // you may not use this file except in compliance with the License.
5
- // You may obtain a copy of the License at
6
- //
7
- // http://www.apache.org/licenses/LICENSE-2.0
8
- //
9
- // Unless required by applicable law or agreed to in writing, software
10
- // distributed under the License is distributed on an "AS IS" BASIS,
11
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- // See the License for the specific language governing permissions and
13
- // limitations under the License.
14
-
15
- import type { i18n } from "i18next";
16
- import type { UseTranslationOptions } from "react-i18next";
17
- import { afterEach, describe, expect, test, vi } from "vitest";
18
-
19
- // The hook is a thin wrapper: it forces the package's own i18next instance in
20
- // rather than letting react-i18next pick up whichever instance a host app's
21
- // provider happens to expose. React itself is never rendered here, so both
22
- // react-i18next and the frontend initialiser are mocked.
23
-
24
- type DefaultCall = [string, { i18n: i18n } & Record<string, unknown>];
25
-
26
- const asI18n = (value: object): i18n => value as unknown as i18n;
27
-
28
- const load = async (): Promise<{
29
- useTranslation: (options?: UseTranslationOptions<"translation">) => unknown;
30
- instance: i18n;
31
- calls: DefaultCall[];
32
- initCount: () => number;
33
- }> => {
34
- vi.resetModules();
35
-
36
- const instance = asI18n({ name: "package instance" });
37
- const calls: DefaultCall[] = [];
38
- let initCalls = 0;
39
-
40
- vi.doMock("../i18nFrontend.js", () => ({
41
- default: (): i18n => {
42
- initCalls += 1;
43
- return instance;
44
- },
45
- }));
46
- vi.doMock("react-i18next", () => ({
47
- useTranslation: (
48
- namespace: string,
49
- options: { i18n: i18n } & Record<string, unknown>,
50
- ): unknown => {
51
- calls.push([namespace, options]);
52
- return { t: (key: string): string => key, i18n: options.i18n };
53
- },
54
- }));
55
-
56
- const imported = await import("../useTranslation.js");
57
- return {
58
- useTranslation: imported.default,
59
- instance,
60
- calls,
61
- initCount: (): number => initCalls,
62
- };
63
- };
64
-
65
- afterEach(() => {
66
- vi.resetModules();
67
- vi.doUnmock("../i18nFrontend.js");
68
- vi.doUnmock("react-i18next");
69
- });
70
-
71
- describe("useTranslation", () => {
72
- test("always asks for the translation namespace", async () => {
73
- const { useTranslation, calls } = await load();
74
- useTranslation();
75
- expect(calls[0]?.[0]).toBe("translation");
76
- });
77
-
78
- test("injects the package's own i18next instance", async () => {
79
- // Without this, a host application that mounts its own I18nextProvider
80
- // would capture the widget's translations and render its keys instead.
81
- const { useTranslation, calls, instance } = await load();
82
- useTranslation();
83
- expect(calls[0]?.[1].i18n).toBe(instance);
84
- });
85
-
86
- test("initialises the frontend instance on use", async () => {
87
- const { useTranslation, initCount } = await load();
88
- useTranslation();
89
- expect(initCount()).toBe(1);
90
- });
91
-
92
- test("works with no options at all", async () => {
93
- const { useTranslation, calls } = await load();
94
- useTranslation();
95
- expect(calls[0]?.[1]).toEqual({ i18n: expect.anything() });
96
- });
97
-
98
- test("passes caller options through", async () => {
99
- const { useTranslation, calls } = await load();
100
- useTranslation({ nsMode: "fallback", useSuspense: false });
101
- expect(calls[0]?.[1]).toMatchObject({
102
- nsMode: "fallback",
103
- useSuspense: false,
104
- });
105
- });
106
-
107
- test("lets the caller override the instance, since options spread last", async () => {
108
- // Worth pinning: the spread order means a caller-supplied i18n wins.
109
- // That is useful for tests and storybooks, and dangerous by accident,
110
- // so it should not change silently.
111
- const { useTranslation, calls, instance } = await load();
112
- const other = asI18n({ name: "caller instance" });
113
-
114
- useTranslation({ i18n: other } as UseTranslationOptions<"translation">);
115
-
116
- expect(calls[0]?.[1].i18n).toBe(other);
117
- expect(calls[0]?.[1].i18n).not.toBe(instance);
118
- });
119
-
120
- test("returns whatever react-i18next returns", async () => {
121
- const { useTranslation } = await load();
122
- const result = useTranslation() as { t: (key: string) => string };
123
- expect(result.t("WIDGET.SUBMIT")).toBe("WIDGET.SUBMIT");
124
- });
125
-
126
- test("re-initialises on every call, matching hook semantics", async () => {
127
- // initializeI18n is idempotent, so calling it per render is safe; the
128
- // test exists so that stops being true loudly rather than quietly.
129
- const { useTranslation, initCount } = await load();
130
- useTranslation();
131
- useTranslation();
132
- expect(initCount()).toBe(2);
133
- });
134
- });
@@ -1,31 +0,0 @@
1
- // Copyright 2021-2026 Prosopo (UK) Ltd.
2
- //
3
- // Licensed under the Apache License, Version 2.0 (the "License");
4
- // you may not use this file except in compliance with the License.
5
- // You may obtain a copy of the License at
6
- //
7
- // http://www.apache.org/licenses/LICENSE-2.0
8
- //
9
- // Unless required by applicable law or agreed to in writing, software
10
- // distributed under the License is distributed on an "AS IS" BASIS,
11
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- // See the License for the specific language governing permissions and
13
- // limitations under the License.
14
-
15
- import {
16
- type UseTranslationOptions,
17
- type UseTranslationResponse,
18
- useTranslation as useTranslationDefault,
19
- } from "react-i18next";
20
- import initializeI18n from "./i18nFrontend.js";
21
-
22
- function useTranslation(
23
- options?: UseTranslationOptions<"translation">,
24
- ): UseTranslationResponse<"translation", "translation"> & {
25
- t: ReturnType<typeof initializeI18n>["t"];
26
- } {
27
- const i18n = initializeI18n();
28
- return useTranslationDefault("translation", { i18n, ...options });
29
- }
30
-
31
- export default useTranslation;