Skip to content

Commit 8389414

Browse files
Feature/locale function (#986)
* [feature]: 2.2.8 - Expose locale function * Add validation * Version bumpies
1 parent 509e69f commit 8389414

File tree

10 files changed

+59
-11
lines changed

10 files changed

+59
-11
lines changed

packages/core/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ const onboard = Onboard({
174174
{ name: 'MetaMask', url: 'https://metamask.io' },
175175
{ name: 'Coinbase', url: 'https://wallet.coinbase.com/' }
176176
]
177-
}
177+
},
178178
i18n: {
179179
en: {
180180
connect: {

packages/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@web3-onboard/core",
3-
"version": "2.2.9-alpha.1",
3+
"version": "2.2.9-alpha.2",
44
"scripts": {
55
"build": "rollup -c",
66
"dev": "rollup -c -w",

packages/core/src/constants.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ export const APP_INITIAL_STATE: AppState = {
88
enabled: true,
99
position: 'topRight',
1010
expanded: false
11-
}
11+
},
12+
locale: ''
1213
}
1314

1415
export const STORAGE_KEYS = {

packages/core/src/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import { state } from './store'
66
import {
77
addChains,
88
setWalletModules,
9-
updateAccountCenter
9+
updateAccountCenter,
10+
setLocale
1011
} from './store/actions'
1112
import { reset$, internalState$ } from './streams'
1213
import { validateInitOptions } from './validation'
@@ -24,7 +25,8 @@ const API = {
2425
get: state.get,
2526
select: state.select,
2627
actions: {
27-
setWalletModules
28+
setWalletModules,
29+
setLocale
2830
}
2931
}
3032
}

packages/core/src/store/actions.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import type {
1111
RemoveWalletAction,
1212
ResetStoreAction,
1313
SetWalletModulesAction,
14+
SetLocaleAction,
1415
UpdateAccountAction,
1516
UpdateAccountCenterAction,
1617
UpdateWalletAction,
@@ -19,6 +20,7 @@ import type {
1920

2021
import {
2122
validateAccountCenterUpdate,
23+
validateLocale,
2224
validateString,
2325
validateWallet,
2426
validateWalletInit
@@ -32,7 +34,8 @@ import {
3234
REMOVE_WALLET,
3335
UPDATE_ACCOUNT,
3436
UPDATE_ACCOUNT_CENTER,
35-
SET_WALLET_MODULES
37+
SET_WALLET_MODULES,
38+
SET_LOCALE
3639
} from './constants'
3740

3841
export function addChains(chains: Chain[]): void {
@@ -161,3 +164,18 @@ export function setWalletModules(wallets: WalletInit[]): void {
161164

162165
dispatch(action as SetWalletModulesAction)
163166
}
167+
168+
export function setLocale(locale: string): void {
169+
const error = validateLocale(locale)
170+
171+
if (error) {
172+
throw error
173+
}
174+
175+
const action = {
176+
type: SET_LOCALE,
177+
payload: locale
178+
}
179+
180+
dispatch(action as SetLocaleAction)
181+
}

packages/core/src/store/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ export const REMOVE_WALLET = 'remove_wallet'
66
export const UPDATE_ACCOUNT = 'update_account'
77
export const UPDATE_ACCOUNT_CENTER = 'update_account_center'
88
export const SET_WALLET_MODULES = 'set_wallet_modules'
9+
export const SET_LOCALE= 'set_locale'

packages/core/src/store/index.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { BehaviorSubject, Subject, Observable } from 'rxjs'
22
import { distinctUntilKeyChanged, pluck, filter } from 'rxjs/operators'
3+
import { locale } from 'svelte-i18n'
4+
import type { Chain, WalletModule } from '@web3-onboard/common'
5+
36
import { APP_INITIAL_STATE } from '../constants'
47
import { notNullish } from '../utils'
5-
import type { Chain, WalletModule } from '@web3-onboard/common'
68

79
import type {
810
AppState,
@@ -11,7 +13,8 @@ import type {
1113
UpdateWalletAction,
1214
AddWalletAction,
1315
UpdateAccountAction,
14-
UpdateAccountCenterAction
16+
UpdateAccountCenterAction,
17+
Locale
1518
} from '../types'
1619

1720
import {
@@ -22,7 +25,8 @@ import {
2225
RESET_STORE,
2326
UPDATE_ACCOUNT,
2427
UPDATE_ACCOUNT_CENTER,
25-
SET_WALLET_MODULES
28+
SET_WALLET_MODULES,
29+
SET_LOCALE
2630
} from './constants'
2731

2832
function reducer(state: AppState, action: Action): AppState {
@@ -115,6 +119,15 @@ function reducer(state: AppState, action: Action): AppState {
115119
}
116120
}
117121

122+
case SET_LOCALE: {
123+
// Set the locale in the svelte-i18n internal state
124+
locale.set(payload as Locale)
125+
return {
126+
...state,
127+
locale: payload as Locale
128+
}
129+
}
130+
118131
case RESET_STORE:
119132
return APP_INITIAL_STATE
120133

packages/core/src/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ export interface AppState {
9494
walletModules: WalletModule[]
9595
wallets: WalletState[]
9696
accountCenter: AccountCenter
97+
locale: Locale
9798
}
9899

99100
export type InternalState = {
@@ -135,6 +136,7 @@ export type Action =
135136
| UpdateAccountAction
136137
| UpdateAccountCenterAction
137138
| SetWalletModulesAction
139+
| SetLocaleAction
138140

139141
export type AddChainsAction = { type: 'add_chains'; payload: Chain[] }
140142
export type AddWalletAction = { type: 'add_wallet'; payload: WalletState }
@@ -169,6 +171,11 @@ export type SetWalletModulesAction = {
169171
payload: WalletModule[]
170172
}
171173

174+
export type SetLocaleAction = {
175+
type: 'set_locale'
176+
payload: string
177+
}
178+
172179
// ==== MISC ==== //
173180
export type ChainStyle = {
174181
icon: string

packages/core/src/validation.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ const walletModule = Joi.object({
9797

9898
const walletInit = Joi.array().items(Joi.function()).required()
9999

100+
const locale = Joi.string()
101+
100102
const accountCenterPosition = Joi.string().valid(
101103
'topRight',
102104
'bottomRight',
@@ -200,3 +202,7 @@ export function validateAccountCenterUpdate(
200202
export function validateWalletInit(data: WalletInit[]): ValidateReturn {
201203
return validate(walletInit, data)
202204
}
205+
206+
export function validateLocale(data: string): ValidateReturn {
207+
return validate(locale, data)
208+
}

packages/react/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@web3-onboard/react",
3-
"version": "2.1.7-alpha.1",
3+
"version": "2.1.7-alpha.2",
44
"description": "Collection of React Hooks for web3-onboard",
55
"module": "dist/index.js",
66
"browser": "dist/index.js",
@@ -21,7 +21,7 @@
2121
"typescript": "^4.5.5"
2222
},
2323
"dependencies": {
24-
"@web3-onboard/core": "^2.2.9-alpha.1",
24+
"@web3-onboard/core": "^2.2.9-alpha.2",
2525
"@web3-onboard/common": "^2.0.7"
2626
},
2727
"peerDependencies": {

0 commit comments

Comments
 (0)