Support for Right-to-left Languages in the Responsive Components and Templates (APL 1.7)
(This is not the most recent version of this document. Use the Other Versions option to see the documentation for the most recent version of this document)
You can use the responsive components and templates to display content in right-to-left languages such as Arabic.
alexa-layouts
package 1.4.0 or later.Right-to-left support in the components and templates
The responsive components and templates display content in either a left-to-right or right-to-left reading direction.
For example, the AlexaHeader
component displays an optional back button, header title text, attribution text or an attribution icon. The following images show the difference between the left-to-right and right-to-left reading directions.
Some components don't display text, but users still read and interact with the content in a reading direction. For example, the AlexaProgressBar
displays a bar that shows ongoing activity or progress. For left-to-right, the bar starts on the left and moves to the right to show completion. For right-to-left, the bar starts on the right and moves to the left.
The following image shows how a progress bar looks in both left-to-right and right-to-left orientation. Both versions of the bar represent two minutes of total time with one minute elapsed.
Components that don't display content in a reading direction, such as AlexaBackground
and AlexaCheckbox
, don't provide any support for the right-to-left layout direction.
Language-specific fonts in the components and templates
Some devices provide language-specific fonts. For example, a device might support an Arabic version of the standard fonts. The responsive components and templates that display text support using these fonts.
The following images show the AlexaButton
and AlexaFooter
components with the default fonts and with Arabic-specific fonts.
Set the layout direction and language
The responsive components and templates provide properties to set the layout direction and language:
-
The
layoutDirection
property is available on each component and template that supports right-to-left content. SetlayoutDirection
to eitherLTR
orRTL
.Some responsive components can inherit
layoutDirection
from the parent component. See the documentation for a given responsive component to determine whether the component can inheritlayoutDirection
. -
The
lang
property is available on each component and template that displays text. Setlang
to the locale code for the content. For example, to use Arabic-specific fonts, setlang
toar-SA
.
Use data-binding to set the layoutDirection
and lang
properties to values in your data source. When your skill constructs the data source to send with the RenderDocument
directive, set these properties in the data source based on the locale
provided in the request.
Bind layoutDirection and lang on a component
To set the layout direction and language for a responsive component
-
In the top-level
Container
component in your template, createbind
variables calledLayoutDirection
andLang
. Bind these properties to properties your data source. Capitalize the names of these custom properties to distinguish them from the component propertieslayoutDirection
andlang
.The following example shows a
Container
that creates twobind
variables. The variables get their values from a data source calledbindExample
. All child components of thisContainer
have access to thesebind
variables.{ "type": "Container", "width": "100%", "height": "100%", "bind": [ { "name": "LayoutDirection", "value": "${payload.bindExample.requestLanguage.layoutDirection}" }, { "name": "Lang", "value": "${payload.bindExample.requestLanguage.lang}" } ], "items": [] }
-
Set the
layoutDirection
on theContainer
to the bound variable.Primitive APL components can inherit the
layoutDirection
from the parent component, so it's useful to set this property on theContainer
as well. Some responsive components can also inherit this value. See the documentation for a given responsive component to determine whether the component inheritslayoutDirection
from the parent container or not."layoutDirection": "${LayoutDirection}"
-
Add the responsive component to the
Container
and bind thelayoutDirection
on the responsive component to the bound variable. If the component displays text, also bindlang
to the bound variable.{ "type": "AlexaRating", "ratingNumber": "${payload.bindExample.properties.rating.number}", "ratingText": "${payload.bindExample.properties.rating.text}", "spacing": "@spacingMedium", "layoutDirection": "${LayoutDirection}", "lang": "${Lang}" }
-
In your skill, set the properties in your data source to reflect the layout direction and language, based on the locale of the user's request.
The following example shows an excerpt of a data source that sets the layout direction and language.
{ "bindExample": { "type": "object", "objectId": "bindExample", "requestLanguage": { "layoutDirection": "RTL", "lang": "ar-SA" }, "properties": {} } }
The following document and data source examples illustrate placing responsive components in a Container
and setting the layoutDirection
and lang
properties:
- The top-level
Container
createsbind
variables for both properties and also setslayoutDirection
. - The primitive
Text
component andAlexaFooter
responsive component can both inheritlayoutDirection
from theContainer
. Therefore, these components don't setlayoutDirection
. Because these components display text, they also setlang
to use language-specific fonts. - The
AlexaHeader
andAlexaRating
responsive components can't inheritlayoutDirection
from theContainer
. Therefore, these components set bothlayoutDirection
andlang
. - The
AlexaBackground
responsive component doesn't display any text or other content that needs a reading direction, so it doesn't includelayoutDirection
orlang
.
The following images show this document in both left-to-right and right-to-left orientations.
Bind layoutDirection and lang on a template
A responsive template is a complete viewport design that fills the entire viewport. You don't normally combine the template with other components.
Place the template in the mainTemplate
section of the document and bind the layoutDirection
and lang
properties on the template to properties in your data source.
To set the layout direction and language for a responsive template
- Add the template to the
mainTemplate
property of the APL document. -
Bind the
layoutDirection
andlang
properties on the template to properties in your data source.The following example shows the
AlexaHeadline
template with thelayoutDirection
andlang
properties bound to the data source.{ "mainTemplate": { "parameters": [ "payload" ], "items": [ { "type": "AlexaHeadline", "layoutDirection": "${payload.headlineData.responseLanguage.layoutDirection}", "lang": "${payload.headlineData.responseLanguage.language}", "primaryText": "${payload.headlineData.textContent.primaryText}", "secondaryText": "${payload.headlineData.textContent.secondaryText}", "footerHintText": "${payload.headlineData.textContent.footerHintText}", "headerAttributionText": "${payload.headlineData.textContent.headerAttributionText}", "headerAttributionImage": "https://s3.amazonaws.com/ask-skills-assets/apl-layout-assets/attribution_dark_hub_prime.png", "headerBackButton": true, "headerBackButtonAccessibilityLabel": "back", "headerDivider": false, "headerTitle": "${payload.headlineData.textContent.title}", "headerSubtitle": "${payload.headlineData.textContent.subTitle}" } ] } }
-
In your skill, set the properties in your data source to reflect the layout direction and language, based on the locale of the user's request.
The following example shows an excerpt of a data source that sets the layout direction and language for the
AlexaHeadline
example.{ "headlineData": { "responseLanguage": { "layoutDirection": "RTL", "language": "ar-SA" }, "textContent": {} } }
The following examples show the complete document and two data sources, one with English content and one with Arabic content. The data source specifies the values to use for layoutDirection
and lang
, so the AlexaHeadline
template automatically adjusts.
The following images show the results for both the English and Arabic examples.
Components and templates with right-to-left support
The following responsive components display in left-to-right or right-to-left:
- AlexaButton
- AlexaFooter
- AlexaHeader
- AlexaImageListItem
- AlexaProgressBar
- AlexaRating
- AlexaSlider
- AlexaSwipeToAction
- AlexaSwitch
- AlexaTextListItem
The following responsive templates display in left-to-right or right-to-left:
Related topics
- Base Component Properties
layoutDirection
component property- Text Component
- EditText Component
- Responsive Components
- Responsive Templates
Last updated: Nov 28, 2023