0

Hi guys I am creating an android app and I want my app to support localisation which means it can translates to multi languages. I have created new folder called "values-zh" and inside this folder there is an xml file called strings.xml. There is also a folder called "values" and inside this folder there is also an xml file called strings.xml.

Inside the strings.xml in values-zh, I have added this:

    <string name="height">高度(仪表)</string>

Inside the strings.xml in values, I have added this:

   <string name="height">Height(Meters)</string>

In my app, I want the user to choose the language they prefer using a spinner.

Inside the spinner.setOnItemSelectedListener method, I have added these codes:

 if(position==0) // assume user selects chinese
 {
    // I want tvHeight changes from Height to 高度(仪表)
  }

How can I do that? Can anyone help me please? Thanks:)

1 Answers1

0

Hope your are expecting the language to be changes while using the app. Let you try the below implementations.

As detailed by Gunhan Sancar, create a global class like this:

public class LocaleHelper {

  private static final String SELECTED_LANGUAGE = "Locale.Helper.Selected.Language";

  public static void onCreate(Context context) {
    String lang = getPersistedData(context, Locale.getDefault().getLanguage());
    setLocale(context, lang);
  }

  public static void onCreate(Context context, String defaultLanguage) {
    String lang = getPersistedData(context, defaultLanguage);
    setLocale(context, lang);
  }

  public static String getLanguage(Context context) {
    return getPersistedData(context, Locale.getDefault().getLanguage());
  }

  public static void setLocale(Context context, String language) {
    persist(context, language);
    updateResources(context, language);
  }

  private static String getPersistedData(Context context, String defaultLanguage) {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
    return preferences.getString(SELECTED_LANGUAGE, defaultLanguage);
  }

  private static void persist(Context context, String language) {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
    SharedPreferences.Editor editor = preferences.edit();

    editor.putString(SELECTED_LANGUAGE, language);
    editor.apply();
  }

  private static void updateResources(Context context, String language) {
    Locale locale = new Locale(language);
    Locale.setDefault(locale);
    Resources resources = context.getResources();
    Configuration configuration = resources.getConfiguration();
    configuration.locale = locale;
    resources.updateConfiguration(configuration, resources.getDisplayMetrics());
  }
}

Then wherever you need just use the below lines to change. Probably in your language selection listener.

LocaleHelper.setLocale(mContext, "ta"); // For Tamil
LocaleHelper.setLocale(mContext, "en"); // For English
LocaleHelper.setLocale(mContext, "zh"); // in your case

Store the language selection in the Shared preference and use it for future change.

Hope it helps. Let me know for queries.

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Swaminathan V
  • 4,663
  • 2
  • 22
  • 32