Is changing the app language an anti-pattern?

I’ve always had problems with apps that require in-app language changes, no matter the system’s language. This is hard to achieve and it feels wrong. Is this considered an antipattern?

If you mean hacking a Locale change, I would describe it more as “unsupported”.

This kind of implementation:

import android.annotation.SuppressLint
import android.content.Context
import android.content.res.Resources
import android.os.Build
import java.util.*

object LanguageUtils {

    fun changeLanguage(context: Context, localeName: String): Resources {
        val res = context.resources
        val dm = res.displayMetrics
        val configuration = res.configuration
        configuration.setLocale(Locale(localeName))
        res.updateConfiguration(configuration, dm)
        context.createConfigurationContext(configuration)
        return res
    }

    @SuppressLint("ObsoleteSdkInt")
    @SuppressWarnings("Deprecated in Android 17")
    fun applyLanguage(context: Context, language: String): Context {
        val locale = Locale(language)
        val configuration = context.resources.configuration
        val displayMetrics = context.resources.displayMetrics

        Locale.setDefault(locale)

        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            configuration.setLocale(locale)
            context.createConfigurationContext(configuration)
        } else {
            configuration.locale = locale
            context.resources.updateConfiguration(configuration, displayMetrics)
            context
        }
    }

    const val DEFAULT_LANGUAGE = "it"
    const val ENGLISH_LANGUAGE = "en"
    const val ITALIAN_LANGUAGE = "it"
}

Sorry for the delay in responding! But, yes, that is what I was referring to. It is unsupported, and I would not be the least bit surprised if people run into problems with it, particularly with certain devices. If you want to call that an anti-pattern, I would not argue.

1 Like

Hello!
If you don’t mind I think my experience can help with understanding how it Can be implemented properly.

I implemented the support of many languages in one of my apps. And the logic of choosing the language is following:
Firstly the language is set the same as the device locale. Even if the device locale would be changed, the language in the app would change as well.
But once the user manually changes the language in the app, I will always override the Locale to apply custom language set by the user. Even if the default device language is changed, I will still use custom.

I can’t say, that this case is popular, but I suppose it happens sometimes. English Locale can be set as a default language of the system not only by native English speakers. And when they open the app and see an option to change app’s language to their native one, it would please them.

I would like to notice. The translation of notifications should be done as well. I made this mistake in one of the first versions, and it lead to funny cases :slight_smile:

Hello! Don’t worry, I appreciate your input. Are you willing to share a snippet of your implementation?

Probably it would be better to make a sample app with the implementation. There are several parts.

I will share a link to the repo in 1-2 days. Will it be OK?

1 Like

Sure! If you feel like doing it. Thank you!

Hello, any updates on this? The language it’s buggy on some devices and I have no clue…