AppLocale是一个android库,用于动态更新应用程序语言

网友投稿 2599 2022-10-26

AppLocale是一个android库,用于动态更新应用程序语言

AppLocale是一个android库,用于动态更新应用程序语言

App Locale 1.2.5

AppLocale is a android library to update the app language dynamically.

1. Add dependency

// Manages the Locale used by the appimplementation 'dev.b3nedikt.applocale:applocale:1.2.5'

2. Initialize

Initialize AppLocale in your Application class:

// The languages supported by our app, normally the ones we have strings.xml files for// in the resources. If you dont set this, it will be assumed the app supports every language// set programmatically using this lib.AppLocale.supportedLocales = listOf(Locale.ENGLISH, Locale.French)// Optional: Persist changes to the desiredLocale to sharedPreferencesAppLocale.appLocaleRepository = SharedPrefsAppLocaleRepository(this)

2. Add to the base activity

If the app has a base activity, this can go there, otherwise it needs to be added to every activity:

abstract class BaseActivity : AppCompatActivity() { override fun attachBaseContext(newBase: Context) { super.attachBaseContext(AppLocale.wrap(newBase)) } override fun getResources(): Resources { return AppLocale.wrap(baseContext).resources }}

3. Update the app language

Now we can just set the desiredLocale to the Locale we want our app to use:

AppLocale.desiredLocale = Locale.FRENCH

When the activity gets restarted, all texts will be localized using the new Locale.

Update the app language without restarting the activity

If you want to change the apps Locale without restarting the activity, you need to add the following additional dependencies:

// Needed to intercept view inflationimplementation 'io.github.inflationx:viewpump:2.0.3'// Allows to update the text of views at runtime without recreating the activityimplementation 'dev.b3nedikt.reword:reword:1.1.0'

Initialize ViewPump & Reword in the application class:

// To dynamically update views we need to intercept view inflation and update// the text of each view. The libraries ViewPump and reword do exactly that when setup// like this:ViewPump.init(ViewPump.builder() .addInterceptor(RewordInterceptor) .build())

To use ViewPump we need to additionally wrap the context with ViewPump in our base activity:

abstract class BaseActivity : AppCompatActivity() { override fun attachBaseContext(newBase: Context) { super.attachBaseContext(ViewPumpContextWrapper.wrap(AppLocale.wrap(newBase))) } override fun getResources(): Resources { return AppLocale.wrap(baseContext).resources }}

After setting the new locale you need to call reword to update all views which have been inflated from xml:

// If we want to update the app language without restarting the activity,// we need to perform the update of the texts manually:val rootView = window.decorView.findViewById(android.R.id.content)Reword.reword(rootView)

If you have changed the texts of views in code, you need to update these texts manually of course.

License

Copyright 2020 applocale Contributors.Licensed under the Apache License, Version 2.0 (the "License");you may not use this file except in compliance with the License.You may obtain a copy of the License at http://apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License.

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:spring controller层引用service报空指针异常nullpointExceptio问题
下一篇:kafka并发写大消息异常TimeoutException排查记录
相关文章

 发表评论

暂时没有评论,来抢沙发吧~