https://kotlinlang.org logo
#android-architecture
Title
# android-architecture
a

Arpan Sarkar

01/12/2022, 2:27 PM
Our app single activity architecture using androidx navigation. Now the multiple language support will roll out in phases. For the time being some fragment need to support localization (with RTL for some languages) and some fragments need to show English only how can i archive this ? On app local is forced to be English
Copy code
class App : Application() {
    private var baseCtx: Context? = null
    private var appCtx: Context? = null
    private var baseRes: Resources? = null

    override fun attachBaseContext(base: Context) {
        val ctx = baseCtx ?: base.createLocalizedContext(Locale.ENGLISH).also {
            baseCtx = it
        }
        super.attachBaseContext(ctx)
    }

    override fun getApplicationContext(): Context {
        return appCtx ?: super.getApplicationContext().createLocalizedContext(Locale.ENGLISH).also {
            appCtx = it
        }
    }

    override fun getResources(): Resources {
        return baseRes ?: baseContext.createLocalizedContext(Locale.ENGLISH).resources.also {
            baseRes = it
        }
    }
}
I am planing to do some thing like this for fragments which requires translation not sure if its a good idea or not
Copy code
override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        val themedContext = ContextThemeWrapper(
            inflater.context,
            R.style.Theme_LocalizedFragment_Green // apply brand them green
        )
        val local = getLocal() // get local from local storage
        if (requiredTranslation) {
            themedContext.applyOverrideConfiguration(
                requireContext().createLocalizedConfiguration(local)
            )
        }
        val themedInflater = inflater.cloneInContext(themedContext)
        return super.onCreateView(themedInflater, container, savedInstanceState).apply {
            // adjust layout direction as per local
            ViewCompat.setLayoutDirection(this, TextUtilsCompat.getLayoutDirectionFromLocale(local))
        }
    }
m

marlonlom

01/12/2022, 3:14 PM
in build.gradle.kts is not possible to setup the multilanguage support?
a

Arpan Sarkar

01/12/2022, 5:26 PM
no in build.gradle.kts its not possible
2 Views