从右到左支持
为您的应用程序添加从右到左(RTL)支持的分步指南
概述
虽然大多数语言是从左到右(LTR)书写的,但有些语言是从右到左(RTL)书写的。 这些语言包括阿拉伯语、希伯来语、波斯语和乌尔都语,全球总共有超过五亿人使用这些语言。
请查看英文和阿拉伯文的 General Translation 主页,以展示 ltr 和 rtl 渲染之间的区别。
指南
要为您的应用程序添加 RTL 支持,我们只需要确定语言的方向并相应地设置屏幕上文本的方向。
您可以使用 generaltranslation
库中的 getLocaleDirection()
函数来确定语言是 "rtl"
还是 "ltr"
。
使用 getLocale()
函数在根布局中获取区域设置,然后将 lang
和 dir
属性添加到 <html>
标签:
import { getLocale } from 'gt-next'
import { getLocaleDirection } from 'generaltranslation';
export default async function RootLayout({ children }) {
const locale = await getLocale(); // e.g. "ar" for Arabic
const dir = getLocaleDirection(locale); // e.g. "rtl" for "right-to-left"
return (
<html lang={locale} dir={dir}>
<body>
{children}
</body>
</html>
)
}
这份指南怎么样?