Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

5338 bug fix #5340

Merged
merged 8 commits into from
Oct 17, 2023
Merged
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion app/src/main/java/fr/free/nrw/commons/upload/LanguagesAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ class LanguagesAdapter constructor(
private val selectedLanguages: HashMap<*, String>
) : ArrayAdapter<String?>(context, R.layout.row_item_languages_spinner) {

companion object {
/**
* Represents the default index for the language list. By default, this index corresponds to the
* English language. This serves as a fallback when the user's system language is not present in
* the language_list.xml. Though this default can be changed by the user, it does not affect other
* functionalities of the application. Fix bug issue 5338
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tiny change: Would you mind changing "Fix" to "Fixes" ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, I fix that, can you make approval for it? Thanks!

*/
const val DEFAULT_INDEX = 0
nicolas-raoul marked this conversation as resolved.
Show resolved Hide resolved
}

private var languageNamesList: List<String>
private var languageCodesList: List<String>

Expand Down Expand Up @@ -85,11 +95,33 @@ class LanguagesAdapter constructor(
return languageNamesList[position]
}

/**
* Retrieves the index of the user's default locale from the list of available languages.
*
* This function checks the user's system language and finds its index within the application's
* list of supported languages. If the system language is not supported, or any error occurs,
* it falls back to the default language index, typically representing English.
*
*
* @param context The context used to get the user's system locale.
* @return The index of the user's default language in the supported language list,
* or the default index if the language is not found.
* Note: This function was implemented to address a bug where unsupported system languages
* resulted in an incorrect language selection. Directly returning the result of `indexOf`
* without checking its validity could result in returning an index of -1, leading to ArrayIndex
* OutOfBoundsException.
* [See bug issue 5338]
* It's essential to ensure that the returned index is valid or fall back to a default index.
* Future contributors are advised not to simplify this function without addressing this concern.
*/
fun getIndexOfUserDefaultLocale(context: Context): Int {
return language.codes.indexOf(context.locale!!.language)

val userLanguageCode = context.locale?.language ?: return DEFAULT_INDEX
nicolas-raoul marked this conversation as resolved.
Show resolved Hide resolved
return language.codes.indexOf(userLanguageCode).takeIf { it >= 0 } ?: DEFAULT_INDEX
}

fun getIndexOfLanguageCode(languageCode: String): Int {

return languageCodesList.indexOf(languageCode)
}

Expand Down