项目作者: Javinator9889

项目描述 :
Android Locale Manager compatible with multiple versions - change your Android App language easily
高级语言: Java
项目地址: git://github.com/Javinator9889/LocaleManager.git
创建时间: 2018-12-20T15:09:32Z
项目社区:https://github.com/Javinator9889/LocaleManager

开源协议:GNU General Public License v3.0

下载


LocaleManager

Android Locale Manager compatible with multiple versions - change your Android App language easily

Build Status
Android X - latest version
Android - latest version

Index

  1. Introduction
  2. Installation
  3. Usage
  4. Contributing
  5. License

Introduction

Nowadays, creating an Android application (that aspires to be popular) needs to be multilingual.
If you are an experienced developer, you may have noticed that there is no pre-built Android
system for changing manually the application language. However, from Android central they encouragingly
recommend us to not to change the language manually, as some incompatibilities can appear or errors.

But there is an extra feature that it is not so much difficult to implement and it is an extra bonus to
our actual and future users that they can switch from any language to another with just clicking a button.
For that reason, and with the intention to save developing time, I decided to create this library
that allows you to build a simple multilingual application, without losing any capability.

With just inheriting from some specific required classes, your application will be fully multilingual.
If you want to customize that settings, directly go to the usage step and setup this lib as you want.

Installation

First, you must include support with JCenter at your own package:

Gradle

At your top-level build.gradle, add jcenter() to your repositories section:

  1. repositories {
  2. ...
  3. jcenter()
  4. }

Then, you should be able (if there is no error) to include the repository. At your local build.gradle
(application level), add the following at the dependencies section:

  1. dependencies {
  2. // If you are deploying an Android X based application
  3. implementation 'com.github.javinator9889:localemanager:1.1X'
  4. // If you are deploying an Android AppCompat based application
  5. implementation 'com.github.javinator9889:localemanager:1.1'
  6. }

Sync your gradle settings for applying changes and you are done!

Maven

At your top-level pom file, include the following:

  1. <repositories>
  2. <repository>
  3. <id>central</id>
  4. <name>bintray</name>
  5. <url>http://jcenter.bintray.com</url>
  6. </repository>
  7. </repositories>

Finally, include at your dependencies section one of the following declarations, depending if
you are deploying an Android app based on Android X or AppCompat:

  1. <!-- AppCompat dependency -->
  2. <dependency>
  3. <groupId>com.github.javinator9889</groupId>
  4. <artifactId>localemanager</artifactId>
  5. <version>1.1</version>
  6. <type>pom</type>
  7. </dependency>
  1. <!-- Android X dependency -->
  2. <dependency>
  3. <groupId>com.github.javinator9889</groupId>
  4. <artifactId>localemanager</artifactId>
  5. <version>1.1X</version>
  6. <type>pom</type>
  7. </dependency>

Usage

As explained at the introduction, using this module is quite simple. It is as simple that
you always must follow this steps:

  1. Make your application class inherit from BaseApplication. If you do not have one, just create it.
  2. Each single activity you want to have a translated one must inherit from BaseActivity.
  3. Each single fragment you want to have a translated one must inherit from BaseFragment.
  4. Each single service (or job) you want to have a translated one must inherit from BaseService or BaseJobService.
  5. When applying the new language, you must call to the localeManager object placed on your application class and set
    the new Language.

By following these steps, you should be able to make your application multilingual. You can read the
docs for getting more information about classes and their possibilities.

Now a little example for start working on your app:

  1. import javinator9889.localemanager.application.BaseApplication;
  2. public class YourApplication extends BaseApplication {
  3. @Override
  4. @Nullable
  5. protected SharedPreferences getCustomSharedPreferences(@NonNull Context base) {
  6. // If you are planning to store the user language in a custom shared preferences, create
  7. // and initialize them here.
  8. // If not, you can safely return "null"
  9. return base.getSharedPreferences("myPrefs", MODE_PRIVATE);
  10. // OR
  11. return null;
  12. }
  13. }
  1. import javinator9889.localemanager.activity.BaseAppCompatActivity;
  2. import javinator9889.localemanager.utils.languagesupport.LanguagesSupport;
  3. public class YourActivity extends BaseAppCompatActivity implements Button.OnClickListener {
  4. // At this activity, we will change out language by implementing an arbitrary button selector
  5. @Override
  6. protected void onCreate(@Nullable Bundle savedInstanceState) {
  7. /*
  8. Initialize some IDs, buttons and set content view.
  9. */
  10. }
  11. @Override
  12. public void onClick(View view) {
  13. switch (view.getId()) {
  14. // Arbitrary ID supposedly defined above
  15. case idEnglishButton:
  16. // Here we are going to change our application language to English
  17. YourApplication.localeManager.setNewLocale(this, LanguagesSupport.Language.ENGLISH);
  18. recreate(); // Restart the activity - from API 11
  19. break;
  20. case idSpanishButton:
  21. // Here we are going to change our application language to English
  22. YourApplication.localeManager.setNewLocale(this, LanguagesSupport.Language.SPANISH);
  23. recreate(); // Restart the activity - from API 11
  24. break;
  25. default:
  26. // Here we are going to change our application language to the Android system
  27. // default
  28. YourApplication.localeManager.setNewLocale(this, LanguagesSupport.Language.SYSTEM);
  29. recreate(); // Restart the activity - from API 11
  30. break;
  31. }
  32. }
  33. }

As you can see, with that simple steps you can easily change dynamically your application language.

English
Spanish
Russian
System

Contributing

As you can see, developing this application is quite simple: the only necessary requirement
is to understand how Android locales works and then just repeat the same process over and over.

If you want to contribute, I probably need your help:

  • Share and like this repository, so much more people can enjoy it and its benefits 🗣❤
  • Comment any bugs or problems you have: there will be an entire community wanting to help
    you 👥
  • Commit your own implementations or enhancements you consider necessary - there are only a
    few languages supported, so you can commit the ones that are missing 🧠

Lets make the Android community stronger and help the others deploying their own applications 💪

License

  1. Copyright © 2018 - present | Javinator9889
  2. This program is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program. If not, see https://www.gnu.org/licenses/.