Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
captain-miao committed May 12, 2017
0 parents commit 62f5eb0
Show file tree
Hide file tree
Showing 31 changed files with 887 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .gitignore
@@ -0,0 +1,9 @@
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
.externalNativeBuild
1 change: 1 addition & 0 deletions app/.gitignore
@@ -0,0 +1 @@
/build
28 changes: 28 additions & 0 deletions app/build.gradle
@@ -0,0 +1,28 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion Integer.parseInt(project.COMPILE_SDK_VERSION)
buildToolsVersion project.BUILD_TOOLS_VERSION
defaultConfig {
applicationId "captain_miao.github.com.multilanguagesswitch"
minSdkVersion project.MIN_SDK_VERSION
targetSdkVersion project.TARGET_SDK_VERSION
versionCode Integer.parseInt(project.VERSION_CODE)
versionName project.VERSION_NAME

resConfigs "en", "zh-rCN", "zh-rTW", "de", "fr", "hi", "it"
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
def android_support_version = project.ANDROID_SUPPORT_VERSION

compile "com.android.support:appcompat-v7:$android_support_version"
compile "com.android.support:support-v4:$android_support_version"
}
25 changes: 25 additions & 0 deletions app/proguard-rules.pro
@@ -0,0 +1,25 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /Users/captain_miao/Library/Android/sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
27 changes: 27 additions & 0 deletions app/src/main/AndroidManifest.xml
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest package="captain_miao.github.com.multilanguagesswitch"
xmlns:android="http://schemas.android.com/apk/res/android">

<application
android:name=".App"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".HomeActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".LanguagesActivity"
android:label="@string/app_name"/>
</application>

</manifest>
@@ -0,0 +1,47 @@
package captain_miao.github.com.multilanguagesswitch;

import android.app.Application;
import android.content.Context;
import android.content.res.Configuration;
import android.preference.PreferenceManager;

import captain_miao.github.com.multilanguagesswitch.constants.ConstantLanguages;
import captain_miao.github.com.multilanguagesswitch.utils.AppLanguageUtils;

/**
* @author YanLu
* @since 17/5/12
*/

public class App extends Application {

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

// @Override
// protected void attachBaseContext(Context base) {
// super.attachBaseContext(AppLanguageUtils.attachBaseContext(base, getAppLanguage(base)));
// }

/**
* Handling Configuration Changes
* @param newConfig newConfig
*/
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
onLanguageChange();
}

private void onLanguageChange() {
AppLanguageUtils.changeAppLanguage(this, AppLanguageUtils.getSupportLanguage(getAppLanguage(this)));
}

private String getAppLanguage(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context)
.getString(context.getString(R.string.app_language_pref_key), ConstantLanguages.ENGLISH);
}
}
@@ -0,0 +1,67 @@
package captain_miao.github.com.multilanguagesswitch;


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

public class HomeActivity extends AppCompatActivity implements View.OnClickListener {
private static final int CHANGE_LANGUAGE_REQUEST_CODE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_other);
ActionBar actionBar = getSupportActionBar();

if(actionBar != null) {
actionBar.setTitle(R.string.pref_setting_customize);
}

LinearLayout linearLayout = (LinearLayout) findViewById(R.id.container);

TextView textView1 = (TextView) findViewById(R.id.tv_content1);
textView1.setText(R.string.pref_setting_customize);
textView1.setOnClickListener(this);

TextView textView2 = (TextView) findViewById(R.id.tv_content2);
textView2.setText(getString(R.string.pref_setting_customize));
textView2.setOnClickListener(this);

TextView textView3 = (TextView) findViewById(R.id.tv_content3);
textView3.setText(getApplication().getString(R.string.pref_setting_customize));
textView3.setOnClickListener(this);

CheckBox checkBox = (CheckBox) findViewById(R.id.cb_content4);
checkBox.setText(getApplication().getString(R.string.pref_setting_customize));
checkBox.setOnClickListener(this);

TextView textView4 = new TextView(this) ;
textView4.setText(textView4.getContext().getString(R.string.pref_setting_customize));
textView4.setOnClickListener(this);

linearLayout.addView(textView4);
}

// @Override
// protected void attachBaseContext(Context newBase) {
// super.attachBaseContext(AppLanguageUtils.attachBaseContext(newBase, newBase.getString(R.string.app_language_pref_key)));
// }

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == CHANGE_LANGUAGE_REQUEST_CODE) {
recreate();
}
}

@Override
public void onClick(View v) {
startActivityForResult(new Intent(HomeActivity.this, LanguagesActivity.class), CHANGE_LANGUAGE_REQUEST_CODE);
}
}
@@ -0,0 +1,95 @@
package captain_miao.github.com.multilanguagesswitch;


import android.app.FragmentTransaction;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.PreferenceFragment;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.MenuItem;

import captain_miao.github.com.multilanguagesswitch.utils.AppLanguageUtils;

public class LanguagesActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_languages);
ActionBar actionBar = getSupportActionBar();

if(actionBar != null) {
actionBar.setTitle(R.string.pref_setting_customize);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
}

if(savedInstanceState == null) {
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, new LanguagesPreferenceFragment()).commitAllowingStateLoss();
}
}

@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(AppLanguageUtils.attachBaseContext(newBase, newBase.getString(R.string.app_language_pref_key)));
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}

public static class LanguagesPreferenceFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.languages_preference);

ListPreference preference = (ListPreference) findPreference(getString(R.string.app_language_pref_key));
preference.setSummary(preference.getEntry());

SharedPreferences prefs = getPreferenceManager().getSharedPreferences();
prefs.registerOnSharedPreferenceChangeListener(mPreferenceChangeListener);
}




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

getPreferenceManager().getSharedPreferences()
.unregisterOnSharedPreferenceChangeListener(mPreferenceChangeListener);
}


private final SharedPreferences.OnSharedPreferenceChangeListener mPreferenceChangeListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (getString(R.string.app_language_pref_key).equals(key)) {
ListPreference preference = (ListPreference) findPreference(key);
preference.setSummary(preference.getEntry());
CharSequence language = preference.getValue();
if (!TextUtils.isEmpty(language)) {
onChangeAppLanguage(language.toString());
}
}
}
};

private void onChangeAppLanguage(String newLanguage) {
AppLanguageUtils.changeAppLanguage(getActivity(), newLanguage);
getActivity().recreate();
}
}
}
@@ -0,0 +1,23 @@
package captain_miao.github.com.multilanguagesswitch.constants;

/**
* @author YanLu
* @since 17/1/12
*/

public class ConstantLanguages {
// 简体中文
public static final String SIMPLIFIED_CHINESE = "zh";
// 英文
public static final String ENGLISH = "en";
// 繁体中文
public static final String TRADITIONAL_CHINESE = "zh-hant";
// 法语
public static final String FRANCE = "fr";
// 德语
public static final String GERMAN = "de";
// 印地语
public static final String HINDI = "hi";
// 意大利语
public static final String ITALIAN = "it";
}

0 comments on commit 62f5eb0

Please sign in to comment.