Skip to content

Instantly share code, notes, and snippets.

@stdStudent
Last active September 7, 2024 04:27
Show Gist options
  • Save stdStudent/db91a8f7774ca6104636ebf27415084c to your computer and use it in GitHub Desktop.
Save stdStudent/db91a8f7774ca6104636ebf27415084c to your computer and use it in GitHub Desktop.
Android, Locale utilities: get a currently displayed locale at a device's system level, validate a locale or a language tag
/**
* Licensed under the BSD-3-Clause-Clear license.
* The license text in enclosed in the comment section.
*/
/**
* The idea is to use the hidden SYSTEM_LOCALES ("system_locales") setting
* to get the currently displayed locale. Although the documentation says
* that one must use `LocaleList.getDefault` instead, I couldn't really
* get the right locale when I changed it in the system settings.
*/
/**
* @return currently displayed locale at a device's system level or null
*/
fun getCurrentLocale(context: Context): Locale? {
val devicesLocales = Settings.System.getString(
context.contentResolver,
"system_locales"
)
val displayedLocale = if (devicesLocales != null && devicesLocales.isNotEmpty()) {
// get the first language as it is what is currently displayed for the user
val locales = devicesLocales.split(",")
val languageTag = locales[0]
// retrieve locale from the language tag
Locale.forLanguageTag(languageTag)
} else {
// it should be unreachable, but you never know
null
}
return displayedLocale
}
/**
* @param fallbackLocale must be set in case the device's locale is not retrievable
* @return currently displayed locale at a device's system level or `fallbackLocale`
*/
fun getCurrentLocale(context: Context, fallbackLocale: Locale): Locale {
return getCurrentLocale(context) ?: fallbackLocale
}
/**
* Licensed under the BSD-3-Clause-Clear license.
* The license text in enclosed in the comment section.
*/
fun isLocaleValid(locale: Locale): Boolean {
val availableLocales = Locale.getAvailableLocales()
return locale in availableLocales
}
fun isLanguageTagValid(languageTag: String): Boolean {
val availableLanguageTags = Locale.getAvailableLocales().map { it.toLanguageTag() }
return languageTag in availableLanguageTags
}
@stdStudent
Copy link
Author

LICENSE
The Clear BSD License

Copyright (c) 2024 stdStudent
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted (subject to the limitations in the disclaimer
below) provided that the following conditions are met:

   * Redistributions of source code must retain the above copyright notice,
   this list of conditions and the following disclaimer.

   * Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.

   * Neither the name of the copyright holder nor the names of its
   contributors may be used to endorse or promote products derived from this
   software without specific prior written permission.

NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY
THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment