LocaleChanger


Source link: https://github.com/franmontiel/LocaleChanger

LocaleChanger

An Android library to programmatically set the Locale of an app and persist the configuration.

Download

Step 1. Add the JitPack repository in your root build.gradle at the end of repositories:

allprojects {

  repositories {

...

maven {
 url "https://jitpack.io" 
}

  
}
 
}

Step 2. Add the dependency

dependencies {

compile 'com.github.franmontiel:LocaleChanger:0.9.1' 
}

Usage

Basic usage

Initialize the library from your Application class with a list of your app supported Locales:

LocaleChanger.initialize(getApplicationContext(), SUPPORTED_LOCALES);

The first time that is invoked it will automatically set a Locale taking into account the system configuration. The first element of the supported Locale list will be used as default if no match with the system configured Locales is found.

You also need to call onConfigurationChange from the same named method in your Application class:

@Override public void onConfigurationChanged(Configuration newConfig) {

  super.onConfigurationChanged(newConfig);

  LocaleChanger.onConfigurationChanged();
 
}

And create a new Locale configured Context for all your Activities calling configureBaseContext:

@Override protected void attachBaseContext(Context newBase) {

  newBase = LocaleChanger.configureBaseContext(newBase);

  super.attachBaseContext(newBase);
 
}

To change the Locale just make the following call:

LocaleChanger.setLocale(newLocale);

Activity recreation

You need to recreate the Activities once the Locale is changed to reload your resources. You can do it by simply calling the replace method of the Activity.

Additionally there is a ActivityRecreationHelper class that is intended for assisting you with the recreation of the Activity.

It can be used to detect when the Locale has changed and reload automatically the Activity when resumed, for that you must call to the onResume and onDestroy methods of the helper class from the Activity methods.

Advanced usage

The default behavior of the library can be changed providing a MatchingAlgorithm and a LocalePreference

  • The MatchingAlgorithm is used when the library is initialized and when the Locale is changed to find a match between your supported Locales and the system Locales. One of those matching Locales will be set by the library. There are two classes that implements this interface:

    • LanguageMatchingAlgorithm will match the first two Locales with the same language. This is the default algorithm used if no one is defined.
    • ClosestMatchingAlgorithm will match the two Locales with most attributes in common (language, country and variation).
  • The LocalePreference is used to select witch one of the two matching Locales will be set. The default behavior is to prefer a supported locales if no preference is provided.

Known issues

  • The ActionBar title is not affected by the Locale change if it is defined in the Manifest label attribute. A valid workaround is to set the title programmatically. More info on Issue #1.

License

Copyright (C) 2017 Francisco José Montiel Navarro  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://www.apache.org/licenses/LICENSE-2.0  Unless required by applicable law or agreed to in writing, software distributed 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 and limitations under the License. 

Resources

MaterialLibrary is an Open Source Android library that back-port Material Design components to pre-Lollipop Android.

Simple API to perform AES encryption on Android with no dependencies.

This library encapsulates scrolling logic when implementing scrolling tabs. It saves us from unpleasant routine aka addHeader, addFooter for every scrolling content which appears in tabs and synchronising it with our real headers. It also works good for a single scrolling widget, for e.g. RecyclerView which has it's own issues with headers (at least parallaxing them).

A powerful library for creating notifications in android platform.

A very simple arc layout library for Android.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes