Skip to content

Commit

Permalink
Android多语言切换国际化first blood
Browse files Browse the repository at this point in the history
  • Loading branch information
finddreams committed Nov 7, 2017
1 parent fb3ad28 commit fa5bc8a
Show file tree
Hide file tree
Showing 26 changed files with 475 additions and 104 deletions.
3 changes: 2 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ android {
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
implementation project(':languagelib')
implementation 'org.greenrobot:eventbus:3.0.0'

}
18 changes: 14 additions & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,33 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.finddreams.multilanguage">

<uses-permission android:name="android.permission.INTERNET" />
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:name=".MyApplication"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<activity
android:name=".MainActivity"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".WebViewActivity"/>
<activity android:name=".LandScapeActivity" android:screenOrientation="landscape"/>
<activity
android:name=".WebViewActivity"
android:launchMode="singleTop" />
<activity
android:name=".LandScapeActivity"
android:launchMode="singleTop"
android:screenOrientation="landscape" />
<activity android:name=".SetLanguageActivity" />
<activity android:name=".OtherActivity"></activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.finddreams.languagelib.LanguageUtil;

public class BaseActivity extends AppCompatActivity {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

import android.os.Bundle;

/**
* 横屏会重置语言
*/
public class LandScapeActivity extends BaseActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_land_scape);
setTitle(R.string.app_name);
}

}
35 changes: 34 additions & 1 deletion app/src/main/java/com/finddreams/multilanguage/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,46 @@
package com.finddreams.multilanguage;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class MainActivity extends AppCompatActivity {
import com.finddreams.languagelib.OnChangeLanguageEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class MainActivity extends BaseActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EventBus.getDefault().register(this);
}
public void openLandScape(View view){
startActivity(new Intent(this,LandScapeActivity.class));

}
public void openWebView(View view){
startActivity(new Intent(this,WebViewActivity.class));
}
public void openSettingLanguage(View view){
startActivity(new Intent(this,SetLanguageActivity.class));
}
public void openOther(View view){
startActivity(new Intent(this,OtherActivity.class));
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onChangeLanguageEvent(OnChangeLanguageEvent event){
Log.d("onchange","ChangeLanguage");
}

@Override
public void onBackPressed() {
super.onBackPressed();
android.os.Process.killProcess(android.os.Process.myPid());
}
}
12 changes: 12 additions & 0 deletions app/src/main/java/com/finddreams/multilanguage/MyApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import android.app.Activity;
import android.app.Application;
import android.content.res.Configuration;
import android.os.Bundle;

import com.finddreams.languagelib.LanguageUtil;

