2016-08-26 2 views
-2

언어 변경 버튼을 사용하여 힌디어와 영어 간 전환을 원할 때 프로그램의 로케일 (언어)을 프로그래밍 방식으로 변경하고 싶습니다.Android - 버튼 클릭시 앱 로케일을 수동으로 변경

언어를 변경할 수있는 코드가 있지만 setContentView() 메서드 앞에있는 활동의 onCreate()를 호출 할 때만 작동합니다.

도움을 주시면 감사하겠습니다.

+0

본질적으로 활동을 다시 시작하지 않겠습니까? 로케일을 변경하면 앱의 모든 텍스트가 즉시 업데이트되지 않습니다. –

+0

@ cricket_007 네, 이것이 제가하고있는 일입니다. 그러나 이것에 대한 더 나은 해결책이있을 것이라고 생각했습니다. – User16119012

+0

내가 아는 것은 수동으로 setText에 모든 메소드를 호출하지 않고 –

답변

2

내 대답은 여기를 참조하십시오

당신이 (기본 문자열에 추가) 모두 영어 아랍어 문자열을 지원하는 응용 프로그램, 는 단순히 두 개의 추가 리소스를 만들 수 있습니다하려는 경우 예를 들어

Android Font in more then one langauge on single screen

/res/values-en (영어 strings.xml 용) 및 /res/values-ar (아랍어 strings.xml 용)이라는 디렉토리가 있습니다.

strings.xml 파일 내에는 리소스 이름이 동일합니다. 예를 들어

/res/values-en/strings.xml 파일 은 다음과 같이 수 :

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
<string name="hello">Hello in English!</string> 
</resources> 

반면의 /res/values-ar/strings.xml 파일은 다음과 같습니다 또한

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
<string name="hello">مرحبا في اللغة الإنجليزية</string> 
</resources> 

, /res/values-ur_IN/strings.xml 파일은 다음과 같이 표시됩니다.

인도용 ur_IN pakisthan 용 ur_PK

문자열을 표시/입술/레이아웃 디렉토리에 기본 레이아웃 파일 문자열 리소스에 어떤 언어 또는 디렉토리 에 관계없이 변수 이름 @ 문자열/안녕하세요으로 문자열을 의미
<?xml version="1.0" encoding="utf-8"?> 
<resources> 
<string name="hello">انگریزی میں خوش!!</string> 
</resources> 

.

Android 운영 체제는 런타임에로드 할 문자열 (프랑스어, 영어 또는 기본값)을 의 버전으로 결정합니다.문자열을 표시하는 텍스트 뷰 컨트롤 있는 레이아웃은 다음과 같습니다 문자열은 일반적인 방법으로 프로그램에 액세스

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
<TextView 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:text="@string/hello" > 
</LinearLayout> 

: 변화를위한

String str = getString(R.string.hello); 

을 언어가 당신이 변화를 좋아합니다 lang ..

btn_english.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       Locale locale = new Locale("en"); 
        Locale.setDefault(locale); 
        Configuration config = new Configuration(); 
        config.locale = locale; 
        getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics()); 
        Toast.makeText(this, getResources().getString(R.string.lbl_langSelectEnglis), Toast.LENGTH_SHORT).show(); 

      } 
     }); 



btn_arbice.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       Locale locale = new Locale("ar"); 
        Locale.setDefault(locale); 
        Configuration config = new Configuration(); 
        config.locale = locale; 
        getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics()); 
        Toast.makeText(this, getResources().getString(R.string.lbl_langSelecURdu), Toast.LENGTH_SHORT).show(); 

      } 
     }); 


btn_urdu.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       Locale locale = new Locale("ur_IN"); 
        Locale.setDefault(locale); 
        Configuration config = new Configuration(); 
        config.locale = locale; 
        getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics()); 
        Toast.makeText(HomeActivity.this, getResources().getString(R.string.lbl_langSelectEnglis), Toast.LENGTH_SHORT).show(); 

      } 
     }); 
+0

이지만 config.locale과 updateConfiguration의 대안은 사용되지 않기 때문이다 – ruselli

1

버튼을 클릭 할 때이 기능을 사용해보십시오.

1

SharedPrefrences를 사용하면 onCheck()가 아닌 onStart()에서 사용자의 프리 플로우 및 호출 로케일을 저장할 수 있습니다. 내가 DEFAULT 값을 정의 이는 잘 작동되도록 앱이 늘 현금

I는 사용자의 선택에 따라 힌디어 또는 영어로 sharedPrefrences 값을 설정
public static final String Default="en"; //so if value isn't found then english language will be used. 


protected void onStart() { 
    SharedPreferences sharedPreferences = this.getSharedPreferences("selectedLanguage", Context.MODE_PRIVATE); 
    String pine = sharedPreferences.getString("language", DEFAULT); 
    String languageToLoad = pine; 
    Locale locale = new Locale(languageToLoad);//Set Selected Locale 
    Locale.setDefault(locale);//set new locale as default 
    Configuration config = new Configuration();//get Configuration 
    config.locale = locale;//set config locale as selected locale 
    this.getResources().updateConfiguration(config, this.getResources().getDisplayMetrics()); 
    invalidateOptionsMenu(); 
    setTitle(R.string.app_name); 
    super.onStart(); 
} 

. 이 경우 스위치를 사용했습니다. 아래에서 코드를 볼 수 있습니다.

aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton compoundButton, boolean b) { 

      if (aSwitch.isChecked()) { 
       SharedPreferences hisharedPreferences = getSharedPreferences("selectedLanguage", Context.MODE_PRIVATE); 
       SharedPreferences.Editor hieditor = npsharedPreferences.edit(); 
       npeditor.putString("language","hi"); 
       npeditor.commit(); 
       aSwitch.setChecked(true); 
       Toast.makeText(Settings.this, "Hindi Language Selected", Toast.LENGTH_LONG).show(); 

      } else { 
       SharedPreferences ensharedPreferences = getSharedPreferences("selectedLanguage", Context.MODE_PRIVATE); 
       SharedPreferences.Editor eneditor = ensharedPreferences.edit(); 
       eneditor.putString("language","en"); 
       eneditor.commit(); 
       Toast.makeText(Settings.this, "English Language Selected", Toast.LENGTH_LONG).show(); 
       aSwitch.setChecked(false); 
      } 
     } 
    }); 

희망이 도움이되었습니다. 당신이 어떤 단계에 갇혀 있다면 나에게 물어보십시오!