-2

I am working on kotlin language in my application and facing some difficulties regarding Changing Locale. As what i have do i have created one global class i.e on java and on this class i have wrote code of Language change locale code and that language code function i am calling to base application class of my project that is in Kotlin.

Here is my source code :

Global class (Java Code)

 public void changelanguage(Context context) {
    Locale locale = Locale.getDefault();
    Locale.setDefault(locale);
    System.out.println("GlobalLocale" +locale.toString());
    Configuration config = new Configuration();
    config.locale = locale;
    context.getResources().updateConfiguration(config,
            context.getResources().getDisplayMetrics());

}  

Base Application Class Code:(Kotlin)

class CrashApplicationClass : Application() {

val TAG = CrashApplicationClass::class.java.simpleName
lateinit var sDefSystemLanguage: String
val gc = GlobalClass.getInstance()

override fun onCreate() {
    // TODO Auto-generated method stub

    // ACRA.init(this)
    super.onCreate()

    instance = this

    gc.changelanguage(baseContext)
    println("Languagekotlin" +  gc.changelanguage(baseContext))
    println("kotlininstance" + instance)
   // sDefSystemLanguage = Locale.getDefault().language
}


val requestQueue: RequestQueue? = null
    get() {
        if (field == null) {
            return Volley.newRequestQueue(applicationContext)
        }
        return field
    }

fun <T> addToRequestQueue(request: Request<T>, tag: String) {
    request.tag = if (TextUtils.isEmpty(tag)) TAG else tag
    requestQueue?.add(request)
}

fun <T> addToRequestQueue(request: Request<T>) {
    request.tag = TAG
    requestQueue?.add(request)
}

fun cancelPendingRequests(tag: Any) {
    if (requestQueue != null) {
        requestQueue!!.cancelAll(tag)
    }
}


companion object {
     val TAG = CrashApplicationClass::class.java.simpleName

    @get:Synchronized var instance: CrashApplicationClass? = null

        private set
}

}

As whats going in code here , I have print logs in java class log the language properly gets : System.out.println("GlobalLocale" +locale.toString());

but on CrashApplication class log : println("Languagekotlin" + gc.changelanguage(baseContext)) -> i am getting "Kotlin.Unit" And also language not changed getting proper locale in java but don't know Kotlin class is not getting it returns "Kotlin.Unit"

And also on Manifest i have set : android:configChanges="locale" Any one have idea about language change in Kotlin. Plz share.

Jyoti
  • 87
  • 3
  • 12
  • Possible duplicate of [Change language programmatically in Android](https://stackoverflow.com/questions/2900023/change-language-programmatically-in-android) – Zoe Sep 15 '17 at 07:34
  • I have voted to close this as a duplicate because the code is the exact same. The only difference between Java and Kotlin is the syntax. You can use the same methods (and you have to use the same methods) and same techniques. java and Kotlin use the same methods when running the Android framework, the only difference is syntax (and null safety, but in terms of the code itself) – Zoe Sep 15 '17 at 07:35
  • `println("Languagekotlin" + gc.changelanguage(baseContext))` prints "Languagekotlinkotlin.Unit" because `changelanguage` is a `void` function. – marstran Sep 15 '17 at 08:29
  • @ LunarWatcher but ur code and question is not related to kotlin language.. !! as locale code in java works fine. i am asking relevant to kotlin programming...!! – Jyoti Sep 15 '17 at 09:24

1 Answers1

3

I have resolved this issue in Kotlin Language now the language changes by improving this function

public String changelanguage(Context context) {
    //String lang = "hi_IN";
  //  Locale locale = new Locale(lang);
    Locale locale = Locale.getDefault();
    Locale.setDefault(locale);
    System.out.println("GlobalLocale" +locale.toString());
    Configuration config = new Configuration();
    config.locale = locale;
    context.getResources().updateConfiguration(config,
    context.getResources().getDisplayMetrics());
    return  locale.toString();
} 

and it resolved my Kotlin.unit error and @marstran thanks for giving me hint about my issue where i am going wrong. now the issue has resolved in Kotlin.

@LunarWatcher before doing negative voting please read question carefully.! Now Just vote up , do positive vote.!!

Jyoti
  • 87
  • 3
  • 12