Husein Behboudi Rad
Husein Behboudi Rad

Reputation: 5462

getString() only returns english values

Here is the code of my onActivityResult method:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == Activity.RESULT_OK) {
        String contents = data.getStringExtra("SCAN_RESULT");
        if (contents.length() == 26) {
            BillBarcode barcode = new BillBarcode(contents);
            edtBillId.setText(barcode.extract(BillBarcode.BarcodePart.BillId));
            edtPaymentId.setText(barcode.extract(BillBarcode.BarcodePart.PaymentId));
            launchService();
        } else {
            Dialog dialog = new DialogBuilder()
                    .setTitle(getString(R.string.dialog_title_global_error))
                    .setMessage(getString(R.string.unknown_barcode))
                    .build(getActivity());

            dialog.show();
        }
    }
}

The problem is that getString(R.string.dialog_title_global_error) and getString(R.string.unknown_barcode) always returns english value while I have Farsi value too and the locale is farsi too.

The problem only exist in this method.

Farsi value:

<string name="unknown_barcode">بارکد قابل استفاده نیست.</string>

English value:

<string name="unknown_barcode">Unknown barcode</string>

EDIT

I have a setting page and set my locale when user selects persian from language page by this code:

      String languageToLoad = "fa";
Resources res = context.getResources();
            // Change locale settings in the app.
            android.content.res.Configuration conf = res.getConfiguration();
            conf.locale = new Locale(languageToLoad);

Upvotes: 3

Views: 1592

Answers (1)

dhke
dhke

Reputation: 15398

Let me try to round up all the comments in an answer. Your problem is a combination of two implementation mistakes:

  1. You are setting the locale of the current Activitys context programmatically. However you are doing so in an unsupported way that may yield incorrect results.

  2. When your activity gets a result from another activity in OnActivityResult(), your Activity either gets restarted completely or the context configuration gets reset to the system's default locale. Either way, the locale you set up in the settings dialog is lost.

Solutions

  1. The proper way of changing the App's locale locally is outlined here: Changing Locale within the app itself.

In particular, while just changing the locale in the configuration class may work for you, it is clearly not the correct way to change an application's locale. Doing it properly requires a little more work:

  Locale locale; // set to locale of your choice, i.e. "fa"
  Configuration config = getResources().getConfiguration();
  config.setLocale(locale); // There's a setter, don't set it directly     
  getApplicationContext().getResources().updateConfiguration(
      config,
      getApplicationContext().getResources().getDisplayMetrics()
  );
  // you might even need to use getApplicationContext().getBaseContext(), here.
  Locale.setLocale(locale);

Setting the locale on the application context should survive the restart of an activity, but as per Android's lifecycle guarantees, you should not assume that the locale stays around.

  1. If you really need the ability to locally change the locale of your app, you should save the user specified locale (e.g. in a SharedPreference) and obtain and re-set it when the app or your activity is restarted (i.e. at least in OnCreate()). Remember that Android is free to save and restart your activities at any time outside of your control and it is your responsibility to handle the restart gracefully.

Upvotes: 1

Related Questions