String localization
This is a recipe showing how to localize strings based on the user's context.
skylight-export-Recipe_ String Localization-01.zip
3KB
Binary
String Localization Recipe Application Bundle

Example of application responding to user locale updates
The application data for this recipe stores the translated string values:
{
"defaultLocale": "en",
"strings": {
"welcome": {
"en": "Welcome!",
"es": "Bienvenidas!",
"de": "Willkommen!",
"fr": "Bienvenue!",
"pt": "Bem vindo!",
"it": "Benvenuta!",
"zh": "欢迎!",
"ja": "ようこそ!"
}
}
}
If the user's locale is set to "Domain Default",
skylight.user.locale
will return an empty string. There is currently no way to retrieve the domain's default locale, so we store the default locale in application data (which will need to be updated whenever the domain default is updated). The root view contains a single card that has its text bound to:
{{ application.data.strings.welcome[user.locale ? user.locale : application.data.defaultLocale] }}
This databinding accesses the appropriate welcome string from application data using the user's locale. If the user's locale is an empty string, we use the default locale specified by the application data.
This card also has an
onSelect
action that runs the following script when selected:const applicationStrings = skylight.application.data.strings
const welcomeStrings = applicationStrings.welcome
// If user.locale is an empty string, that means that it is currently set to the domain default.
// In the future, the domain default locale will be accessible through the scripting API; for now
// we resort to storing it in a value in application data
const locale = skylight.user.locale ? skylight.user.locale : skylight.application.data.defaultLocale
const localizedWelcome = welcomeStrings[locale]
skylight.showNotification({
body: localizedWelcome
})
This script uses a logic similar to the text databinding to show a localized notification.
For more details on retrieving the user's locale using the scripting API, see https://docs.upskill.io/development/reference/scripting-api#user-locale
Last modified 2yr ago