Answers to How to select different assets files as my language change using Locale and Configuration? ( 2 )

  1. 2017-01-03 10:01

    Make sure that you have to set adapter again after selecting another locale file. If you are using RecyclerView, then set LayoutManager to your recycler view like this:

    RecyclerView yourRecyclerView= (RecyclerView) findViewById(R.id.yourRecyclerView);
    LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this, LinearLayoutManager.VERTICAL, false);
    rc_qList.setHasFixedSize(true);
    rc_qList.setLayoutManager(layoutManager);
    
    RecyclerViewAdapter yourAdapter = new RecyclerViewAdapter (MainActivity.this, yourArrayList);
    rc_qList.setAdapter(yourAdapter);
    
  2. 2017-01-03 10:01

    You can get the current language of the device using the Locale object and then compare it to the constants of the Locale object. For example:

    Log.d(TAG, "onCreate: "+Locale.ENGLISH);
    Log.d(TAG, "onCreate: "+Locale.getDefault().getDisplayLanguage());
    Log.d(TAG, "onCreate: "+Locale.getDefault().getDisplayCountry());
    Log.d(TAG, "onCreate: "+Locale.getDefault().getCountry());
    Log.d(TAG, "onCreate: "+Locale.getDefault().getLanguage());
    

    prints:

    onCreate: en
    onCreate: English
    onCreate: United Kingdom
    onCreate: GB
    onCreate: en
    

    Don't forget to call adapter.notifyDataSetChanged() after your data collection has changed to make sure the adapter applies the changes.

Leave a reply to - How to select different assets files as my language change using Locale and Configuration?

◀ Go back