Skip to content

Commit e2cc337

Browse files
Add docs for snap_getPreferences (#1681)
* Add docs for snap_getPreferences * add what's new * Update docs/whats-new.md --------- Co-authored-by: Alexandra Carrillo <[email protected]>
1 parent 8a97a35 commit e2cc337

File tree

4 files changed

+53
-7
lines changed

4 files changed

+53
-7
lines changed

docs/whats-new.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ of the [MetaMask developer page](https://metamask.io/developer/).
1717
- Updated [Wallet landing page](/wallet) and added [Connect to MetaMask](/wallet/connect) section
1818
with SDK, third-party libraries, and Wallet API connection options.
1919
([#1494](https://github.com/MetaMask/metamask-docs/pull/1494))
20+
- Documented [`snap_getPreferences`](/snaps/reference/snaps-api/#snap_getpreferences).
21+
([#1681](https://github.com/MetaMask/metamask-docs/pull/1681))
2022

2123
## September 2024
2224

snaps/features/localization.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ title and description) in the user's language.
1313
### 1. Get the user's language
1414

1515
In your Snap's code, determine the user's language by using the
16-
[`snap_getLocale`](../reference/snaps-api.md#snap_getlocale) API method.
17-
To call `snap_getLocale`, first request the required permission by adding it to the
16+
[`snap_getPreferences`](../reference/snaps-api.md#snap_getpreferences) API method.
17+
To call `snap_getPreferences`, first request the required permission by adding it to the
1818
`initialPermissions` field in your manifest file:
1919

2020
```json title="snap.manifest.json"
2121
"initialPermissions": {
22-
"snap_getLocale": {}
22+
"snap_getPreferences": {}
2323
}
2424
```
2525

26-
Your Snap can then call `snap_getLocale` to get the user's language code (for example, `en` or `es`).
26+
Your Snap can then call `snap_getPreferences` to get the user's language code (for example, `en` or `es`).
2727

2828
### 2. Localize the Snap's UI
2929

@@ -65,7 +65,9 @@ export const locales = {
6565
export type Locale = keyof typeof locales
6666

6767
export async function getMessage(id: keyof (typeof locales)[Locale]) {
68-
const locale = (await snap.request({ method: "snap_getLocale" })) as Locale
68+
const { locale } = (await snap.request({ method: "snap_getPreferences" })) as {
69+
locale: Locale
70+
}
6971
const { message } = locales[locale]?.[id] ?? locales[FALLBACK_LANGUAGE][id]
7072

7173
return message
@@ -113,7 +115,7 @@ The following is an example of a localized manifest file:
113115
"locales": ["locales/da.json", "locales/en.json", "locales/nl.json"]
114116
},
115117
"initialPermissions": {
116-
"snap_getLocale": {}
118+
"snap_getPreferences": {}
117119
},
118120
"manifestVersion": "0.1"
119121
}

snaps/how-to/get-allowlisted.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ The following is a list of permissions that do not require allowlisting:
162162
- [`endowment:transaction-insight`](../reference/permissions.md#endowmenttransaction-insight)
163163
- [`snap_dialog`](../reference/snaps-api.md#snap_dialog)
164164
- [`snap_getLocale`](../reference/snaps-api.md#snap_getlocale)
165+
- [`snap_getPreferences`](../reference/snaps-api.md#snap_getpreferences)
165166
- [`snap_manageState`](../reference/snaps-api.md#snap_managestate)
166167
- [`snap_notify`](../reference/snaps-api.md#snap_notify)
167168

snaps/reference/snaps-api.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,10 +484,15 @@ console.log(contents)
484484
</TabItem>
485485
</Tabs>
486486

487-
## `snap_getLocale`
487+
## `snap_getLocale` (deprecated)
488488

489489
Gets the user's locale setting. You can use this method to localize text in your snap.
490490

491+
:::warning
492+
This method is deprecated.
493+
Use [`snap_getPreferences`](#snap_getpreferences) instead.
494+
:::
495+
491496
#### Returns
492497

493498
The user's locale setting as a [language code](https://github.com/MetaMask/metamask-extension/blob/develop/app/_locales/index.json).
@@ -545,6 +550,42 @@ await snap.request({
545550
</TabItem>
546551
</Tabs>
547552

553+
## `snap_getPreferences`
554+
555+
Gets the user's preferences.
556+
557+
#### Returns
558+
559+
An object containing the user's preferences:
560+
561+
- `locale` - The user's locale setting as a language code.
562+
- `currency` - The user's preferred fiat currency code.
563+
564+
#### Example
565+
566+
```tsx title="index.tsx"
567+
import { Box, Text } from "@metamask/snaps-sdk/jsx";
568+
569+
const { locale } = await snap.request({ method: "snap_getPreferences" });
570+
571+
let greeting = "Hello";
572+
if(locale === "es") {
573+
greeting = "Hola";
574+
}
575+
576+
await snap.request({
577+
method: "snap_dialog",
578+
params: {
579+
type: "alert",
580+
content: (
581+
<Box>
582+
<Text>{greeting}</Text>
583+
</Box>
584+
),
585+
},
586+
});
587+
```
588+
548589
## `snap_manageAccounts`
549590

550591
Manages [account management Snap](../features/custom-evm-accounts/index.md) accounts.

0 commit comments

Comments
 (0)