Allows easy iteration about all available locales. Kotlin Collection implementation for LocaleList which doesn't implement Collection<Locale> itself.
package com.pascalwelsch.android.util | |
import android.content.Context | |
import androidx.core.os.ConfigurationCompat | |
import androidx.core.os.LocaleListCompat | |
import java.util.Locale | |
/** | |
* Returns an [Iterable] for the languages of the user, sorted by priority. First choice first. | |
*/ | |
fun Context.userLocales(): Iterable<Locale> { | |
return ConfigurationCompat.getLocales(resources.configuration).asIterable() | |
} | |
fun LocaleListCompat.asIterable(): Iterable<Locale> = LocaleIterable(this) | |
private class LocaleIterable(private val localList: LocaleListCompat) : Iterable<Locale> { | |
override fun iterator(): Iterator<Locale> = LocaleIterator(localList) | |
} | |
private class LocaleIterator(private val localList: LocaleListCompat) : Iterator<Locale> { | |
var i = -1 | |
override fun hasNext(): Boolean = (i + 1) < localList.size() | |
override fun next(): Locale { | |
if (!hasNext()) throw NoSuchElementException() | |
i++ | |
return localList.get(i) | |
} | |
} |
This comment has been minimized.
This comment has been minimized.
I guess
should be
|
This comment has been minimized.
This comment has been minimized.
Thanks @tobltobs, I updated it |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
Usage: