@rango-dev/widget-embedded 0.5.1-next.2 → 0.5.1-next.21

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 (81) hide show
  1. package/dist/QueueManager.d.ts.map +1 -1
  2. package/dist/Wallets.d.ts +14 -0
  3. package/dist/Wallets.d.ts.map +1 -0
  4. package/dist/Widget.d.ts +3 -3
  5. package/dist/Widget.d.ts.map +1 -1
  6. package/dist/components/AppRouter.d.ts.map +1 -1
  7. package/dist/components/BottomLogo.d.ts.map +1 -1
  8. package/dist/components/ChangeSlippageButton.d.ts.map +1 -1
  9. package/dist/components/ConfirmSwapWarnings.d.ts +2 -2
  10. package/dist/components/ConfirmSwapWarnings.d.ts.map +1 -1
  11. package/dist/components/HeaderButtons.d.ts.map +1 -1
  12. package/dist/components/Layout.d.ts.map +1 -1
  13. package/dist/components/SwapDetailsPlaceholder.d.ts.map +1 -1
  14. package/dist/components/TokenInfo.d.ts.map +1 -1
  15. package/dist/components/TokenPreview.d.ts.map +1 -1
  16. package/dist/components/WidgetEvents.d.ts +2 -0
  17. package/dist/components/WidgetEvents.d.ts.map +1 -0
  18. package/dist/components/warnings/HighSlippageWarning.d.ts.map +1 -1
  19. package/dist/components/warnings/MinRequiredSlippage.d.ts.map +1 -1
  20. package/dist/hooks/useConfirmSwap.d.ts +1 -1
  21. package/dist/hooks/useConfirmSwap.d.ts.map +1 -1
  22. package/dist/hooks/useWalletProviders.d.ts +6 -0
  23. package/dist/hooks/useWalletProviders.d.ts.map +1 -0
  24. package/dist/index.d.ts +5 -2
  25. package/dist/index.d.ts.map +1 -1
  26. package/dist/index.js +1 -1
  27. package/dist/index.js.map +4 -4
  28. package/dist/pages/Home.d.ts.map +1 -1
  29. package/dist/pages/WalletsPage.d.ts +2 -2
  30. package/dist/pages/WalletsPage.d.ts.map +1 -1
  31. package/dist/store/wallets.d.ts +2 -2
  32. package/dist/store/wallets.d.ts.map +1 -1
  33. package/dist/types/config.d.ts +8 -2
  34. package/dist/types/config.d.ts.map +1 -1
  35. package/dist/types/swap.d.ts +1 -1
  36. package/dist/types/swap.d.ts.map +1 -1
  37. package/dist/utils/providers.d.ts +11 -0
  38. package/dist/utils/providers.d.ts.map +1 -0
  39. package/dist/utils/routing.d.ts +1 -1
  40. package/dist/utils/routing.d.ts.map +1 -1
  41. package/dist/utils/swap.d.ts.map +1 -1
  42. package/dist/utils/wallets.d.ts.map +1 -1
  43. package/package.json +10 -13
  44. package/src/QueueManager.tsx +0 -25
  45. package/src/Wallets.tsx +105 -0
  46. package/src/Widget.tsx +41 -84
  47. package/src/components/AppRouter.tsx +0 -2
  48. package/src/components/BottomLogo.tsx +2 -4
  49. package/src/components/ChangeSlippageButton.tsx +3 -3
  50. package/src/components/ConfirmSwapWarnings.tsx +10 -13
  51. package/src/components/HeaderButtons.tsx +7 -9
  52. package/src/components/Layout.tsx +10 -11
  53. package/src/components/SwapDetailsPlaceholder.tsx +10 -2
  54. package/src/components/TokenInfo.tsx +26 -23
  55. package/src/components/TokenPreview.tsx +11 -12
  56. package/src/components/WidgetEvents.tsx +45 -0
  57. package/src/components/warnings/BalanceErrors.tsx +3 -3
  58. package/src/components/warnings/HighSlippageWarning.tsx +9 -2
  59. package/src/components/warnings/MinRequiredSlippage.tsx +11 -4
  60. package/src/hooks/useConfirmSwap.ts +6 -3
  61. package/src/hooks/useWalletProviders.ts +20 -0
  62. package/src/index.ts +57 -1
  63. package/src/pages/ConfirmSwapPage.tsx +5 -5
  64. package/src/pages/Home.tsx +4 -5
  65. package/src/pages/WalletsPage.tsx +10 -7
  66. package/src/store/wallets.ts +42 -17
  67. package/src/types/config.ts +8 -2
  68. package/src/types/swap.ts +1 -1
  69. package/src/utils/providers.ts +53 -0
  70. package/src/utils/routing.ts +1 -1
  71. package/src/utils/swap.ts +26 -13
  72. package/src/utils/wallets.ts +31 -11
  73. package/dist/hooks/useSelectLanguage.d.ts +0 -5
  74. package/dist/hooks/useSelectLanguage.d.ts.map +0 -1
  75. package/dist/i18n.d.ts +0 -3
  76. package/dist/i18n.d.ts.map +0 -1
  77. package/dist/mockData/pendingSwap.d.ts +0 -2
  78. package/dist/mockData/pendingSwap.d.ts.map +0 -1
  79. package/src/hooks/useSelectLanguage.ts +0 -15
  80. package/src/i18n.js +0 -30
  81. package/src/mockData/pendingSwap.ts +0 -607
