Android 多语言适配

藏宝库编辑 2024-9-9 17:47:23 83 0 来自 中国
迩来一段时间在国际部分做Android开辟,以是手头的项目都须要去适配多语言。这里总结了一些多语言适配的履历。
演示结果:(在app底部tab添加多语言适配)

1.gif 1.在res下创建多语言资源文件:

2.png 2.选择须要添加的语言

然后得到多种语言适配string文件:

    <!-- 中文string -->    <string name="home_page">首页</string>    <string name="q_a">问答</string>    <string name="system">体系</string>    <string name="mine">我的</string>    <!-- 英语string -->    <string name="home_page">home page</string>    <string name="q_a">Q&amp;A</string>    <string name="system">system</string>    <string name="mine">mine</string>    <!-- 阿拉伯语string -->    <string name="home_page">الصفحة الرئيسية</string>    <string name="q_a">سؤال وجواب</string>    <string name="system">نظم</string>    <string name="mine">ركاز</string>3.代码设置多语言

import android.annotation.TargetApiimport android.content.Contextimport android.os.Buildimport android.os.LocaleListimport java.util.*object LanguageHelper {    fun getAttachBaseContext(context: Context): Context {        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {            return setAppLanguageApi24(context)        } else {            setAppLanguage(context)        }        return context    }    /**     * 获取当前体系语言,如未包罗则默认英文    * Locale类包罗语言、国家等属性     */    private fun getSystemLocale(): Locale {        val systemLocale = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {            LocaleList.getDefault()[0]        } else {            Locale.getDefault()        }        return when (systemLocale.language) {            Locale.CHINA.language -> {                Locale.CHINA            }            Locale.ENGLISH.language -> {                Locale.ENGLISH            }            else -> {                Locale.ENGLISH            }        }    }    /**     * 兼容 7.0 及以上     */    @TargetApi(Build.VERSION_CODES.N)    private fun setAppLanguageApi24(context: Context): Context {        val locale = getSystemLocale()        val resource = context.resources        val configuration = resource.configuration        configuration.setLocale(locale)        configuration.setLocales(LocaleList(locale))        return context.createConfigurationContext(configuration)    }    /**     * 设置应用语言     */    private fun setAppLanguage(context: Context) {        val resources = context.resources        val displayMetrics = resources.displayMetrics        val configuration = resources.configuration        // 获取当前体系语言,默认设置跟随体系        val locale = getSystemLocale()        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {            configuration.setLocale(locale)        } else {            configuration.locale = locale        }        resources.updateConfiguration(configuration, displayMetrics)    }}最后,设置多语言,在application的onCreate方法中调用 LanguageHelper.getAttachBaseContext(this),获取体系语言,通过Configuration来举行设置。
这里在现实适配阿拉伯语言的时间,须要留意一些题目,好比阿拉伯人是从右往左的,结构须要换成从右往左,以是在写结构时,以往用的left、right相干属性,都须要改为start、end相干的属性来写,结构自动会改为从右往左。而且有的带有方向性的图片也要有左右两个范例,乃至某些结构改变会出乱,须要举行判定当前是否为阿拉伯语种,来特殊结构处置惩罚。
您需要登录后才可以回帖 登录 | 立即注册

Powered by CangBaoKu v1.0 小黑屋藏宝库It社区( 冀ICP备14008649号 )

GMT+8, 2025-2-23 00:02, Processed in 0.136445 second(s), 35 queries.© 2003-2025 cbk Team.

快速回复 返回顶部 返回列表