Is changing the app language an anti-pattern?

from the CommonsWare Community archives

At June 18, 2021, 11:50am, grrigore asked:

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?


At June 18, 2021, 12:07pm, mmurphy replied:

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


At June 18, 2021, 12:48pm, grrigore replied:

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"
}

At June 20, 2021, 10:52pm, mmurphy replied:

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.


At June 21, 2021, 2:25pm, relaxedsoul replied:

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.


At June 21, 2021, 2:50pm, relaxedsoul replied:

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:


At June 22, 2021, 7:01am, grrigore replied:

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


At June 22, 2021, 1:52pm, relaxedsoul replied:

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?


At June 22, 2021, 2:39pm, grrigore replied:

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


At October 11, 2021, 9:07am, grrigore replied:

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