Skip to content

Instantly share code, notes, and snippets.

@yoesuv
Created June 1, 2020 09:23
Show Gist options
  • Save yoesuv/3ad86d4ce2615f6a83fc2d3a68ba21cb to your computer and use it in GitHub Desktop.
Save yoesuv/3ad86d4ce2615f6a83fc2d3a68ba21cb to your computer and use it in GitHub Desktop.
import android.content.Context
import android.os.Build
import android.os.LocaleList
import java.util.*
class ContextWrapper(base: Context) : android.content.ContextWrapper(base) {
companion object {
fun wrap(context: Context, newLocale: Locale): ContextWrapper {
var mContext = context
val res = mContext.resources
val configuration = res.configuration
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
configuration.setLocale(newLocale)
val localeList = LocaleList(newLocale)
LocaleList.setDefault(localeList)
mContext = mContext.createConfigurationContext(configuration)
} else {
configuration.locale = newLocale
res.updateConfiguration(configuration, res.displayMetrics)
}
return ContextWrapper(mContext)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment