kcNeko
kcNeko

Reputation: 609

Change language in android application using string file

I'm new in java and actually developing a game app and I wanted add a feature which could change languages in the game.

I already made 2 strings.xml. One is the default (English), the other one is the translated version (File)

enter image description here

Here's my code

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class LanguageActivity extends Activity {
  private static Button button_fil;
  private static Button button_eng;

  public void onButtonClickListener() {
    button_fil = (Button) findViewById(R.id.btnFilipino);
    button_fil.setOnClickListener(
      new View.OnClickListener() {@
        Override
        public void onClick(View v) {
          Toast.makeText(LanguageActivity.this, "Filipino Language", Toast.LENGTH_SHORT).show();
        }
      }
    );

    button_eng = (Button) findViewById(R.id.btnEnglish);
    button_eng.setOnClickListener(
      new View.OnClickListener() {@
        Override
        public void onClick(View v) {
          Toast.makeText(LanguageActivity.this, "English Language", Toast.LENGTH_SHORT).show();
        }
      }
    );

  }


  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.language);
    onButtonClickListener();

  }

Thanks a lot!

Upvotes: 11

Views: 26368

Answers (4)

Viral Patel
Viral Patel

Reputation: 33408

This is a method i wrote and is working perfectly well for me for changing the language from app (and JUST FOR A SINGLE APP - not the entire device):

private void setLanguageForApp(String languageToLoad){
    Locale locale;
    if(languageToLoad.equals("not-set")){ //use any value for default
        locale = Locale.getDefault();
    }
    else {
        locale = new Locale(languageToLoad);
    }
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    getBaseContext().getResources().updateConfiguration(config,
            getBaseContext().getResources().getDisplayMetrics());
}

NOTE: Call this method before setContentView() in the first activity's onCreate() everytime when the app is opened.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setLanguageForApp("en"); //hard-coded here - get from whereever you stored
    setContentView(R.layout.activity_category_list);
    ...
    ...
    ...

Store the selected locale code in shared preferences and retrieve to pass as parameter.

Method for language selection dialog: (Note: it reloads app after language change to bring the language change in effect)

private void showLanguageChangePopup() {
    CharSequence languages[] = new CharSequence[] {
            "English",
            "हिंदी (Hindi)",
            "Français (French)",
            "Italiano (Italian)",
            "Deutsch (German)",
            "Español (Spanish)",
            "日本語 (Japanese)",
            "한국어 (Korean)",
            "Nederlands (Dutch)",
            "Português (Portuguese)",
            "руÑÑкий (Russian)",
            "中文 (Chinese)",
            "العربية (Arabic)"
    };
    final String codes[] = new String[] {
            "en",
            "hi",
            "fr",
            "it",
            "de",
            "es",
            "ja",
            "ko",
            "nl",
            "pt",
            "ru",
            "zh",
            "ar"
    };

    int currentLangIndex = Prefs.getUserPreferenceIntValue(Prefs.Key.SELECTED_LANGUAGE_INDEX, getBaseContext());
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(R.string.text_select_language);
    builder.setSingleChoiceItems(languages, currentLangIndex, null);
    builder.setNegativeButton(R.string.text_translate_cancel, null);
    builder.setPositiveButton(R.string.action_change_language, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            int selectedIndex = ((AlertDialog) dialog).getListView().getCheckedItemPosition();
            Prefs.setUserPreferenceStringValue(Prefs.Key.LANGUAGE, codes[selectedIndex], getBaseContext());
            Prefs.setUserPreferenceIntValue(Prefs.Key.SELECTED_LANGUAGE_INDEX, selectedIndex, getBaseContext());
            Intent i = new Intent(CategoryListActivity.this, CategoryListActivity.class);
            startActivity(i);
            finish();

        }
    });

    builder.show();
}

Upvotes: 5

Vinayagam.D
Vinayagam.D

Reputation: 332

<resources>

    <string name="app_name">Androidlocalization</string>
    <string name="hello_world">Hello world!</string>
    <string name="title_activity_android_localize">AndroidLocalize</string>
    <string name="greet">बधाई सचिन !!</string>
    <string name="langselection">जिस भाषा में आप सचिन को नमस्कार करना चाहते हैं का चयन करें!!!!</string>
    <string name="chooselang">Choose the language</string>
      <string-array name="languages">
        <item>Select language</item>
        <item>தமிழ்</item>
        <item>हिंदी</item>
        <item>English</item>
    </string-array>

</resources>

Each code is in same folder for different language add different value folder

For example value folder for hindi goes inside value-hi

Upvotes: 3

When you are supporting multiple languages , You need to create separate values folder like values-fr for instance and put your stings.xml file inside this folder . Should work. Hope this helps!

Upvotes: 3

Zumry Mohamed
Zumry Mohamed

Reputation: 9558

Always follow official tutorial

Add your string files here.

if you don't have one, Create values-fil folder in the MyProject/res folder.

enter image description here

Check this if you want to handle Phone system language changes.

Upvotes: 5

Related Questions