Arpan Sarkar
01/12/2022, 2:27 PMclass 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
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))
}
}
marlonlom
01/12/2022, 3:14 PMArpan Sarkar
01/12/2022, 5:26 PM