i18n-keyless-react 1.2.0 → 1.3.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.
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import React from "react";
|
|
2
2
|
import { useI18nKeyless } from "./store";
|
|
3
|
-
type ComponentProps = NonNullable<ReturnType<typeof useI18nKeyless.getState>["config"]>["component"] extends React.ComponentType<infer P> ? P :
|
|
3
|
+
type ComponentProps = NonNullable<ReturnType<typeof useI18nKeyless.getState>["config"]>["component"] extends React.ComponentType<infer P> ? P : {
|
|
4
|
+
children: string;
|
|
5
|
+
};
|
|
4
6
|
/**
|
|
5
7
|
* the component should be a React component
|
|
6
8
|
*
|
|
@@ -10,14 +12,16 @@ type ComponentProps = NonNullable<ReturnType<typeof useI18nKeyless.getState>["co
|
|
|
10
12
|
* }
|
|
11
13
|
* ```
|
|
12
14
|
*
|
|
13
|
-
* Then you can pass MyComponent's props to i18n-keyless'
|
|
15
|
+
* Then you can pass MyComponent's props to i18n-keyless' I18nText
|
|
14
16
|
*
|
|
15
17
|
* ```ts
|
|
16
|
-
* <
|
|
18
|
+
* <I18nText anyprop can fit>My text to translate</I18nText>
|
|
17
19
|
* ```
|
|
18
20
|
*/
|
|
19
21
|
type MyI18nTextProps = Omit<ComponentProps, "children"> & {
|
|
20
22
|
children: string;
|
|
21
23
|
};
|
|
22
|
-
export declare const
|
|
24
|
+
export declare const I18nText: React.FC<MyI18nTextProps> & {
|
|
25
|
+
isI18nTextComponent?: boolean;
|
|
26
|
+
};
|
|
23
27
|
export {};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
2
|
import React, { useEffect } from "react";
|
|
3
3
|
import { useI18nKeyless } from "./store";
|
|
4
|
-
export const
|
|
4
|
+
export const I18nText = ({ children, ...textProps }) => {
|
|
5
5
|
const translations = useI18nKeyless((store) => store.translations);
|
|
6
6
|
const currentLanguage = useI18nKeyless((store) => store.currentLanguage);
|
|
7
7
|
const config = useI18nKeyless((store) => store.config);
|
|
@@ -13,7 +13,8 @@ export const MyI18nText = ({ children, ...textProps }) => {
|
|
|
13
13
|
if (!config) {
|
|
14
14
|
return null;
|
|
15
15
|
}
|
|
16
|
-
const translatedText = translations[children] || children;
|
|
16
|
+
const translatedText = currentLanguage === config.languages.primary ? children : translations[children] || children;
|
|
17
17
|
const TextComponent = config.component || React.Fragment;
|
|
18
18
|
return (_jsx(TextComponent, { ...textProps, children: translatedText }, currentLanguage));
|
|
19
19
|
};
|
|
20
|
+
I18nText.isI18nTextComponent = true;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { I18nText } from "./I18nText";
|
|
2
2
|
export { init, getTranslation, setCurrentLanguage, useCurrentLanguage, clearI18nKeylessStorage, fetchAllTranslations, } from "./store";
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { I18nText } from "./I18nText";
|
|
2
2
|
export { init, getTranslation, setCurrentLanguage, useCurrentLanguage, clearI18nKeylessStorage, fetchAllTranslations, } from "./store";
|
package/dist/package.json
CHANGED
package/dist/store.js
CHANGED
|
@@ -134,7 +134,7 @@ export const useI18nKeyless = create((set, get) => ({
|
|
|
134
134
|
set({ translating: { ...get().translating, [key]: true } });
|
|
135
135
|
}
|
|
136
136
|
if (config?.handleTranslate) {
|
|
137
|
-
|
|
137
|
+
await config?.handleTranslate?.(key);
|
|
138
138
|
}
|
|
139
139
|
else {
|
|
140
140
|
const body = {
|
|
@@ -199,9 +199,9 @@ export async function init(config) {
|
|
|
199
199
|
if (!config.languages.supported.includes(config.languages.initWithDefault)) {
|
|
200
200
|
config.languages.supported.push(config.languages.initWithDefault);
|
|
201
201
|
}
|
|
202
|
-
if (!config.component) {
|
|
203
|
-
|
|
204
|
-
}
|
|
202
|
+
// if (!config.component) {
|
|
203
|
+
// console.warn("i18n-keyless: component is not provided, using default component React.Fragment");
|
|
204
|
+
// }
|
|
205
205
|
if (!config.storage) {
|
|
206
206
|
throw new Error("i18n-keyless: storage is required. You can use react-native-mmkv, @react-native-async-storage/async-storage, or window.localStorage, or any storage that has a getItem, setItem, removeItem, or get, set, and remove method");
|
|
207
207
|
}
|