@@ -18,13 +18,15 @@ import { useWallets } from '@rango-dev/wallets-core';
18
18
  import { useUiStore } from '../store/ui';
19
19
  import { useNavigateBack } from '../hooks/useNavigateBack';
20
20
  import { navigationRoutes } from '../constants/navigationRoutes';
21
- import { useTranslation } from 'react-i18next';
21
+ import { i18n } from '@lingui/core';
22
22
  import { useMetaStore } from '../store/meta';
23
23
  import { Spinner } from '@rango-dev/ui';
24
24
  import { LoadingFailedAlert } from '@rango-dev/ui';
25
+ import { WidgetConfig } from '../types';
26
+ import { configWalletsToWalletName } from '../utils/providers';
25
27
 
26
28
  interface PropTypes {
27
- supportedWallets?: WalletType[];
29
+ supportedWallets: WidgetConfig['wallets'];
28
30
  multiWallets: boolean;
29
31
  }
30
32
 
@@ -48,13 +50,16 @@ const LoaderContainer = styled('div', {
48
50
  const AlertContainer = styled('div', {
49
51
  paddingBottom: '$16',
50
52
  });
53
+
54
+ const ALL_SUPPORTED_WALLETS = Object.values(WalletTypes);
55
+
51
56
  export function WalletsPage({ supportedWallets, multiWallets }: PropTypes) {
52
57
  const { navigateBackFrom } = useNavigateBack();
53
58
  const { state, disconnect, getWalletInfo, connect } = useWallets();
54
59
  const wallets = getlistWallet(
55
60
  state,
56
61
  getWalletInfo,
57
- supportedWallets || Object.values(WalletTypes)
62
+ configWalletsToWalletName(supportedWallets) || ALL_SUPPORTED_WALLETS
58
63
  );
59
64
  const walletsRef = useRef<WalletInfo[]>();
60
65
 
@@ -66,7 +71,6 @@ export function WalletsPage({ supportedWallets, multiWallets }: PropTypes) {
66
71
  const toggleConnectWalletsButton =
67
72
  useUiStore.use.toggleConnectWalletsButton();
68
73
  const loadingMetaStatus = useMetaStore.use.loadingStatus();
69
- const { t } = useTranslation();
70
74
 
71
75
  const onSelectWallet = async (type: WalletType) => {
72
76
  const wallet = state(type);
@@ -110,10 +114,9 @@ export function WalletsPage({ supportedWallets, multiWallets }: PropTypes) {
110
114
 
111
115
  return (
112
116
  <SecondaryPage
113
- title={t('Select Wallet') || ''}
117
+ title={i18n.t('Select Wallet') || ''}
114
118
  textField={false}
115
- onBack={navigateBackFrom.bind(null, navigationRoutes.wallets)}
116
- >
119
+ onBack={navigateBackFrom.bind(null, navigationRoutes.wallets)}>
117
120
  <>
118
121
  {walletErrorMessage && (
119
122
  <AlertContainer>
@@ -45,7 +45,7 @@ interface WalletsStore {
45
45
  initSelectedWallets: () => void;
46
46
  setSelectedWallet: (wallet: SelectableWallet) => void;
47
47
  clearConnectedWallet: () => void;
48
- getOneOfWalletsDetails: (account: Wallet) => void;
48
+ getWalletsDetails: (accounts: Wallet[], shouldRetry?: boolean) => void;
49
49
  setCustomDestination: (customDestination: string) => void;
50
50
  }
51
51
 
@@ -56,7 +56,7 @@ export const useWalletsStore = createSelectors(
56
56
  selectedWallets: [],
57
57
  customDestination: '',
58
58
  connectWallet: (accounts) => {
59
- const getOneOfWalletsDetails = get().getOneOfWalletsDetails;
59
+ const getWalletsDetails = get().getWalletsDetails;
60
60
  set((state) => ({
61
61
  connectedWallets: state.connectedWallets
62
62
  .filter((wallet) => wallet.walletType !== accounts[0].walletType)
@@ -72,7 +72,7 @@ export const useWalletsStore = createSelectors(
72
72
  }))
73
73
  ),
74
74
  }));
75
- accounts.forEach(async (account) => getOneOfWalletsDetails(account));
75
+ getWalletsDetails(accounts);
76
76
  },
77
77
  disconnectWallet: (walletType) => {
78
78
  set((state) => ({
@@ -131,27 +131,50 @@ export const useWalletsStore = createSelectors(
131
131
  connectedWallets: [],
132
132
  selectedWallets: [],
133
133
  })),
134
- getOneOfWalletsDetails: async (account: Wallet) => {
135
- const tokens = useMetaStore.getState().meta.tokens;
134
+ getWalletsDetails: async (accounts, shouldRetry = true) => {
135
+ const getWalletsDetails = get().getWalletsDetails;
136
+ const { tokens } = useMetaStore.getState().meta;
136
137
  set((state) => ({
137
- connectedWallets: state.connectedWallets.map((balance) => {
138
- return balance.address === account.address &&
139
- balance.chain === account.chain
140
- ? { ...balance, loading: true }
141
- : balance;
138
+ connectedWallets: state.connectedWallets.map((wallet) => {
139
+ return accounts.find((account) =>
140
+ isAccountAndWalletMatched(account, wallet)
141
+ )
142
+ ? { ...wallet, loading: true }
143
+ : wallet;
142
144
  }),
143
145
  }));
144
146
  try {
145
- const response = await httpService().getWalletsDetails([
146
- { address: account.address, blockchain: account.chain },
147
- ]);
148
- const retrivedBalance = response.wallets[0];
147
+ const data = accounts.map(({ address, chain }) => ({
148
+ address,
149
+ blockchain: chain,
150
+ }));
151
+ const response = await httpService().getWalletsDetails(data);
152
+ const retrivedBalance = response.wallets;
149
153
  if (retrivedBalance) {
150
154
  set((state) => ({
151
155
  connectedWallets: state.connectedWallets.map(
152
156
  (connectedWallet) => {
153
- return isAccountAndWalletMatched(account, connectedWallet)
154
- ? makeBalanceFor(account, retrivedBalance, tokens)
157
+ const matchedAccount = accounts.find((account) =>
158
+ isAccountAndWalletMatched(account, connectedWallet)
159
+ );
160
+ const retrivedBalanceAccount = retrivedBalance.find(
161
+ (balance) =>
162
+ balance.address === connectedWallet.address &&
163
+ balance.blockChain === connectedWallet.chain
164
+ );
165
+ if (
166
+ retrivedBalanceAccount?.failed &&
167
+ matchedAccount &&
168
+ shouldRetry
169
+ ) {
170
+ getWalletsDetails([matchedAccount], false);
171
+ }
172
+ return matchedAccount && retrivedBalanceAccount
173
+ ? makeBalanceFor(
174
+ matchedAccount,
175
+ retrivedBalanceAccount,
176
+ tokens
177
+ )
155
178
  : connectedWallet;
156
179
  }
157
180
  ),
@@ -160,7 +183,9 @@ export const useWalletsStore = createSelectors(
160
183
  } catch (error) {
161
184
  set((state) => ({
162
185
  connectedWallets: state.connectedWallets.map((balance) => {
163
- return isAccountAndWalletMatched(account, balance)
186
+ return accounts.find((account) =>
187
+ isAccountAndWalletMatched(account, balance)
188
+ )
164
189
  ? resetConnectedWalletState(balance)
165
190
  : balance;
166
191
  }),
@@ -1,5 +1,7 @@
1
1
  import { Asset } from 'rango-sdk';
2
2
  import { WalletType } from '@rango-dev/wallets-shared';
3
+ import { ProviderInterface } from '@rango-dev/wallets-core';
4
+ import { Language } from '@rango-dev/ui';
3
5
 
4
6
  /**
5
7
  * The above type defines a set of optional color properties for a widget.
@@ -100,6 +102,9 @@ export type BlockchainAndTokenConfig = {
100
102
  * @property {WidgetTheme} theme - The `theme` property is a part of the `WidgetConfig` type and is
101
103
  * used to specify the visual theme of the widget. It is of type `WidgetTheme`, which is an interface
102
104
  * that defines the various properties of the theme, such as colors, fonts, and others.
105
+ * @property {boolean} externalWallets
106
+ * If `externalWallets` is `true`, you should add `WidgetWallets` to your app.
107
+
103
108
  */
104
109
  export type WidgetConfig = {
105
110
  apiKey: string;
@@ -108,9 +113,10 @@ export type WidgetConfig = {
108
113
  from?: BlockchainAndTokenConfig;
109
114
  to?: BlockchainAndTokenConfig;
110
115
  liquiditySources?: string[];
111
- wallets?: WalletType[];
116
+ wallets?: (WalletType | ProviderInterface)[];
112
117
  multiWallets?: boolean;
113
118
  customDestination?: boolean;
114
- language?: string;
119
+ language?: Language;
115
120
  theme?: WidgetTheme;
121
+ externalWallets?: boolean;
116
122
  };
package/src/types/swap.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { SwapSavedSettings } from '@rango-dev/ui/dist/types/swaps';
1
+ import { SwapSavedSettings } from 'rango-types/lib';
2
2
 
3
3
  export type PendingSwapSettings = Omit<
4
4
  SwapSavedSettings,
@@ -0,0 +1,53 @@
1
+ import { allProviders } from '@rango-dev/provider-all';
2
+ import { ProviderInterface } from '@rango-dev/wallets-core';
3
+ import { WidgetConfig } from '../types';
4
+
5
+ /**
6
+ *
7
+ * Generate a list of providers by passing a provider name (e.g. metamask) or a custom provider which implemented ProviderInterface.
8
+ * @returns ProviderInterface[] a list of ProviderInterface
9
+ *
10
+ */
11
+ export function matchAndGenerateProviders(
12
+ providers: WidgetConfig['wallets']
13
+ ): ProviderInterface[] {
14
+ const all = allProviders();
15
+
16
+ if (providers) {
17
+ const selectedProviders: ProviderInterface[] = [];
18
+
19
+ providers.forEach((requestedProvider) => {
20
+ // There are two types of provider we get, the first one is only passing the wallet name
21
+ // then we will match the wallet name with our providers (@rango-dev/provider-*).
22
+ // The second way is passing a custom provider which implemented ProviderInterface.
23
+ if (typeof requestedProvider === 'string') {
24
+ const result: ProviderInterface | undefined = all.find((provider) => {
25
+ return provider.config.type === requestedProvider;
26
+ });
27
+ if (result) {
28
+ selectedProviders.push(result);
29
+ } else {
30
+ console.warn(
31
+ `Couldn't find ${requestedProvider} provider. Please make sure you are passing the correct name.`
32
+ );
33
+ }
34
+ } else {
35
+ // It's a custom provider so we directly push it to the list.
36
+ selectedProviders.push(requestedProvider);
37
+ }
38
+ });
39
+ return selectedProviders;
40
+ }
41
+
42
+ return all;
43
+ }
44
+
45
+ export function configWalletsToWalletName(
46
+ config: WidgetConfig['wallets']
47
+ ): string[] {
48
+ const providers = matchAndGenerateProviders(config);
49
+ const names = providers.map((provider) => {
50
+ return provider.config.type;
51
+ });
52
+ return names;
53
+ }
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  SimulationAssetAndAmount,
3
3
  SimulationValidationStatus,
4
- } from '@rango-dev/ui/dist/types/swaps';
4
+ } from '@rango-dev/ui/dist/widget/ui/src/types/swaps';
5
5
  import BigNumber from 'bignumber.js';
6
6
  import { BestRouteResponse, BlockchainMeta, Token } from 'rango-sdk';
7
7
  import { areEqual } from './common';
package/src/utils/swap.ts CHANGED
@@ -6,6 +6,7 @@ import {
6
6
  SwapResult,
7
7
  Token,
8
8
  } from 'rango-sdk';
9
+ import { i18n } from '@lingui/core';
9
10
  import { ConnectedWallet } from '../store/wallets';
10
11
  import { ZERO } from '../constants/numbers';
11
12
  import { numberToString } from './numbers';
@@ -132,30 +133,32 @@ export function getSwapButtonState(
132
133
  inputAmount: string
133
134
  ): SwapButtonState {
134
135
  if (loadingMetaStatus !== 'success')
135
- return { title: 'Connect Wallet', disabled: true };
136
+ return { title: `${i18n.t('Connect Wallet')}`, disabled: true };
136
137
  if (connectedWallets.length == 0)
137
- return { title: 'Connect Wallet', disabled: false };
138
- if (loading) return { title: 'Finding Best Route...', disabled: true };
138
+ return { title: `${i18n.t('Connect Wallet')}`, disabled: false };
139
+ if (loading)
140
+ return { title: `${i18n.t('Finding Best Route...')}`, disabled: true };
139
141
  else if (!inputAmount || inputAmount === '0')
140
- return { title: 'Enter an amount', disabled: true };
142
+ return { title: `${i18n.t('Enter an amount')}`, disabled: true };
141
143
  else if (!bestRoute || !bestRoute.result)
142
- return { title: 'Swap', disabled: true };
143
- else if (hasLimitError) return { title: 'Limit Error', disabled: true };
144
+ return { title: `${i18n.t('Swap')}`, disabled: true };
145
+ else if (hasLimitError)
146
+ return { title: `${i18n.t('Limit Error')}`, disabled: true };
144
147
  else if (highValueLoss)
145
- return { title: 'Price impact is too high!', disabled: true };
148
+ return { title: `${i18n.t('Price impact is too high!')}`, disabled: true };
146
149
  else if (priceImpactCanNotBeComputed)
147
150
  return {
148
- title: 'USD price is unknown, price impact might be high!',
151
+ title: `${i18n.t('USD price is unknown, price impact might be high!')}`,
149
152
  disabled: false,
150
153
  hasWarning: true,
151
154
  };
152
155
  else if (needsToWarnEthOnPath)
153
156
  return {
154
- title: 'The route goes through Ethereum. Continue?',
157
+ title: `${i18n.t('The route goes through Ethereum. Continue?')}`,
155
158
  disabled: false,
156
159
  hasWarning: true,
157
160
  };
158
- else return { title: 'Swap', disabled: false };
161
+ else return { title: `${i18n.t('Swap')}`, disabled: false };
159
162
  }
160
163
 
161
164
  export function canComputePriceImpact(
@@ -349,9 +352,19 @@ export function createBestRouteRequestBody(
349
352
 
350
353
  const filteredBlockchains = removeDuplicateFrom(
351
354
  (initialRoute?.result?.swaps || []).reduce(
352
- (blockchains: string[], swap) => (
353
- blockchains.push(swap.from.blockchain, swap.to.blockchain), blockchains
354
- ),
355
+ (blockchains: string[], swap) => {
356
+ blockchains.push(swap.from.blockchain, swap.to.blockchain);
357
+ // Check if internalSwaps array exists
358
+ if (swap.internalSwaps && Array.isArray(swap.internalSwaps)) {
359
+ swap.internalSwaps.map((internalSwap) => {
360
+ blockchains.push(
361
+ internalSwap.from.blockchain,
362
+ internalSwap.to.blockchain
363
+ );
364
+ });
365
+ }
366
+ return blockchains;
367
+ },
355
368
  []
356
369
  )
357
370
  );
@@ -93,13 +93,18 @@ export function prepareAccountsForWalletStore(
93
93
  const result: Wallet[] = [];
94
94
 
95
95
  function addAccount(network: Network, address: string) {
96
- const newAccount: Wallet = {
97
- address,
98
- chain: network,
99
- walletType: wallet,
100
- };
96
+ const accountForChainAlreadyExists = !!result.find(
97
+ (account) => account.chain === network
98
+ );
99
+ if (!accountForChainAlreadyExists) {
100
+ const newAccount: Wallet = {
101
+ address,
102
+ chain: network,
103
+ walletType: wallet,
104
+ };
101
105
 
102
- result.push(newAccount);
106
+ result.push(newAccount);
107
+ }
103
108
  }
104
109
 
105
110
  const supportedChains = supportedChainNames || [];
@@ -154,12 +159,26 @@ export function getRequiredChains(route: BestRouteResponse | null) {
154
159
  route?.result?.swaps.forEach((swap) => {
155
160
  const currentStepFromBlockchain = swap.from.blockchain;
156
161
  const currentStepToBlockchain = swap.to.blockchain;
157
- let lastAddedWallet = wallets[wallets.length - 1];
158
- if (currentStepFromBlockchain != lastAddedWallet)
162
+ if (!wallets.includes(currentStepFromBlockchain)) {
159
163
  wallets.push(currentStepFromBlockchain);
160
- lastAddedWallet = wallets[wallets.length - 1];
161
- if (currentStepToBlockchain != lastAddedWallet)
164
+ }
165
+ if (!wallets.includes(currentStepToBlockchain)) {
162
166
  wallets.push(currentStepToBlockchain);
167
+ }
168
+
169
+ // Check if internalSwaps array exists
170
+ if (swap.internalSwaps && Array.isArray(swap.internalSwaps)) {
171
+ swap.internalSwaps.forEach((internalSwap) => {
172
+ const internalStepFromBlockchain = internalSwap.from.blockchain;
173
+ const internalStepToBlockchain = internalSwap.to.blockchain;
174
+ if (!wallets.includes(internalStepFromBlockchain)) {
175
+ wallets.push(internalStepFromBlockchain);
176
+ }
177
+ if (!wallets.includes(internalStepToBlockchain)) {
178
+ wallets.push(internalStepToBlockchain);
179
+ }
180
+ });
181
+ }
163
182
  });
164
183
  return wallets;
165
184
  }
@@ -234,7 +253,8 @@ export function isAccountAndWalletMatched(
234
253
  ) {
235
254
  return (
236
255
  account.address === connectedWallet.address &&
237
- account.chain === connectedWallet.chain
256
+ account.chain === connectedWallet.chain &&
257
+ account.walletType === connectedWallet.walletType
238
258
  );
239
259
  }
240
260
 
@@ -1,5 +0,0 @@
1
- export default function useSelectLanguage(): {
2
- changeLanguage: (value: string) => void;
3
- currentLanguage: string;
4
- };
5
- //# sourceMappingURL=useSelectLanguage.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"useSelectLanguage.d.ts","sourceRoot":"","sources":["../../src/hooks/useSelectLanguage.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,OAAO,UAAU,iBAAiB;4BAIR,MAAM;;EAQtC"}
package/dist/i18n.d.ts DELETED
@@ -1,3 +0,0 @@
1
- export default i18n;
2
- import i18n from "i18next";
3
- //# sourceMappingURL=i18n.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"i18n.d.ts","sourceRoot":"","sources":["../src/i18n.js"],"names":[],"mappings":""}
@@ -1,2 +0,0 @@
1
- export declare const pendingSwap: any;
2
- //# sourceMappingURL=pendingSwap.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"pendingSwap.d.ts","sourceRoot":"","sources":["../../src/mockData/pendingSwap.ts"],"names":[],"mappings":"AACA,eAAO,MAAM,WAAW,EAAE,GA6lBzB,CAAC"}
@@ -1,15 +0,0 @@
1
- import { useTranslation } from 'react-i18next';
2
-
3
- export default function useSelectLanguage() {
4
- const { i18n } = useTranslation();
5
- // @ts-ignore
6
- const currentLanguage = i18n?.language || 'en';
7
- const changeLanguage = (value: string) => {
8
- if (currentLanguage !== value) {
9
- // @ts-ignore
10
- i18n.changeLanguage(value);
11
- }
12
- };
13
-
14
- return { changeLanguage, currentLanguage };
15
- }
package/src/i18n.js DELETED
@@ -1,30 +0,0 @@
1
- import i18n from 'i18next';
2
- import { initReactI18next } from 'react-i18next';
3
- import en from './languages/en.json';
4
- import tr from './languages/tr.json';
5
- import Backend from 'i18next-http-backend';
6
- import LanguageDetector from 'i18next-browser-languagedetector';
7
-
8
- const languages = ['en', 'tr'];
9
-
10
- i18n
11
- .use(Backend)
12
- // detect user language
13
- .use(LanguageDetector)
14
- // pass the i18n instance to react-i18next.
15
- .use(initReactI18next)
16
- .init({
17
- resources: {
18
- en: {
19
- translation: en,
20
- },
21
- tr: {
22
- translation: tr,
23
- },
24
- },
25
- fallbackLng: 'en',
26
- debug: true,
27
- whitelist: languages,
28
- });
29
-
30
- export default i18n;