Skip to content

Commit

Permalink
Add docs for snap_getPreferences (#1681)
Browse files Browse the repository at this point in the history
* Add docs for snap_getPreferences

* add what's new

* Update docs/whats-new.md

---------

Co-authored-by: Alexandra Carrillo <[email protected]>
  • Loading branch information
ziad-saab and alexandratran authored Oct 29, 2024
1 parent 8a97a35 commit e2cc337
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 7 deletions.
2 changes: 2 additions & 0 deletions docs/whats-new.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ of the [MetaMask developer page](https://metamask.io/developer/).
- Updated [Wallet landing page](/wallet) and added [Connect to MetaMask](/wallet/connect) section
with SDK, third-party libraries, and Wallet API connection options.
([#1494](https://github.com/MetaMask/metamask-docs/pull/1494))
- Documented [`snap_getPreferences`](/snaps/reference/snaps-api/#snap_getpreferences).
([#1681](https://github.com/MetaMask/metamask-docs/pull/1681))

## September 2024

Expand Down
14 changes: 8 additions & 6 deletions snaps/features/localization.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ title and description) in the user's language.
### 1. Get the user's language

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

```json title="snap.manifest.json"
"initialPermissions": {
"snap_getLocale": {}
"snap_getPreferences": {}
}
```

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

### 2. Localize the Snap's UI

Expand Down Expand Up @@ -65,7 +65,9 @@ export const locales = {
export type Locale = keyof typeof locales

export async function getMessage(id: keyof (typeof locales)[Locale]) {
const locale = (await snap.request({ method: "snap_getLocale" })) as Locale
const { locale } = (await snap.request({ method: "snap_getPreferences" })) as {
locale: Locale
}
const { message } = locales[locale]?.[id] ?? locales[FALLBACK_LANGUAGE][id]

return message
Expand Down Expand Up @@ -113,7 +115,7 @@ The following is an example of a localized manifest file:
"locales": ["locales/da.json", "locales/en.json", "locales/nl.json"]
},
"initialPermissions": {
"snap_getLocale": {}
"snap_getPreferences": {}
},
"manifestVersion": "0.1"
}
Expand Down
1 change: 1 addition & 0 deletions snaps/how-to/get-allowlisted.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ The following is a list of permissions that do not require allowlisting:
- [`endowment:transaction-insight`](../reference/permissions.md#endowmenttransaction-insight)
- [`snap_dialog`](../reference/snaps-api.md#snap_dialog)
- [`snap_getLocale`](../reference/snaps-api.md#snap_getlocale)
- [`snap_getPreferences`](../reference/snaps-api.md#snap_getpreferences)
- [`snap_manageState`](../reference/snaps-api.md#snap_managestate)
- [`snap_notify`](../reference/snaps-api.md#snap_notify)

Expand Down
43 changes: 42 additions & 1 deletion snaps/reference/snaps-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -484,10 +484,15 @@ console.log(contents)
</TabItem>
</Tabs>

## `snap_getLocale`
## `snap_getLocale` (deprecated)

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

:::warning
This method is deprecated.
Use [`snap_getPreferences`](#snap_getpreferences) instead.
:::

#### Returns

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

## `snap_getPreferences`

Gets the user's preferences.

#### Returns

An object containing the user's preferences:

- `locale` - The user's locale setting as a language code.
- `currency` - The user's preferred fiat currency code.

#### Example

```tsx title="index.tsx"
import { Box, Text } from "@metamask/snaps-sdk/jsx";

const { locale } = await snap.request({ method: "snap_getPreferences" });

let greeting = "Hello";
if(locale === "es") {
greeting = "Hola";
}

await snap.request({
method: "snap_dialog",
params: {
type: "alert",
content: (
<Box>
<Text>{greeting}</Text>
</Box>
),
},
});
```

## `snap_manageAccounts`

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

0 comments on commit e2cc337

Please sign in to comment.