RTL语言

QuibblerQuibbler 2020-03-23 约 8 分钟 2486 次阅读

RTL语言



1、RTL适配

应用国际化最让人头疼的适配就是RTL语言,诸如阿拉伯语等等文字布局都是从右到左。Android已经极力提供最简单的方法适配RTL,在AndroidManifest中的application节点中声明支持RTL:

<application
        android:supportsRtl="true"
        ...>

切换到RTL语言的时候会自动对布局进行RTL镜像切换,注意有时候可能和LTR不一样因为一些属性适配的不太好,比如使用alignStrart替代alignLeft。

2、Unicode双向算法

对于语言来说RTL可能情况会复杂一些,尤其是混合的语言,即便设置了RTL效果也并不如意。关于URL的计算涉及到复杂的Unicode双向算法。Unicode中的每个字符都有一个关联的方向属性。大多数字母都以LTR(从左到右)强类型输入。从右到左脚本的字母被强类型输入为RTL(从右到左)。从右到左将显示一系列强类型的RTL字符。这与周围的基本方向无关。

当将具有不同方向性的文本内联混合时,bidi算法会针对具有相同方向性的连续字符的每个序列生成单独的方向性偏斜。因此,在以下示例中,存在三个定向运行:

空格和标点符号在Unicode中不是强类型为LTR或RTL,因为它们可以在两种类型的脚本中使用。因此,它们被归类为中立或薄弱的角色。具有相同方向类型的两个强类型字符之间的中性字符也将假定该方向性。因此,两个RTL字符之间的中性字符将被视为RTL字符本身,并将具有扩展定向运行的效果。这就是为什么以下示例中的三个阿拉伯语单词作为一个方向游标从右向左读取的原因- 包括两个中间空格,这些空格作为中性符接周围字符的方向。

RTL脚本中的数字在从右到左的流程中从左到右运行,但是bidi算法对它们的处理方式与单词稍有不同。据说它们的方向性较弱。图片中的两个示例说明了这种差异。

3、TextView:textDirection

上面只需要了解,实际开发过程中并不需要知道具体实现细节,有优秀的语言学家和算法工程师完成底层实现。在Android中可以给TextView设置不同的属性来控制RTL方向性,这个属性就是textDirection。关于TextView详见《TextView》

<!-- Defines the direction of the text. -->
     <attr name="textDirection" format="integer">
     
        <!-- Default. -->
        <enum name="inherit" value="0" />
        
        <!-- Default for the root view. The first strong directional character determines the
             paragraph direction.  If there is no strong directional character, the paragraph
             direction is the view’s resolved layout direction. -->
        <enum name="firstStrong" value="1" />
        
        <!-- The paragraph direction is RTL if it contains any strong RTL character, otherwise
             it is LTR if it contains any strong LTR characters.  If there are neither, the
             paragraph direction is the view’s resolved layout direction. -->
        <enum name="anyRtl" value="2" />
        
        <!-- The paragraph direction is left to right. -->
        <enum name="ltr" value="3" />
        
        <!-- The paragraph direction is right to left. -->
        <enum name="rtl" value="4" />
        
        <!-- The paragraph direction is coming from the system Locale. -->
        <enum name="locale" value="5" />
        
        <!-- The first strong directional character determines the paragraph direction. If
             there is no strong directional character, the paragraph direction is LTR. -->
        <enum name="firstStrongLtr" value="6" />
        
        <!-- The first strong directional character determines the paragraph direction. If
             there is no strong directional character, the paragraph direction is RTL. -->
        <enum name="firstStrongRtl" value="7" />
    </attr>

参考资料

        RTL Unicode双向算法基础知识

        阿拉伯语网站中英文、数字、符号的左右顺序问题

        如何实现Android RTL文本方向?

        UNICODE BIDIRECTIONAL ALGORITHM


相关推荐

精选
ViewPager和PagerAdapter、FragmentPagerAdapter、FragmentStatePager
Android

ViewPager和PagerAdapter、FragmentPagerAdapter、FragmentStatePager

ViewPager1、ViewPagerandroidx.viewpager.widget.ViewPager,Android中使用非常广泛的控件,可以说是APP必备:首次打开引导页、页面Banner广告等。常用方法:setAdapter() 设置适配器setOffscreenPageLimit() 设置缓存的页面个数,默认是 1setCurrentItem() 跳转到特定的页面setOnPage

1.1k
获取Android内置WebView内核版本
Android

获取Android内置WebView内核版本

获取Android内置WebView内核版本竟然能遇到这样奇葩的事情,网页用的技术过于新颖,以至于只支持高版本Chromium内核的,低版本安卓系统中内置的内核版本较低,无法加载前端页面。1、设置查看在系统设置里 > 应用 > 应用管理 > 显示系统应用,查看WebView组件:2、页面查看通过WebView发起的网络请求,都会带上浏览器的UA,通常页面都可以通过UA判断浏览器的内核版本。这里有两

1.1w
Android 14适配总结
Android

Android 14适配总结

Android 14适配总结毕业工作至今已经适配了三个Android大版本,从Android 11到Android 12、再到Android 13。2023年,Google即将推出的Android 14,上半年已经开始第一批适配。现在,第四个Android版本已经适配完,总结记录一下。1、Android 14计划Google一般会在2月份对外发布预告,同时放出开发者预览版。“拉通”各大平台、厂商以

1.1w