Last Updated
Viewed 12 Times

I am training to force my application to start in Arabic. so I set my class LocalHelper.


object LocalHelper {

    fun onAttach(context: Context): Context {
        val lang = getPersistedData(context/*, Locale.getDefault().getLanguage()*/)
        return setLocale(context, lang)
    }

    private fun setLocale(context: Context, language: String): Context {
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            updateResources(context, language)
        } else updateResourcesLegacy(context, language)

    }

    private fun getPersistedData(context: Context): String {
//        val appComponent = Injector.INSTANCE.appComponent
//            ?: return SharedPreferencesUtils(context, DATABASE_NAME_QURAN, Gson())
//                .currentLanguage.id
        return Language.ARABIC
    }

    @TargetApi(Build.VERSION_CODES.N)
    private fun updateResources(context: Context, language: String): Context {
        val locale = Locale(language)
        Locale.setDefault(locale)

        val configuration = context.resources.configuration
        configuration.setLocale(locale)
        configuration.setLayoutDirection(locale)

        return context.createConfigurationContext(configuration)
    }

    private fun updateResourcesLegacy(context: Context, language: String): Context {
        val locale = Locale(language)
        Locale.setDefault(locale)

        val resources = context.resources

        val configuration = resources.configuration
        configuration.locale = locale
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            configuration.setLayoutDirection(locale)
        }

        context.resources.configuration.setLayoutDirection(locale)
        resources.updateConfiguration(configuration, resources.displayMetrics)

        return context
    }

}

  • And in My Application class, I update my configuration and base Attach.
    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        LocalHelper.onAttach(context = this)
    }

    override fun attachBaseContext(base: Context) {
        super.attachBaseContext(
            LocalHelper.onAttach(
                context = base
            )
        )
    }


  • Also, I create Strings.xml for Arabic.
  • Finally i update my BaseActivity configuration and attach such as Application class.
  • When i run my application direction is correct but text is still English

So it work fine from android 8, but in android 7,6 or 5 it doesn't work.

Similar Question 1 : Android Ukrainian Language

How can i convert Date to Ukrainian language dynamically.

I'm using this code:

final SimpleDateFormat dateFormat = new SimpleDateFormat("MMMM, yyyy");
final Date date = calendar.getTime();

final TextView chosenMonth = (TextView) findViewById(R.id.chosenMonth);
chosenMonth.setText(dateFormat.format(date));

To format date as "September, 2013". This text language is Device language, but i need to format this date and display only in Ukrainian language.

I tried to implement this using Locale class but there is no constant for UA language.

I get a response from a webservice that will return several language codes (ISO 639-2) as options. I now want to translate those into a human understandable word. For example:

eng-> English
ger -> German
fre -> French

How would you translate those words. Should I be using the strings.xml? But how will I get the Resource ID of those words?

Thanks a lot

I want to support different screen sizes and languages at the same time. So I got some folders for my layout like

  • layout (German)
  • layout-en (English)
  • layout-fr (French)

etc...

and I got layout-large for big screens. So when I open another language in the large screen I got the layout from the small screen. Is there a way to manage this? layout-large-fr or layout-fr-large did not work...

And of course I defined translated strings.

Similar Question 4 (2 solutions) : Change language programmatically in Android

Similar Question 5 (1 solutions) : Changing language programmatically in Android

Similar Question 6 (2 solutions) : Android: changing activity language

Similar Question 7 (2 solutions) : Android localization arabic language

Similar Question 8 (1 solutions) : Android Language selection

Similar Question 9 (2 solutions) : android change language listener

cc