Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
166 views
in Technique[技术] by (71.8m points)

android - Android语言(Android Language)

I am training to force my application to start in Arabic.

(我正在接受培训,以迫使我的申请开始使用阿拉伯语。)

so I set my class LocalHelper .

(所以我设置了类LocalHelper 。)


object LocalHelper {

    fun onAttach(context: Context): Context {
        val lang = getPersistedData(context/*, Locale.getDefault().getLanguage()*/)
        return setLocale(context, lang)
    }

    private fun setLocale(context: Context, language: String): Context {
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            updateResources(context, language)
        } else updateResourcesLegacy(context, language)

    }

    private fun getPersistedData(context: Context): String {
//        val appComponent = Injector.INSTANCE.appComponent
//            ?: return SharedPreferencesUtils(context, DATABASE_NAME_QURAN, Gson())
//                .currentLanguage.id
        return Language.ARABIC
    }

    @TargetApi(Build.VERSION_CODES.N)
    private fun updateResources(context: Context, language: String): Context {
        val locale = Locale(language)
        Locale.setDefault(locale)

        val configuration = context.resources.configuration
        configuration.setLocale(locale)
        configuration.setLayoutDirection(locale)

        return context.createConfigurationContext(configuration)
    }

    private fun updateResourcesLegacy(context: Context, language: String): Context {
        val locale = Locale(language)
        Locale.setDefault(locale)

        val resources = context.resources

        val configuration = resources.configuration
        configuration.locale = locale
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            configuration.setLayoutDirection(locale)
        }

        context.resources.configuration.setLayoutDirection(locale)
        resources.updateConfiguration(configuration, resources.displayMetrics)

        return context
    }

}

  • And in My Application class, I update my configuration and base Attach.

    (在“我的应用程序”类中,我更新配置并进行基本附加。)

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        LocalHelper.onAttach(context = this)
    }

    override fun attachBaseContext(base: Context) {
        super.attachBaseContext(
            LocalHelper.onAttach(
                context = base
            )
        )
    }


  • Also, I create Strings.xml for Arabic.

    (另外,我为阿拉伯语创建Strings.xml 。)

  • Finally i update my BaseActivity configuration and attach such as Application class.

    (最后,我更新了BaseActivity配置并附加了Application类。)

  • When i run my application direction is correct but text is still English

    (当我运行时,我的申请方向是正确的,但文本仍为英文)

So it work fine from android 8, but in android 7,6 or 5 it doesn't work.

(所以它可以在android 8上正常工作,但是在android 7,6或5上却不起作用。)

  ask by Safwat Malek translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...