Last active
May 4, 2018 14:35
-
-
Save Ulop/043b16c27eb3dae285e889349b651251 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.coin.profit.base | |
import android.os.Build | |
import android.annotation.TargetApi | |
import android.content.Context | |
import android.content.ContextWrapper | |
import android.content.res.Configuration | |
import java.util.* | |
/** | |
* Created by user on 14.07.2017. | |
*/ | |
class MyContextWrapper(base: Context) : ContextWrapper(base) { | |
companion object { | |
@Suppress("DEPRECATION") | |
fun wrap(ctx: Context, language: String): ContextWrapper { | |
var context = ctx | |
val config = context.resources.configuration | |
val sysLocale = when { | |
Build.VERSION.SDK_INT >= Build.VERSION_CODES.N -> getSystemLocale(config) | |
else -> getSystemLocaleLegacy(config) | |
} | |
if (language != "") { | |
val locale = Locale(language) | |
Locale.setDefault(locale) | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { | |
setSystemLocale(config, locale) | |
} else { | |
setSystemLocaleLegacy(config, locale) | |
} | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { | |
config.setLayoutDirection(locale) | |
context = context.createConfigurationContext(config) | |
} else { | |
context.resources.updateConfiguration(config, context.resources.displayMetrics) | |
} | |
} | |
return CoinContextWrapper(context) | |
} | |
@Suppress("DEPRECATION") | |
fun update(ctx: Context, language: String){ | |
val config = ctx.resources.configuration | |
if (language != "") { | |
val locale = Locale(language) | |
Locale.setDefault(locale) | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { | |
setSystemLocale(config, locale) | |
} else { | |
setSystemLocaleLegacy(config, locale) | |
} | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { | |
config.setLayoutDirection(locale) | |
//context = context.createConfigurationContext(config) | |
} else { | |
ctx.resources.updateConfiguration(config, ctx.resources.displayMetrics) | |
} | |
} | |
} | |
@Suppress("DEPRECATION") | |
private fun getSystemLocaleLegacy(config: Configuration) = config.locale | |
@TargetApi(Build.VERSION_CODES.N) | |
private fun getSystemLocale(config: Configuration) = config.locales[0] | |
@Suppress("DEPRECATION") | |
private fun setSystemLocaleLegacy(config: Configuration, locale: Locale) { | |
config.locale = locale | |
} | |
@TargetApi(Build.VERSION_CODES.N) | |
private fun setSystemLocale(config: Configuration, locale: Locale) { | |
config.setLocale(locale) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override fun attachBaseContext(newBase: Context) { | |
super.attachBaseContext(MyContextWrapper.wrap(newBase,appLanguage)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment