그런데.... 시스템에서 언어를 변경해야 하는 방식으로 프로그래밍을 하면 전체 시스템 언어가 바뀌어서 여간 불편한 것이 아니다.
그래서 앱에만 적용되도록 바꿔주는 방법을 알아보도록 하자!!!
- // 로케일을 영어로 설정
- Locale en = Locale.US;
- // 환경설정 가져오기
- Configuration config = new Configuration( );
- // 환경설정 값에서 지역설정을 영어로 변경
- config.locale = en;
- // 리소스를 변경된 지역설정을 기준으로 갱신하기
- getResources( ).updateConfiguration( config, getResources( ).getDisplayMetrics( ) );
로케일이 변경된 다음에는 invalidateViews( ) 를 호출해줘야 변경된 언어로 화면이 갱신된다.
다음은 참고코드
출처 : https://stackoverflow.com/questions/5244889/load-language-specific-string-from-resource
- package com.my.package.localisation;
- <div>
- import android.content.Context;
- import android.content.res.AssetManager;
- import android.content.res.Configuration;
- import android.content.res.Resources;
- import android.os.Build;
- import android.support.annotation.NonNull;
- import android.util.DisplayMetrics;
- import java.util.Formatter;
- import java.util.Locale;
- /**
- * Class to manage fetching {@link Resources} for a specific {@link Locale}. API levels less
- * than {@link Build.VERSION_CODES#JELLY_BEAN_MR1} require an ugly implementation.
- * <p/>
- * Subclass extends {@link Resources} in case of further functionality requirements.
- */
- public class MyResources {
- private final Context mContext;
- private final AssetManager assetManager;
- private final DisplayMetrics metrics;
- private final Configuration configuration;
- private final Locale targetLocale;
- private final Locale defaultLocale;
- public MyResources(@NonNull final Context mContext, @NonNull final Locale defaultLocale,
- @NonNull final Locale targetLocale) {
- this.mContext = mContext;
- final Resources resources = this.mContext.getResources();
- this.assetManager = resources.getAssets();
- this.metrics = resources.getDisplayMetrics();
- this.configuration = new Configuration(resources.getConfiguration());
- this.targetLocale = targetLocale;
- this.defaultLocale = defaultLocale;
- }
- public String[] getStringArray(final int resourceId) {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
- configuration.setLocale(targetLocale);
- return mContext.createConfigurationContext(configuration).getResources().getStringArray(resourceId);
- } else {
- configuration.locale = targetLocale;
- final String[] resourceArray = new ResourceManager(assetManager, metrics, configuration).getStringArray(resourceId);
- configuration.locale = defaultLocale; // reset
- new ResourceManager(assetManager, metrics, configuration); // reset
- return resourceArray;
- }
- }
- public String getString(final int resourceId) {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
- configuration.setLocale(targetLocale);
- return mContext.createConfigurationContext(configuration).getResources().getString(resourceId);
- } else {
- configuration.locale = targetLocale;
- final String resource = new ResourceManager(assetManager, metrics, configuration).getString(resourceId);
- configuration.locale = defaultLocale; // reset
- new ResourceManager(assetManager, metrics, configuration); // reset
- return resource;
- }
- }
- private final class ResourceManager extends Resources {
- public ResourceManager(final AssetManager assets, final DisplayMetrics metrics, final Configuration config) {
- super(assets, metrics, config);
- }
- /**
- * Return the string array associated with a particular resource ID.
- *
- * @param id The desired resource identifier, as generated by the aapt
- * tool. This integer encodes the package, type, and resource
- * entry. The value 0 is an invalid identifier.
- * @return The string array associated with the resource.
- * @throws NotFoundException Throws NotFoundException if the given ID does not exist.
- */
- @Override
- public String[] getStringArray(final int id) throws NotFoundException {
- return super.getStringArray(id);
- }
- /**
- * Return the string value associated with a particular resource ID,
- * substituting the format arguments as defined in {@link Formatter}
- * and {@link String#format}. It will be stripped of any styled text
- * information.
- * {@more}
- *
- * @param id The desired resource identifier, as generated by the aapt
- * tool. This integer encodes the package, type, and resource
- * entry. The value 0 is an invalid identifier.
- * @param formatArgs The format arguments that will be used for substitution.
- * @return String The string data associated with the resource,
- * stripped of styled text information.
- * @throws NotFoundException Throws NotFoundException if the given ID does not exist.
- */
- @NonNull
- @Override
- public String getString(final int id, final Object... formatArgs) throws NotFoundException {
- return super.getString(id, formatArgs);
- }
- /**
- * Return the string value associated with a particular resource ID. It
- * will be stripped of any styled text information.
- * {@more}
- *
- * @param id The desired resource identifier, as generated by the aapt
- * tool. This integer encodes the package, type, and resource
- * entry. The value 0 is an invalid identifier.
- * @return String The string data associated with the resource,
- * stripped of styled text information.
- * @throws NotFoundException Throws NotFoundException if the given ID does not exist.
- */
- @NonNull
- @Override
- public String getString(final int id) throws NotFoundException {
- return super.getString(id);
- }
- }
- }