Skip to content

Instantly share code, notes, and snippets.

@ismailgungor
Created March 28, 2019 11:44
Show Gist options
  • Save ismailgungor/dc62c795fd6a86653956be5fb5ff0770 to your computer and use it in GitHub Desktop.
Save ismailgungor/dc62c795fd6a86653956be5fb5ff0770 to your computer and use it in GitHub Desktop.
package com.kekyazilim.annotationreflectionexample.translator.kotlin
import android.app.Activity
import android.widget.TextView
import com.kekyazilim.annotationreflectionexample.translator.fakes.FakeTranslatorJson
import java.util.*
object KViewTranslator {
fun bindAndTranslate(target: Activity) {
val language = Locale.getDefault().language
val fakeJson = FakeTranslatorJson().getLanguageJsonObject()
for (field in target.javaClass.declaredFields) {
val currentAnnotation = field.getAnnotation(KBindTranslateView::class.java)
currentAnnotation?.let {
val translatedMessage = fakeJson
.getJSONObject(currentAnnotation.key)
.getString(language)
field.set(target, target.findViewById(currentAnnotation.value))
if (TextView::class.java.isAssignableFrom(field.type))
(target.findViewById(currentAnnotation.value) as TextView)
.text = translatedMessage
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment