Coder Social home page Coder Social logo

guardianproject / android-languages Goto Github PK

View Code? Open in Web Editor NEW

This project forked from delight-im/android-languages

0.0 3.0 1.0 135 KB

Custom language selection and access to language names for Android

License: Apache License 2.0

Java 100.00%

android-languages's Introduction

Android-Languages

Custom language selection and access to language names for Android

Requirements

  • Android 2.2+

Installation

  • Add this library to your project
    • Declare the Gradle repository in your root build.gradle

      allprojects {
          repositories {
              maven { url "https://jitpack.io" }
          }
      }
    • Declare the Gradle dependency in your app module's build.gradle

      dependencies {
          compile 'com.github.delight-im:Android-Languages:v1.0.1'
      }

Usage

Custom language preference in your PreferenceActivity

Add the following code to your preferences definitions in XML. The class LanguagePreference extends ListPreference and has the same appearance. It ignores the attributes android:entries, android:entryValues, android:defaultValue and android:summary because it comes with its own implementation for those.

Be sure to set android:key to the preference key that you want to store the language code in. For android:title, provide a value from your string resources, which will be used as the preference's caption.

<im.delight.android.languages.LanguagePreference
    android:key="myPreferenceKey"
    android:title="@string/myPreferenceTitle"
    android:enabled="true"
    android:selectable="true" />

Extend the Application class

In your AndroidManifest.xml, modify the <application> tag to include android:name="com.my.package.App". Be sure to change com.my.package to your package name.

In the package that you specified there, create a new class App that extends Application:

public class App extends Application { }

Have your app show up in the custom language

Always make sure two include the correct preference key for the language setting instead of myPreferenceKey.

Modify your Application subclass as follows:

public class App extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        Language.setFromPreference(this, "myPreferenceKey");
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        Language.setFromPreference(this, "myPreferenceKey");
    }

}

In your PreferenceFragment or PreferenceActivity subclass that contains the language preference, override onPause as follows:

@Override
protected void onPause() {
    Language.setFromPreference(this, "myPreferenceKey", true);

    super.onPause();
}

Setting the default option's label

Optionally, you may set a localized label for the default option. The default is Standard (recommended).

To change the label, go to your PreferenceFragment or PreferenceActivity subclass that contains the language preference. Modify onCreate as follows:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    LanguageList.setStandardOptionLabel(getString(R.string.default_locale));
}

Access to the language names and keys

Optionally, you may retrieve a string array containing the list of all languages, when needed. Call LanguageList.getHumanReadable for the human-readable names and LanguageList.getMachineReadable for the machine-readable codes.

Contributing

All contributions are welcome! If you wish to contribute, please create an issue first so that your feature, problem or question can be discussed.

License

Copyright (c) delight.im <[email protected]>

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.

android-languages's People

Contributors

n8fr8 avatar ocram avatar rosenpin avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.