locale-helper-android

所属分类:android开发
开发工具:kotlin
文件大小:0KB
下载次数:0
上传日期:2021-12-23 16:09:50
上 传 者sh-1993
说明:  在Android中以编程方式更改语言
(Change Language Programmatically in Android)

文件列表:
.idea/ (0, 2021-12-23)
.idea/codeStyles/ (0, 2021-12-23)
.idea/codeStyles/Project.xml (4247, 2021-12-23)
.idea/codeStyles/codeStyleConfig.xml (142, 2021-12-23)
.idea/compiler.xml (169, 2021-12-23)
.idea/git_toolbox_prj.xml (507, 2021-12-23)
.idea/jarRepositories.xml (2266, 2021-12-23)
.idea/misc.xml (878, 2021-12-23)
.idea/vcs.xml (167, 2021-12-23)
LICENSE (11357, 2021-12-23)
app/ (0, 2021-12-23)
app/build.gradle (1177, 2021-12-23)
app/proguard-rules.pro (751, 2021-12-23)
app/src/ (0, 2021-12-23)
app/src/debug/ (0, 2021-12-23)
app/src/debug/res/ (0, 2021-12-23)
app/src/debug/res/values/ (0, 2021-12-23)
app/src/debug/res/values/google_maps_api.xml (136, 2021-12-23)
app/src/main/ (0, 2021-12-23)
app/src/main/AndroidManifest.xml (1030, 2021-12-23)
app/src/main/java/ (0, 2021-12-23)
app/src/main/java/com/ (0, 2021-12-23)
app/src/main/java/com/zeugmasolutions/ (0, 2021-12-23)
app/src/main/java/com/zeugmasolutions/localeexample/ (0, 2021-12-23)
app/src/main/java/com/zeugmasolutions/localeexample/Application.kt (603, 2021-12-23)
app/src/main/java/com/zeugmasolutions/localeexample/BaseActivity.kt (869, 2021-12-23)
app/src/main/java/com/zeugmasolutions/localeexample/MainActivity.kt (1049, 2021-12-23)
app/src/main/java/com/zeugmasolutions/localeexample/SecondActivity.kt (836, 2021-12-23)
app/src/main/res/ (0, 2021-12-23)
app/src/main/res/drawable-v24/ (0, 2021-12-23)
app/src/main/res/drawable-v24/ic_launcher_foreground.xml (1969, 2021-12-23)
app/src/main/res/drawable/ (0, 2021-12-23)
app/src/main/res/drawable/ic_launcher_background.xml (4887, 2021-12-23)
app/src/main/res/layout/ (0, 2021-12-23)
app/src/main/res/layout/activity_main.xml (1893, 2021-12-23)
app/src/main/res/layout/activity_second.xml (1887, 2021-12-23)
... ...

