Я пытаюсь написать свою собственную библиотеку локализации.
Я пытаюсь выяснить, как переопределить все вызовы строковых активов.
Я понял, что у меня может быть ContextWrapper как:
class MyConextWrapper(context: Context?) : ContextWrapper(context) {
private val _baseResources = context?.resources
private var customResources : Resources? = null
override fun getResources(): Resources {
if (customResources == null) {
customResources = MyRes(_baseResources?.assets, _baseResources?.displayMetrics, _baseResources?.configuration)
}
return customResources!!
}
}
class MyRes : Resources {
constructor(assets: AssetManager?, metrics: DisplayMetrics?, config: Configuration?) : super(assets, metrics, config)
override fun getString(id: Int): String {
return super.getString(id)
}
override fun getString(id: Int, vararg formatArgs: Any?): String {
return super.getString(id, *formatArgs)
}
override fun getText(id: Int): CharSequence {
return super.getText(id)
}
override fun getText(id: Int, def: CharSequence?): CharSequence {
return super.getText(id, def)
}
}
А затем переопределить в моей деятельности, как
override fun attachBaseContext(newBase: Context?) {
super.attachBaseContext(MyConextWrapper(newBase))
}
Теперь все вызовы getString (R.String. *) Будут проходить через мой код
Но как мне беспрепятственно переопределить такие вещи, как
<TextView
android:id="@+id/headlineTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/string_id"/>
Как мне переопределить строку, которая выходит из этого актива string_id?
При отладке работы TextView я вижу, что он получает строку из TypedArray
, которая, в свою очередь, берет ее непосредственно из assetManager
, и этот класс является окончательным, поэтому я не могу переопределить его в моем ContextWrapper.
У меня вопрос, как я могу изменить это поведение, например https://lokalise.co/ говорят, что они делают