Skip to content

Commit 7eeac39

Browse files
authored
Set lang and dir attributes on tweet bodies (#170)
Tweets can be written in different languages. Language metadata is essential for browsers to correctly render multilingual texts because the different language uses different shapes of glyphs for the same Unicode codepoints, and the browsers must select fonts based on the intended language[^1]. Twitter API infers the primary language of each tweet, and this patch sets the `lang` attribute to the inferred language for the tweet bodies. `dir="auto"` is also necessary for proper rendering of texts in the right-to-left languages, such as Arabic, Hebrew, and Persian. [^1]: Regarding font selection in CJK languages: <https://heistak.github.io/your-code-displays-japanese-wrong/>.
1 parent bbd2bab commit 7eeac39

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

packages/react-tweet/src/twitter-theme/quoted-tweet/quoted-tweet-body.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import s from './quoted-tweet-body.module.css'
44
type Props = { tweet: EnrichedQuotedTweet }
55

66
export const QuotedTweetBody = ({ tweet }: Props) => (
7-
<p className={s.root}>
7+
<p className={s.root} lang={tweet.lang} dir="auto">
88
{tweet.entities.map((item, i) => (
99
<span key={i} dangerouslySetInnerHTML={{ __html: item.text }} />
1010
))}

packages/react-tweet/src/twitter-theme/tweet-body.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { TweetLink } from './tweet-link.js'
33
import s from './tweet-body.module.css'
44

55
export const TweetBody = ({ tweet }: { tweet: EnrichedTweet }) => (
6-
<p className={s.root}>
6+
<p className={s.root} lang={tweet.lang} dir="auto">
77
{tweet.entities.map((item, i) => {
88
switch (item.type) {
99
case 'hashtag':

0 commit comments

Comments
 (0)