In 2017, Airbnb supported 27 languages and had developed robust translation tools that made it easy to add more. We launched Croatian in May with little overhead beyond setting up the new domain and translating phrases. However, this was not true for all new languages; our next most requested language, Hebrew, posed a unique challenge. Because it reads right-to-left, the entire Hebrew UI must be flipped. Browsers only handle reversing the DOM structure, but styling and interactions must be coded manually.
This talk covers the journey of enabling right-to-left languages on airbnb.com. Recently, Airbnb has moved to a React frontend and away from Sass to a CSS-in-JS paradigm. We developed a performant and cross-browser solution for RTL that leveraged a CSS-in-JS abstraction layer to isolate the logic from our codebase. Our efforts led us most of the way to launching in Arabic and Hebrew while requiring little effort from our product engineers and with minimal disruption to their work.
3. DotheRight(toLeft)Thing
DIRECTIONAL CONTENT IN REACT
Maha()ماحا
Lives in New York City
Studies English at NYU
“A terminally pessimistic
and culturally
dispossessed teenager”
Khalid()خالد
Lives in Cairo, Egypt
Getting an MBA from
Cairo University
“A chronic misfit with a
creepy side-kick friend”
@majapw
3 Do the Right (to Left) Thing (PDF) - May 20, 2018
4. DotheRight(toLeft)Thing
DIRECTIONAL CONTENT IN REACT
Maha()ماحا
Lives in New York City
Studies English at NYU
“A terminally pessimistic
and culturally
dispossessed teenager”
Khalid()خالد
Lives in Cairo, Egypt
Getting an MBA from
Cairo University
“A chronic misfit with a
creepy side-kick friend”
@majapw
4 Do the Right (to Left) Thing (PDF) - May 20, 2018
9. DotheRight(toLeft)Thing
DIRECTIONAL CONTENT IN REACT
•Why support right-to-left languages at all?
•Why is it hard to do so?
•What tools are already available?
•How did we solve this problem at Airbnb?
@majapw
9 Do the Right (to Left) Thing (PDF) - May 20, 2018
12. [http://www.internetworldstats.com/stats5.htm]
العربية
ARABIC ON THE WEB
•58.7% of the Middle East is on the Internet.
•Over 90% of the population of Qatar and the
UAE have access to the Internet.
•40% of the Arab Digital Generation uses the
Internet for at least 5 hours a day.
@majapw
12 Do the Right (to Left) Thing (PDF) - May 20, 2018
13. العربية
ARABIC ON THE WEB
•4-5% of Internet users speak Arabic.
•Arabic-speaking Internet users have
grown 7250% since 2000.
•60% of Arabic speakers would prefer
content in their own language.
•Arabic speakers prefer blogging and
other similar platforms so asto
createthatArabiccontent.
[http://www.internetworldstats.com/stats7.htm] @majapw
13 Do the Right (to Left) Thing (PDF) - May 20, 2018
14. العربية
ARABIC ON THE WEB
[https://w3techs.com/technologies/overview/content_language/all]
[http://funredes.org/lc2017/]
Only0.8%to3%
oftopwebsites
haveArabiccontent
Atlas Mountains, Morocco
@majapw
14 Do the Right (to Left) Thing (PDF) - May 20, 2018
15. WHY IS THIS HARD?
@majapw
15 Do the Right (to Left) Thing (PDF) - May 20, 2018
28. <DirectionProvider/>
Sets the direction attribute based on a global language setting on a top-level node
withDirection()
Wraps individual components and consumes the direction as a prop, allowing for customization
based on its value.
@majapw
react-with-direction
28 Do the Right (to Left) Thing (PDF) - May 20, 2018
29. render() {
const { count } = this.state;
return (
<div>
{`Step ${count + 1}`}
</div>
);
}
react-with-direction
DirectionProvider
@majapw
29 Do the Right (to Left) Thing (PDF) - May 20, 2018
30. render() {
const { count } = this.state;
return (
<div>
{`Step ${count + 1}`}
<IconChevronRight />
</div>
);
}
react-with-direction
DirectionProvider
@majapw
30 Do the Right (to Left) Thing (PDF) - May 20, 2018
34. export default function IconChevronNext({ ...props }) {
...
}
react-with-direction
withDirection
@majapw
34 Do the Right (to Left) Thing (PDF) - May 20, 2018
35. export default function IconChevronNext({ ...props }) {
return <IconChevronRight {...props} />;
}
react-with-direction
withDirection
@majapw
35 Do the Right (to Left) Thing (PDF) - May 20, 2018
36. function IconChevronNext({ direction, ...props }) {
return <IconChevronRight {...props} />;
}
export default withDirection(IconChevronNext);
react-with-direction
withDirection
@majapw
36 Do the Right (to Left) Thing (PDF) - May 20, 2018
37. function IconChevronNext({ direction, ...props }) {
if (direction === DIRECTIONS.RTL) {
return <IconChevronLeft {...props} />;
}
return <IconChevronRight {...props} />;
}
export default withDirection(IconChevronNext);
react-with-direction
withDirection
@majapw
37 Do the Right (to Left) Thing (PDF) - May 20, 2018
41. Whataboutstyle?
AUTOMATED OPTIONS
R2 (ded/R2) - a CSS LTR ∞ RTL converter for i18n friendly layouts
css-flip (twitter/css-flip) - a CSS BiDi flipper
rtlcss (MohammadYounes/rtlcss) - Framework for transforming CSS from LTR to RTL
cssjanus (cssjanus/cssjanus) - Flip CSS stylesheets between left-to-right and right-to-left
webpack-rtl-plugin (romainberger/webpack-rtl-plugin) - Webpack plugin to produce a rtl css bundle
@majapw
41 Do the Right (to Left) Thing (PDF) - May 20, 2018
42. R2 (ded/R2) - a CSS LTR ∞ RTL converter for i18n friendly layouts
css-flip (twitter/css-flip) - a CSS BiDi flipper
rtlcss (MohammadYounes/rtlcss) - Framework for transforming CSS from LTR to RTL
cssjanus (cssjanus/cssjanus) - Flip CSS stylesheets between left-to-right and right-to-left
webpack-rtl-plugin (romainberger/webpack-rtl-plugin) - Webpack plugin to produce a rtl CSS bundle
Whataboutstyle?
AUTOMATED OPTIONS
@majapw
42 Do the Right (to Left) Thing (PDF) - May 20, 2018
79. •Handles all directional styles.
•No changes to how developers write components.
•Handles both Aphrodite-generated classnames and inline style
objects.
•Has minimal performance impact.
AutomaticStyleFlippinginRWS
IMPLEMENTATION REQUIREMENTS
Petra, Jordan
@majapw
79 Do the Right (to Left) Thing (PDF) - May 20, 2018
81. •Create happens at compile-time.
•While resolvehappens at run-time, the method is
defined once and exported as a singleton.
Attempt#1
SOME IMPORTANT CONSIDERATIONS
This means that we do not have access to
directional context.
@majapw
81 Do the Right (to Left) Thing (PDF) - May 20, 2018
82. •We can assume all our apps have a top-leveldir
attribute that is set before the first render.
•Aphrodite has a built-in plugin system to handle
customstyleselectors.
Attempt#1
THINGS WE CAN LEVERAGE
@majapw
82 Do the Right (to Left) Thing (PDF) - May 20, 2018
84. CSS-in-JS
Attempt#1
USING APHRODITE’S PLUGIN SYSTEM
CSS
{
container: {
color: 'red',
},
}
.container_fesszw {
color: red !important;
}
@majapw
84 Do the Right (to Left) Thing (PDF) - May 20, 2018
85. {
container: {
‘:focus’: {
color: 'purple',
},
},
}
CSS-in-JS
Attempt#1
USING APHRODITE’S PLUGIN SYSTEM
CSS
.container_fesszw:focus {
color: purple !important;
}
@majapw
85 Do the Right (to Left) Thing (PDF) - May 20, 2018
86. [dir=“rtl"] .container_fesszw {
margin-left: 10px !important;
}
[dir="ltr"] .container_fesszw {
margin-right: 10px !important;
}
CSS-in-JS
Attempt#1
USING APHRODITE’S PLUGIN SYSTEM
CSS
{
container: {
_rtl: {
marginLeft: 10,
},
_ltr: {
marginRight: 10,
},
},
}
@majapw
86 Do the Right (to Left) Thing (PDF) - May 20, 2018
91. Transform into _rtl/_ltrstyles by using Kent
Dodds’ rtl-css-jslibrary.
Enable our custom plugin in Aphrodite.
[Now]Compile-timeStaticStyles
Pass unchanged to Aphrodite.
Aphrodite
react-with-styles-interface-aphrodite
react-with-styles
<Component/>
resolveStaticStyles(styles.container)
91 Do the Right (to Left) Thing (PDF) - May 20, 2018
92. [Now]Compile-timeStaticStyles
Pass unchanged to Aphrodite.
Aphrodite
react-with-styles-interface-aphrodite
react-with-styles
<Component/>
Transform into _rtl/_ltrstyles by using Kent
Dodds’ rtl-css-jslibrary.
Enable our custom plugin in Aphrodite.
resolveStaticStyles({
__name: ‘.container_rbvzuy’,
__definition: {
color: ‘red’,
float: ‘left’,
},
})
92 Do the Right (to Left) Thing (PDF) - May 20, 2018
93. [Now] Compile-time Static Styles
Pass unchanged to Aphrodite.
Transform into _rtl/_ltr-keyed styles by the
interface using Kent Dodds’ rtl-css-js library.
Enable our custom plugin in Aphrodite.
Aphrodite
react-with-styles-interface-aphrodite
react-with-styles
<Component/>
resolveStaticStyles({
__name: ‘.container_rbvzuy’,
__definition: {
_ltr: {
color: ‘red’,
float: ‘left’,
},
_rtl: rtlCSSJS({
color: ‘red’,
float: ‘left’,
}),
},
})
93 Do the Right (to Left) Thing (PDF) - May 20, 2018
94. [Now] Compile-time Static Styles
Pass unchanged to Aphrodite.
Transform into _rtl/_ltr-keyed styles by the
interface using Kent Dodds’ rtl-css-js library.
Enable our custom plugin in Aphrodite.
Aphrodite
react-with-styles-interface-aphrodite
react-with-styles
<Component/>
Compile-time Created Styles
Pass as is to Aphrodite
Transform into _rtl/_ltr-keyed styles by the
interface using Kent Dodds’ rtl-css-js library
Pass into a version of Aphrodite with a custom
plugin enabled
resolveStaticStyles({
__name: ‘.container_rbvzuy’,
__definition: {
_ltr: {
color: ‘red’,
float: ‘left’,
},
_rtl: {
color: ‘red’,
float: ‘right’,
},
},
})
94 Do the Right (to Left) Thing (PDF) - May 20, 2018
100. [Now]Run-timeDynamicStyles
Pass inline styles unchanged back to RWS.
Transform styles into Aphrodite classnames.*
Enable our custom plugin in Aphrodite.
*Only do this for directional styles.
Aphrodite
react-with-styles-interface-aphrodite
react-with-styles
<Component/>
{
left: 40
}
@majapw
100 Do the Right (to Left) Thing (PDF) - May 20, 2018
101. [Now]Run-timeDynamicStyles
Pass inline styles unchanged back to RWS.
Transform styles into Aphrodite classnames.*
Enable our custom plugin in Aphrodite.
*Only do this for directional styles.
Aphrodite
react-with-styles-interface-aphrodite
react-with-styles
<Component/>
{
__name: ‘inlineStyle_aov0j6’,
__definition: {
left: 40,
},
}
@majapw
101 Do the Right (to Left) Thing (PDF) - May 20, 2018
102. @majapw
[Now]Run-timeDynamicStyles
Pass inline styles unchanged back to RWS.
Transform styles into Aphrodite classnames.*
Enable our custom plugin in Aphrodite.
*Only do this for directional styles.
Aphrodite
react-with-styles-interface-aphrodite
react-with-styles
<Component/>
{
__name: ‘inlineStyle_aov0j6’,
__definition: {
_ltr: {
left: 40,
},
_rtl: rtlCSSJS({
left: 40,
}),
},
}
102 Do the Right (to Left) Thing (PDF) - May 20, 2018
103. @majapw
[Now]Run-timeDynamicStyles
Pass inline styles unchanged back to RWS.
Transform styles into Aphrodite classnames.*
Enable our custom plugin in Aphrodite.
*Only do this for directional styles.
Aphrodite
react-with-styles-interface-aphrodite
react-with-styles
<Component/>
{
__name: ‘inlineStyle_aov0j6’,
__definition: {
_ltr: {
left: 40,
},
_rtl: {
right: 40,
},
},
}
103 Do the Right (to Left) Thing (PDF) - May 20, 2018
104. [Now]Run-timeDynamicStyles
Pass inline styles unchanged back to RWS.
Transform styles into Aphrodite classnames.*
Enable our custom plugin in Aphrodite.
*Only do this for directional styles.
Aphrodite
react-with-styles-interface-aphrodite
react-with-styles
<Component/>
resolveStaticStyles({
__name: ‘inlineStyle_aov0j6’,
__definition: {
_ltr: {
left: 40,
},
_rtl: {
right: 40,
},
},
})
104 Do the Right (to Left) Thing (PDF) - May 20, 2018
108. •Descendent selectors do not allow for context switching.
•Descendent selectors coupled with shorthand/longhand
styles cause specificity issues.
•React 16 upgrade caused server/client mismatches due
to the way we were initializing direction and transforming
inline styles to classnames.
Attempt#1
FAILURES
@majapw
108 Do the Right (to Left) Thing (PDF) - May 20, 2018
110. Attempt#2
MODIFIED REQUIREMENTS
•Handles all directional styles.
•No changes to how developers write components.
•Handles both Aphrodite-generated classnames and inline style objects.
•Has minimal performance impact.
@majapw
110 Do the Right (to Left) Thing (PDF) - May 20, 2018
111. •Handles all directional styles.
•Changestohowdeveloperswritecomponentscaneasilybecodemodded.
•Handles both Aphrodite-generated classnames and inline style objects.
•Has minimal performance impact.
•Handlesdirectionalcontextswitching.
•Doesnotintroducespecificityissues.
•Doesnotcauseaserver/clientmismatch.
Attempt#2
MODIFIED REQUIREMENTS
@majapw
111 Do the Right (to Left) Thing (PDF) - May 20, 2018