SIP-10: snap_getLocale
Author | Frederik Bolding, Hassan Malik |
---|---|
Discussions-To | https://github.com/MetaMask/SIPs/discussions/99 |
Status | Draft |
Created | 2023-06-30 |
Table of Contents
Abstract
This SIP proposes an RPC method that lets snaps access the user selected locale in MetaMask, as a way to inform their localization efforts. This proposal will outline implementation details of said RPC method.
Motivation
Snaps that want to localize their copy used in custom interfaces etc. currently have to implement their own system for letting a user select their preferred language. The proposed RPC method provides a developer experience improvement to snap developers by letting them use the user’s existing and preferred localization settings.
Specification
Formal specifications are written in TypeScript.
Language
The key words “MUST”, “MUST NOT”, “REQUIRED”, “SHALL”, “SHALL NOT”, “SHOULD”, “SHOULD NOT”, “RECOMMENDED”, “NOT RECOMMENDED”, “MAY”, and “OPTIONAL” written in uppercase in this document are to be interpreted as described in RFC 2119
Proposed implementation
The proposed RPC method snap_getLocale
SHOULD be a restricted RPC method requiring user consent before usage via the permission system. The RPC method SHOULD only be available to Snaps.
The implementation MUST use a getLocale
method hook for accessing the user locale. This lets each implementing client specify the function themselves, letting the RPC implementation remain platform-agnostic.
The client MUST return an IETF BCP 47 formatted locale string.
/**
* Builds the method implementation for `snap_getLocale`.
*
* @param hooks - The RPC method hooks.
* @param hooks.getLocale - A function that returns the user selected locale.
* @returns The user selected locale.
*/
export function getImplementation({ getLocale }: GetLocaleMethodHooks) {
return async function implementation(
_args: RestrictedMethodOptions<void>,
): Promise<string> {
return getLocale();
};
}
/**
* Method hook for accessing the user locale from the PreferencesController.
*
* @returns The user selected locale.
*/
function getLocale(): string {
return this.preferencesController.store.getState().currentLocale;
}
Copyright
Copyright and related rights waived via CC0.
Citation
Please cite this document as:
Frederik Bolding, Hassan Malik, "SIP-10: snap_getLocale [DRAFT]," Snaps Improvement Proposals, no. 10, June 2023. [Online serial]. Available: https://github.com/MetaMask/SIPs/blob/master/SIPS/sip-10.md