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

Resolve switching languages problem in settings fragment #5560

Merged
merged 3 commits into from
Mar 22, 2024
Merged
Changes from all 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
Original file line number Diff line number Diff line change
@@ -146,7 +146,7 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
appUiLanguageListPreference.setSummary(Locale.getDefault().getDisplayLanguage());
} else {
// If any language is selected by user previously, use it
Locale defLocale = new Locale(languageCode);
Locale defLocale = createLocale(languageCode);
appUiLanguageListPreference.setSummary((defLocale).getDisplayLanguage(defLocale));
}
appUiLanguageListPreference.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@@ -167,7 +167,7 @@ public boolean onPreferenceClick(Preference preference) {
descriptionLanguageListPreference.setSummary(Locale.getDefault().getDisplayLanguage());
} else {
// If any language is selected by user previously, use it
Locale defLocale = new Locale(languageCode);
Locale defLocale = createLocale(languageCode);
descriptionLanguageListPreference.setSummary(defLocale.getDisplayLanguage(defLocale));
}
descriptionLanguageListPreference.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@@ -361,7 +361,7 @@ public void onItemClick(AdapterView<?> adapterView, View view, int i,
}
recentLanguagesDao.addRecentLanguage(new Language(languageName, languageCode));
saveLanguageValue(languageCode, keyListPreference);
Locale defLocale = new Locale(languageCode);
Locale defLocale = createLocale(languageCode);
if(keyListPreference.equals("appUiDefaultLanguagePref")) {
appUiLanguageListPreference.setSummary(defLocale.getDisplayLanguage(defLocale));
setLocale(requireActivity(), languageCode);
@@ -426,7 +426,7 @@ private void onRecentLanguageClicked(String keyListPreference, Dialog dialog, Ad
recentLanguagesDao.addRecentLanguage(
new Language(recentLanguageName, recentLanguageCode));
saveLanguageValue(recentLanguageCode, keyListPreference);
final Locale defLocale = new Locale(recentLanguageCode);
final Locale defLocale = createLocale(recentLanguageCode);
if (keyListPreference.equals("appUiDefaultLanguagePref")) {
appUiLanguageListPreference.setSummary(defLocale.getDisplayLanguage(defLocale));
setLocale(requireActivity(), recentLanguageCode);
@@ -455,7 +455,7 @@ public void setLocale(final Activity activity, String userSelectedValue) {
if (userSelectedValue.equals("")) {
userSelectedValue = Locale.getDefault().getLanguage();
}
final Locale locale = new Locale(userSelectedValue);
final Locale locale = createLocale(userSelectedValue);
Locale.setDefault(locale);
final Configuration configuration = new Configuration();
configuration.locale = locale;
@@ -467,6 +467,25 @@ public void setLocale(final Activity activity, String userSelectedValue) {
editor.apply();
}

/**
* Create Locale based on different types of language codes
* @param languageCode
* @return Locale and throws error for invalid language codes
*/
public static Locale createLocale(String languageCode) {
String[] parts = languageCode.split("-");
switch (parts.length) {
case 1:
return new Locale(parts[0]);
case 2:
return new Locale(parts[0], parts[1]);
case 3:
return new Locale(parts[0], parts[1], parts[2]);
default:
throw new IllegalArgumentException("Invalid language code: " + languageCode);
}
}

/**
* Save userselected language in List Preference
* @param userSelectedValue
Original file line number Diff line number Diff line change
@@ -19,8 +19,10 @@ import fr.free.nrw.commons.TestCommonsApplication
import fr.free.nrw.commons.recentlanguages.Language
import fr.free.nrw.commons.recentlanguages.RecentLanguagesAdapter
import fr.free.nrw.commons.recentlanguages.RecentLanguagesDao
import fr.free.nrw.commons.upload.UploadMediaDetailAdapter
import fr.free.nrw.commons.settings.SettingsFragment.createLocale
import org.junit.Assert
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
@@ -34,6 +36,8 @@ import org.robolectric.Shadows
import org.robolectric.annotation.Config
import org.robolectric.annotation.LooperMode
import java.lang.reflect.Method
import java.util.Locale


@RunWith(RobolectricTestRunner::class)
@Config(sdk = [21], application = TestCommonsApplication::class)
@@ -255,4 +259,30 @@ class SettingsFragmentUnitTests {
verify(recentLanguagesTextView, times(1)).visibility = View.VISIBLE
}

@Test
fun testCreateLocaleWithLanguageCode() {
val locale: Locale = createLocale("en")

assertEquals("en", locale.language)
assertEquals("",locale.country)
assertEquals("",locale.variant)
}

@Test
fun testCreateLocaleWithLanguageAndCountryCode() {
val locale: Locale = createLocale("zh-CN")

assertEquals("zh", locale.language)
assertEquals("CN",locale.country)
assertEquals("",locale.variant)
}

@Test
fun testCreateLocaleWithLanguageCountryAndVariantCode() {
val locale: Locale = createLocale("pt-BR-variant")

assertEquals("pt", locale.language)
assertEquals("BR",locale.country)
assertEquals("variant",locale.variant)
}
}