안드로이드 앱에서 언어설정을 사용할 일이 거의 없어서 까먹고 있었다가 최근에 구현할 일이 있어서 생각난김에 정리해본다.
LocaleHelper클래스 작성
import android.annotation.TargetApi;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.preference.PreferenceManager;
import java.util.Locale;
public class LocaleHelper {
public static Context onAttach(Context context) {
String locale = getPersistedLocale(context);
LogUtils.e("", "onAttach: " + locale);
return setLocale(context, locale);
}
public static String getPersistedLocale(Context context) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getString(SHARED_KEY_LANGUAGE, "");
}
public static Context setLocale(Context context, String localeSpec) {
LogUtils.e("", "setLocale 111 : " + localeSpec);
Locale locale;
if (localeSpec.equals("system")) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
locale = Resources.getSystem().getConfiguration().getLocales().get(0);
} else {
//noinspection deprecation
locale = Resources.getSystem().getConfiguration().locale;
}
} else {
locale = new Locale(localeSpec);
LogUtils.e("", "setLocale 222: " + locale);
}
Locale.setDefault(locale);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
LogUtils.e("", "setLocale 333 : " + locale);
return updateResources(context, locale);
} else {
return updateResourcesLegacy(context, locale);
}
}
@TargetApi(Build.VERSION_CODES.N)
public static Context updateResources(Context context, Locale locale) {
Configuration configuration = context.getResources().getConfiguration();
configuration.setLocale(locale);
configuration.setLayoutDirection(locale);
return context.createConfigurationContext(configuration);
}
@SuppressWarnings("deprecation")
public static Context updateResourcesLegacy(Context context, Locale locale) {
Resources resources = context.getResources();
Configuration configuration = resources.getConfiguration();
configuration.locale = locale;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
configuration.setLayoutDirection(locale);
}
resources.updateConfiguration(configuration, resources.getDisplayMetrics());
return context;
}
}
코드를 입력하세요
BaseActivity에 onSharedPreferenceChangeListener 추가
import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import java.util.Locale;
public class BaseActivity extends AppCompatActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
public SharedPreferences mSharedPreferences;
private String mInitLocale;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mInitLocale = LocaleHelper.getPersistedLocale(this);
Log.e("", "onCreate : " + mInitLocale);
mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
}
@Override
public void onResume() {
super.onResume();
mSharedPreferences.registerOnSharedPreferenceChangeListener(this);
Log.e("", "onResume mInitLocale : " + mInitLocale);
Log.e("", "onResume LocaleHelper.getPersistedLocale(this) : " + LocaleHelper.getPersistedLocale(this));
if (mInitLocale != null && !mInitLocale.equals(LocaleHelper.getPersistedLocale(this))) {
recreate();
}
}
@Override
public void onPause() {
super.onPause();
mSharedPreferences.unregisterOnSharedPreferenceChangeListener(this);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
LogUtils.e("", "onSharedPreferenceChanged : " + key);
LocaleHelper.setLocale(BaseActivity.this, mSharedPreferences.getString(SHARED_KEY_LANGUAGE, ""));
this.recreate();
}
public void setPersistedLocale(Context context, String newLocale) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = preferences.edit();
LogUtils.e("", "setPersistedLocale : " + newLocale);
editor.putString(SHARED_KEY_LANGUAGE, newLocale);
editor.apply();
editor.commit();
}
public void setLanguage(View view) {
switch (view.getId()) {
case R.id.ll_set_kor:
LocaleHelper.setLocale(BaseActivity.this, Locale.KOREAN.getLanguage());
setPersistedLocale(BaseActivity.this, Locale.KOREAN.getLanguage());
break;
case R.id.ll_set_eng:
LocaleHelper.setLocale(BaseActivity.this, Locale.US.getLanguage());
setPersistedLocale(BaseActivity.this, Locale.US.getLanguage());
break;
case R.id.ll_set_jpn:
LocaleHelper.setLocale(BaseActivity.this, Locale.JAPANESE.getLanguage());
setPersistedLocale(BaseActivity.this, Locale.JAPANESE.getLanguage());
break;
case R.id.ll_set_chz:
LocaleHelper.setLocale(BaseActivity.this, Locale.CHINESE.getLanguage());
setPersistedLocale(BaseActivity.this, Locale.CHINESE.getLanguage());
break;
}
}
}
코드를 입력하세요
이제 적용될 화면 즉 액티비티에서 추가해야할 내용
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(LocaleHelper.onAttach(newBase));
}
BaseActivity를 상속받은 액티비티에서 attachBaseContext를 Override해준다.