Question:
There is a method in java:
public static Locale parseStringLocale(String locale) {
return locale.length() > 2
? new Locale(locale.substring(0, 2), locale.substring(3))
: new Locale(locale);
}
When I try to convert it to kotlin, I get something like this:
fun parseStringLocale(locale: String): Locale {
return if (locale.length > 2)
Locale(locale.substring(0, 2), locale.substring(3))
else
Locale(locale)
}
But it is no longer static, it should somehow glue the companion object or how the issue can be solved for the future call of this method.
Answer:
Static methods in Kotlin are set through the companion object
those. in your case
companion object{
fun parseStringLocale(locale: String): Locale {
return if (locale.length > 2)
Locale(locale.substring(0, 2), locale.substring(3))
else
Locale(locale)
}
}