I currently have 2 localizations, English and Spanish, for an app I'm building to create recipes. Part of the recipe creation process involves doing some text parsing on each step. I am attempting to build a MutableMap of each localization and all the strings from a string-array of common measurement units. Here is how I am attempting to do this:
for (i in 0 until resources.configuration.locales.size()) {
val currentLocaleList = resources.configuration.locales
val currentLocale = currentLocaleList.get(i).toLanguageTag()
val configuration = Configuration()
configuration.setLocale(Locale.forLanguageTag(currentLocale))
val context = context?.createConfigurationContext(configuration)
val measurementsLocaleUnits = context?.resources?.getStringArray(
R.array.measurement_units) as Array<String>
measurementLocales[currentLocale] = measurementsLocaleUnits
}
When I test this on my Galaxy S21+ (Android 13), it works correctly. It finds the English and Spanish localizations of my string-array, measurement_units, but I gave the app to my girlfriend who has a Galaxy A71 (Android 12), it only finds the current localization her phone is set to. I also tested it on an emulated Google Pixel 4, which also uses Android 12, and I have the same issue. I have tried a variety of different ways of retrieving the locales, but everything I tried seemed to be getting EVERY locale that exists on the phone, not the locales of the localizations for the string resources. I don't want to hard code the language tags of the localizations in case I add more later, so I want it to be generalized. Any help would be appreciated.
Edit: I have discovered the source of the problem. It is not related to the version of Android, but rather the phone itself. If you don't have English and Spanish on the device, it will crash, but if you have English and Spanish installed on the device, it will work. I have realized now that resources.configuration.locales
is getting the locales of the actual device, not all the locales of the string resources I have created in Android Studio. So I need to get these locales, not the locales of the device.