# node: msg URL: https://generaltranslation.com/en-GB/docs/node/api/strings/msg.mdx --- title: msg description: API reference for the msg() string function --- ## Overview The `msg` function marks and encodes strings for translation. ```js const encodedString = msg('Hello, world!'); ``` The encoded string should be passed to the [`getMessages`](/docs/node/api/get-messages) function to retrieve translations. **Encoding:** `msg` encodes the input string, so you cannot use it directly in your application. If you want to retrieve the original string, you need to decode it with [`decodeMsg`](#decodemsg). Note: if you call `msg` with just a string and no options, it still returns an encoded string — not the original. Use `decodeMsg` to retrieve the original content. ## Decoding [#decodemsg] To get the original string back, you need to decode it using [`decodeMsg`](#decodemsg) ```js import { msg, decodeMsg } from 'gt-node'; const encodedString = msg('Hello, world!'); const decodedString = decodeMsg(encodedString); console.log(decodedString); // "Hello, world!" ``` ## Reference ### Parameters | Name | Type | Description | | ---------- | ----------------------------------------------------------------------------- | -------------------------------------------------------- | | `content` | `string` | The string to be encoded. | | `options?` | [`InlineTranslationOptions`](/docs/next/api/types/inline-translation-options) | Translation options to customise the behaviour of `msg`. | ### Returns An encoded string, with interpolated variables (if any) replaced by their values. *** ## Behaviour ### Production During the CD process, any content within a `msg` function will be translated before your application is deployed. This ensures fast load times for all locales, but it can only translate content known at build time. Once generated, translations are either (1) stored in the CDN or (2) stored in your app's build output, according to your configuration. From there, the translated content is served to your users. If a translation is not found, it will fall back to the original content. Make sure to follow the [deployment guide here](/docs/next/tutorials/quickdeploy). ### Development In development, the `msg` function translates content on demand. This is useful for prototyping how your app will look in different languages. Remember to add a Dev API key to your environment to enable this behaviour. You will see a delay during on-demand translation in development. This will not occur in production builds unless content is explicitly translated on demand. *** ## Example ### Basic usage You can use `msg` to mark strings for translation. ```js copy import { msg, getMessages } from 'gt-node'; const greeting = msg('Hello, world!'); const m = await getMessages(); const translated = m(greeting); console.log(translated); // "Hello, world!" (translated) ``` Note: "Hello, world!" will be translated into the user's preferred language. ### Using variables [#variables] You can pass variables to dictionary translations. ```js copy import { msg, getMessages } from 'gt-node'; const greeting = msg('Hello, {name}!', { name: 'Alice' }); const m = await getMessages(); const translated = m(greeting); console.log(translated); // "Hello, Alice!" (translated) ``` Note: "Alice" will not be translated into the user's preferred language because it is a variable. ### Using ICU message format `gt-node` supports ICU message format, which also lets you format your variables. ```js copy import { msg, getMessages } from 'gt-node'; const encodedString = msg('There are {count, plural, =0 {no items} =1 {one item} other {{count} items}} in the cart', { count: 10 }); const m = await getMessages(); const translated = m(encodedString); console.log(translated); ``` ICU message format is a powerful way to format your variables. For more information, see the [ICU message format documentation](https://unicode-org.github.io/icu/userguide/format_parse/messages/). *** ## Notes * The `msg` function marks strings for translation. * Strings translated with `msg` are processed before runtime, during the build process (unless in development). ## Next steps * See [`getMessages`](/docs/node/api/get-messages) for resolving translated strings at runtime.