Change Language Programmatically in Android ============================== ![Header image](https://github.com/zeugma-solutions/locale-helper-android/blob/master/assets/change-language-programmatically.jpg) This is a helper library to change the language programmatically in Android. > Android by default uses the locale of the device to select the > appropriate language dependent resources. And most of the time this > behaviour is enough for common applications. However, there are cases where you would want to change the language of your app and the UI of the app. As a result, ```LocaleHelper``` has emerged. **Download** = ```groovy implementation 'com.zeugmasolutions.localehelper:locale-helper-android:1.5.1' ``` **Features** = 1. Changes language on-the-fly 2. Persists the changes in `Preferences` automatically 3. Detects changes when activity loads from backstack 4. Detects Right-To-Left (RTL) languages and updates layout direction 5. Supports DayNight themes 6. Small footprint (~3KB, ~50 methods), easy to use **Demo** = ![Demo video](https://github.com/zeugma-solutions/locale-helper-android/blob/master/https://media.giphy.com/media/1yidskG8wYqVIy3Pku/giphy.gif) [Demo source code](https://github.com/zeugma-solutions/locale-helper-android/blob/master/https://github.com/zeugma-solutions/locale-helper-android/tree/master/app "Demo source code") **Setup** = **(Option 1) Using base classes** 1. Extend your app class ```kotlin class App : LocaleAwareApplication() { } ``` 2. Extend your base activity class ```kotlin open class BaseActivity : LocaleAwareCompatActivity() { } ``` `LocaleAwareCompatActivity` provides a helper method called ```updateLocale``` That's it. **(Option 2) Using delegates** This option requires you to do extra steps if you don't want to extend from base classes. 1. On your custom Application class override methods below. For more details check: [LocaleAwareApplication](https://github.com/zeugma-solutions/locale-helper-android/blob/master/https://github.com/zeugma-solutions/locale-helper-android/blob/15885c0716d0fc3866e3ce7688656c95801707e9/localehelper/src/main/java/com/zeugmasolutions/localehelper/LocaleHelperActivities.kt#L47) ```kotlin class MyApp : Application() { private val localeAppDelegate = LocaleHelperApplicationDelegate() override fun attachBaseContext(base: Context) { super.attachBaseContext(localeAppDelegate.attachBaseContext(base)) } override fun onConfigurationChanged(newConfig: Configuration) { super.onConfigurationChanged(newConfig) localeAppDelegate.onConfigurationChanged(this) } override fun getApplicationContext(): Context = LocaleHelper.onAttach(super.getApplicationContext()) } ``` 2. On your base activity class override methods below. For more details check: [LocaleAwareCompatActivity](https://github.com/zeugma-solutions/locale-helper-android/blob/master/https://github.com/zeugma-solutions/locale-helper-android/blob/15885c0716d0fc3866e3ce7688656c95801707e9/localehelper/src/main/java/com/zeugmasolutions/localehelper/LocaleHelperActivities.kt#L10) ```kotlin open class BaseActivity : AppCompatActivity() { private val localeDelegate: LocaleHelperActivityDelegate = LocaleHelperActivityDelegateImpl() override fun getDelegate() = localeDelegate.getAppCompatDelegate(super.getDelegate()) override fun attachBaseContext(newBase: Context) { super.attachBaseContext(localeDelegate.attachBaseContext(newBase)) } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) localeDelegate.onCreate(this) } override fun onResume() { super.onResume() localeDelegate.onResumed(this) } override fun onPause() { super.onPause() localeDelegate.onPaused() } override fun createConfigurationContext(overrideConfiguration: Configuration): Context { val context = super.createConfigurationContext(overrideConfiguration) return LocaleHelper.onAttach(context) } override fun getApplicationContext(): Context = localeDelegate.getApplicationContext(super.getApplicationContext()) open fun updateLocale(locale: Locale) { localeDelegate.setLocale(this, locale) } } ``` **Usage** = (Option 1) If you're using the base classes, just call `updateLocale(newLocale)`. It will then update the locale and restart the activity. Example: ```kotlin toTRButton.setOnClickListener { updateLocale(Locales.Turkish) } ``` In `java.util.Locale` class most of the common `Locales` and their variants are defined. However, it doesn't contain all the Locales so `com.zeugmasolutions.Locales` provides the missing ones for easy access. (Option 2) To change the locale you can call `setLocale` on the delegate ```kotlin localeDelegate.setLocale(this, locale) ``` The delegate will set the new locale and recreate the activity. **Notes** = 1. actionbar(toolbar) title should be set when `onCreate` is called. ```kotlin override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) //sample setTitle(R.string.main_activity_title) //sample } ``` 2. If your locale is Right-To-Left(RTL) don't forget to enable it in the `AndroidManifest.xml` ```xml ``` 3. Google introduced a new App Bundle format to split apk files in smaller sizes when they’re being installed on the client devices. However, this means that we cannot have dynamic language changes in our applications. To prevent that split for language files we need to add extra lines in our build.gradle file inside the app folder like below. ```groovy android { //... //... removed for brevity bundle { language { enableSplit = false } } } ```

近期下载者

相关文件


收藏者