Multi-language Messaging
How to send messages in different languages with OneSignal.
You can send multi-language messages across most channels in OneSignal. The best way to send a multi-language message varies based on the channel:
Channel | Sending Method |
---|---|
Push | Dashboard or API using language codes or Segments, Google Sheets Extension |
Dashboard or API using variable substitution or Segments | |
In-App Messages | Dashboard using Segments |
SMS | Dashboard or API using Segments, Google Sheets Extension |
Set User's Language
The OneSignal SDK will automatically detect and set the user's Language property based on the device settings. You can also override this property with the SDK or API.
SDK setLanguage
Method
setLanguage
MethodThe Language property is set automatically by the OneSignal SDK based on the device settings. However, you can override this by passing in the ISO 639-1 language code to the SDK setLanguage
method.
Update User API
Update the device's language with the Edit device endpoint using an ISO 639-1 language code.
request.body = "{\"properties\":{\"language\":\"fr\"}"
Self-Managed Language and Content (API only)
If you store / manage the language of your users outside of OneSignal, you can create already-translated versions of messages and send them individually to each customer. You can send a translated message through the Create notification endpoint.
API custom_data
Property
custom_data
PropertyUsing Templates and the Create notification endpoint, you can pass in the user's language via the custom_data
parameter and reference it in the template via Liquid syntax. The below example shows how you can setup the template and pass the customer's custom_data
language and conditionally check for the language using Liquid syntax.
{% if message.custom_data.language == "en" %}
Hello, {{ first_name }}!
{% elsif message.custom_data.language == "fr" %}
Bonjour {{ first_name }}!
{% else %}
Hi, {{ first_name }}!
{% endif %}
request.body = "{\"custom_data\":{\"language\":\"fr\"}"
Bonjour George!
Push
Dashboard Sending
The OneSignal dashboard and API makes "English" the default language, but use any language you need in this field. Think of "English" as Default. For example if you enter a Spanish message into the "Any/English" field, then all users will get the notification in Spanish.
To send a message in multiple languages, select Add Languages or Edit Language.
Select the languages you want to use in the push. Any languages you do not select will get the "Any/English" message.
You must provide the translation for each message you want to include.
Google Sheets Extension
Google Sheets Extension allows you to store message templates as a spreadsheet to send the message directly from the Google sheet.
Follow the steps outlined in Google Sheets Extension to compose and send your message.
API
The API headings
and contents
properties both allow multiple language codes to be set. If a device is set to a language that is not present in the contents
property, then the content in en
will be used.
For example, if the receiving device's language is de
and you send the notification with only en
and fr
language codes for contents
and headings
, the notification the device receives will use the values of contents
and headings
in the en
language code.
// Devices with "de" language code will receive "en" for contents and headings
// because "de" is not specified
{
"contents": {"en": "English content", "fr": "French content"},
"headings": {"en": "English heading", "fr": "French heading"}
}
Note: Make sure that you are including the same set of language codes for each field.
If the language code set in contents
is not present in headings
, then the title ("heading") will not display.
For example, if the receiving device's language is de
and you send the notification with en
, ru
and de
for contents
, but send only en
for headings
, then the titles for that notification will not display for any device with a language code of de
and ru
.
// Devices with "de" & "ru" language code will not display headings
// because "de" & "ru" are not specified
{
"contents": {"en": "English content", "ru": "Russian content", "de": "German content"},
"headings": {"en": "English content"}
}
Email
To send multi-language Emails via the dashboard, you will need to choose one of the following approaches:
- Use Segments with Language and send messages to each segment, or
- Use property substitution with Liquid syntax to create conditional statements in a message
Right-to-Left Text Support in Email
Because text alignment is set in the email styling, sending right-to-left text and left-to-right messages using the same template may be challenging to accomplish, though not impossible.
You might want to either rely on the the recipient's computer to manage right-to-left text display, segment users who need to receive right-to-left messages, or conditionally change the styling through custom HTML & CSS.
Dashboard - Segment Audience
To send a language-specific Email to each language you need to support:
- Create a Segment for each language.
- Create an Email template for each language.
- Send your Email for each language by selecting the Segment and template for that language.
Dashboard - Conditionally Check Language with Liquid
Use Liquid Syntax to add conditional logic in the body of a message to render content in several languages. You can use the language code {{ user.language }}
, or set a Data Tag on the user to provide the language value.
When using an if/else
statement for multi-language messages, always include your default language last in the final else
statement.
For example, if you want to use English as your default language, use an if
statement to check if the user's language matches all defined languages before defaulting to English. This template example will render into three languages depending on the recipient's language code or Data Tag for {{ language }}
.
{% assign userLang = user.language %}
{% if userLang == 'es' %}
Hola {{ first_name }}!
{% elsif userLang == 'fr' %}
Bonjour {{ first_name }}!
{% else %}
Hello {{ first_name }}!
{% endif %}
API
When calling the Create notification API endpoint, you can specify custom_data
to use in Liquid syntax. You can use data added to the notification to show conditional multi-language content as demonstrated above in "Dashboard - Conditionally Check Language with Liquid".
Make sure to accurately reference the source of your data in your Liquid tags.
{% if message.custom_data.language == "fr" %}
Bonjour {{ name }}
{% elsif message.custom_data.language == "en" %}
Hola {{ name }}
{% else %}
Hello {{ name }}
{% endif %}
{
"app_id": "5eb5a37e-b458-11e3-ac11-000c2940e62c",
"template_id": "45d24a11-f739-4878-a15c-f32535547e90",
"include_email_tokens": ["[email protected]", "[email protected]"],
"custom_data": {
"language": "fr",
"cart_items": [
{"item_name": "sweater", "img_url": "https://.."},
{"item_name": "socks", "img_url": "https://.."}
]
}
}
Bonjour George
In-App Messages
Dashboard - Segment Audience
To send a language-specific In-App Message to each language you need to support:
- Create a Segment for each language.
- Create an In-App Message for each language.
- Send your In-App Message for each language by selecting the Segment for that language.
Supported Languages
Language | Language Code |
---|---|
English | en |
Arabic | ar |
Azerbaijani | az |
Bosnian | bs |
Catalan | ca |
Chinese (Simplified) | zh-Hans |
Chinese (Traditional) | zh-Hant |
Croatian | hr |
Czech | cs |
Danish | da |
Dutch | nl |
Estonian | et |
Finnish | fi |
French | fr |
Georgian | ka |
Bulgarian | bg |
German | de |
Greek | el |
Hindi | hi |
Hebrew | he |
Hungarian | hu |
Indonesian | id |
Italian | it |
Japanese | ja |
Korean | ko |
Latvian | lv |
Lithuanian | lt |
Malay | ms |
Norwegian | nb |
Persian | fa |
Polish | pl |
Portuguese | pt |
Punjabi | pa |
Romanian | ro |
Russian | ru |
Serbian | sr |
Slovak | sk |
Spanish | es |
Swedish | sv |
Thai | th |
Turkish | tr |
Ukrainian | uk |
Vietnamese | vi |
Updated over 1 year ago