/**
* Created by lx on 17-10-26.
*/
Expand All @@ -12,6 +15,8 @@ public class MyApplication extends Application{
@Override
public void onCreate() {
super.onCreate();
LanguageUtil.init(this);
LanguageUtil.getInstance().setConfiguration();
registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
@Override
public void onActivityCreated(Activity activity, Bundle bundle) {
Expand All @@ -25,6 +30,7 @@ public void onActivityStarted(Activity activity) {

@Override
public void onActivityResumed(Activity activity) {
LanguageUtil.getInstance().setConfiguration();
}

@Override
Expand All @@ -48,4 +54,10 @@ public void onActivityDestroyed(Activity activity) {
}
});
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
LanguageUtil.getInstance().setConfiguration();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

/**
* 测试语言是否被重置
*/
public class OtherActivity extends AppCompatActivity {

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,122 @@
package com.finddreams.multilanguage;

import android.support.v7.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class SetLanguageActivity extends AppCompatActivity {
import com.finddreams.languagelib.LanguageType;
import com.finddreams.languagelib.LanguageUtil;

/**
* 设置语言页面
*/
public class SetLanguageActivity extends BaseActivity implements View.OnClickListener {

private RelativeLayout rl_followsytem;
private RelativeLayout rl_simplified_chinese;
private RelativeLayout rl_traditional_chinese;
private RelativeLayout rl_english;
private ImageView iv_english;
private ImageView iv_followsystem;
private ImageView iv_simplified_chinese;
private ImageView iv_traditional_chinese;
private int savedLanguageType;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_set_language);
setTitle(R.string.setting_language_title);
initViews();
}

private void initViews() {
rl_followsytem = findViewById(R.id.rl_followsytem);
rl_simplified_chinese = findViewById(R.id.rl_simplified_chinese);
rl_traditional_chinese = findViewById(R.id.rl_traditional_chinese);
rl_english = findViewById(R.id.rl_english);
iv_followsystem = findViewById(R.id.iv_followsystem);
iv_english = findViewById(R.id.iv_english);
iv_simplified_chinese = findViewById(R.id.iv_simplified_chinese);
iv_traditional_chinese = findViewById(R.id.iv_traditional_chinese);
rl_followsytem.setOnClickListener(this);
rl_simplified_chinese.setOnClickListener(this);
rl_traditional_chinese.setOnClickListener(this);
rl_english.setOnClickListener(this);
savedLanguageType = LanguageUtil.getInstance().getLanguageType();
if (savedLanguageType == LanguageType.LANGUAGE_FOLLOW_SYSTEM) {
setFollowSytemVisible();
} else if (savedLanguageType == LanguageType.LANGUAGE_CHINESE_TRADITIONAL) {
setTraditionalVisible();
} else if (savedLanguageType == LanguageType.LANGUAGE_EN) {
setEnglishVisible();
} else if (savedLanguageType == LanguageType.LANGUAGE_CHINESE_SIMPLIFIED) {
setSimplifiedVisible();
} else {
setSimplifiedVisible();
}
}

@Override
public void onClick(View view) {
int id = view.getId();
int selectedLanguage = 0;
switch (id) {
case R.id.rl_followsytem:
setFollowSytemVisible();
selectedLanguage = LanguageType.LANGUAGE_FOLLOW_SYSTEM;
break;
case R.id.rl_simplified_chinese:
setSimplifiedVisible();
selectedLanguage = LanguageType.LANGUAGE_CHINESE_SIMPLIFIED;

break;
case R.id.rl_traditional_chinese:
setTraditionalVisible();
selectedLanguage = LanguageType.LANGUAGE_CHINESE_TRADITIONAL;

break;
case R.id.rl_english:
setEnglishVisible();
selectedLanguage = LanguageType.LANGUAGE_EN;
break;
}
LanguageUtil.getInstance().updateLanguage(selectedLanguage);
Intent intent = new Intent(SetLanguageActivity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
if (selectedLanguage == LanguageType.LANGUAGE_FOLLOW_SYSTEM) {
System.exit(0);
}
}

private void setSimplifiedVisible() {
iv_followsystem.setVisibility(View.GONE);
iv_english.setVisibility(View.GONE);
iv_simplified_chinese.setVisibility(View.VISIBLE);
iv_traditional_chinese.setVisibility(View.GONE);
}

private void setEnglishVisible() {
iv_followsystem.setVisibility(View.GONE);
iv_english.setVisibility(View.VISIBLE);
iv_simplified_chinese.setVisibility(View.GONE);
iv_traditional_chinese.setVisibility(View.GONE);
}

private void setTraditionalVisible() {
iv_followsystem.setVisibility(View.GONE);
iv_english.setVisibility(View.GONE);
iv_simplified_chinese.setVisibility(View.GONE);
iv_traditional_chinese.setVisibility(View.VISIBLE);
}

private void setFollowSytemVisible() {
iv_followsystem.setVisibility(View.VISIBLE);
iv_english.setVisibility(View.GONE);
iv_simplified_chinese.setVisibility(View.GONE);
iv_traditional_chinese.setVisibility(View.GONE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,45 @@

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

public class WebViewActivity extends AppCompatActivity {
/**
* webview在7.0上会重置语言
*/
public class WebViewActivity extends BaseActivity {

private WebView webview;
private ProgressBar progressbar;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
WebView webview = findViewById(R.id.webview);
webview = findViewById(R.id.webview);
progressbar = findViewById(R.id.progressbar);
webview.loadUrl("http://blog.csdn.net/finddreams");
webview.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
webview.loadUrl(url);
return true;
}
});
webview.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
progressbar.setProgress(newProgress);
if (newProgress == 100) {
progressbar.setVisibility(View.GONE);
}
}
});
}
}
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_land_scape.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="这是横屏页面" />
android:text="@string/landscape" />
</RelativeLayout>
Loading

0 comments on commit fa5bc8a

Please sign in to comment.