Salesforce uses custom labels to present text based on a user's language setting. The values on the custom labels can be accessed from your Apex classes and view definitions, enabling you to show localized text in your Slack app.
Slack and Salesforce User Locales
Your Slack app publishes text across channel messages, ephemeral messages, and modals. You can't localize text in actions like in slash commands or shortcuts, although you can localize the response to those actions.
Ephemeral messages and modals target a Salesforce user and are personalized with that user's language settings. The Slack user's locale is employed if the user isn’t authenticated as a Salesforce user.
Channel messages are shown in English. Starting in Summer '22, you can enable localization on a channel.
note: Using the Slack user locale to show a localized label in your Slack app isn't supported. Currently, your Slack locale must match the Salesforce locale for labels to be localized correctly.
A user interacts with the app directly one-on-one via direct conversations on:
- The app page
- The app's Messages tab
In this case, the Slack app posts messages with the Salesforce user's language. Otherwise, the Slack app responds in English. But direct messages between two users and the app also use each user's language instead of a common language.
Update the User Language
Salesforce supports various end-user languages.
To update the user language, from your org's personal settings, go to the Language & Time Zone page. See Edit Your Language and Locale Settings.
Work with Custom Labels
Use a custom label to present information in a user's native language. You can access the label from your Apex code or an expression in your view definition.
To add a custom label in your org, from Setup, go to the Custom Labels page and add your labels. See Custom Labels.
Access the Label
In Apex, use this syntax to access your label.
System.Label.LabelName
If you're using a namespace, use this syntax.
System.Label.Namespace.LabelName
For string substitution, use FORMAT.
{ !FORMAT(label.LabelName , args) }
Use Custom Labels
Localized labels are supported for managed and unmanaged packages.
Use {!label.labelName} or {!label.Namespace.labelName} in an expression to reference your custom labels in a view definition.
important: All expressions in view definitions are case-sensitive. For example, {!label.labelName} works but {!Label.labelName} doesn't.
This example uses the c namespace and assumes that you created custom labels in your org with the corresponding label names.
description: "A context block with custom labels"
components:
- definition: context
name: "context_block"
components:
- definition: image
properties:
url: "https://via.placeholder.com/150"
altText: "{!label.c.AltTextLabel}"
- definition: text
properties:
text:
type: "mrkdwn"
text: "{!label.c.MarkdownLabel}"
- definition: context
components:
- definition: text
properties:
text: "{!label.c.TextLabel}"
Alternatively, use an Apex data provider or access your label from your Apex code.
// Apex data provider
String myLabel = Label.LabelName; // Define the custom label
Slack.ViewReference viewReference = Slack.View.myviewname.get(); // Get the view
viewReference.setParameter('labelValue', myLabel); // Assign the label to the view property
In your view, define the property for your custom label.
schema:
properties:
labelValue:
type: string
defaultValue: "Display custom label here"
Define an expression that references the custom label.
- definition: section
properties:
text: "{!view.properties.labelValue}"
Format Dates and Time in Slack
Slack formats date and time by using a built-in formatter. Your dates and time are dynamically localized to your time zone if you use Slack's date format syntax <!date^timestamp^token_string^optional_link|fallback_text>.
To generate Slack's date format syntax in your expressions, use the FORMATSLACKDATE key.
{!FORMATSLACKDATE(epochSeconds, string_including_dateFormat)}
This example formats a view property by using the {date_short} and {time_secs} tokens.
"{!FORMATSLACKDATE(view.properties.epochSeconds,'{date_short} {time_secs}')}"
Similarly, you can add the formatting tokens to a string.
"{!FORMATSLACKDATE(view.properties.recordCreateDate, 'Your record was created on {date_long} at {time_secs} in your timezone.')}"
tip: Slack supports various tokens to handle date and time formatting. For more information, see Date formatting.
In your view definition, use the mrkdwn component to support date and time formatting.
components:
- definition: section
properties:
text:
text: "{!FORMATSLACKDATE(view.properties.epochSeconds,label.help.dateTimeToken)}\n Here are the available commands for this app"
type: "mrkdwn"
disableEncoding: true
Dates render in the time zone of the device running the Slack client. We recommend that you use the time zone from your Salesforce org. If you change the time zone of the client device, all timestamps in past messages and in future messages render by using the new time zone. To rerender a current message in the Slack client, scroll away and back again to the message.
Considerations and Limitations
Labels that the Slack language provides always use your Slack language setting. These labels aren't localized if your Slack and Salesforce locales are different. For example, Slack provides these labels, which appear in the Slack language setting.
- (optional) label on an optional input field
- Press 'enter' to submit label for the hint text that appears below an input field
- Close button label on a modal
- Only visible to you label on an ephemeral message
To ensure that your labels are localized correctly, change your Slack language to match the Salesforce language.
Enable Channel Localization
Slack apps translate messages based on a user's language. However, channel messages use English by default.
To enable your channel messages to use the Slack channel's language, include these bot token scopes in your app at api.slack.com in the OAuth & Permissions page.
users:readchannels:readgroups:readim:readmpim:read
If your app uses a user token, add the scopes under the user token scopes as well.
Similarly, update your app definition file (YourSlackAppName.slackapp-meta.xml) to include the tokens by using the <botScopes> or <userScopes> field.