Skip to content

Commit

Permalink
Extract locale name util
Browse files Browse the repository at this point in the history
  • Loading branch information
charlag committed Mar 3, 2024
1 parent 0e13cef commit abbd30a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import com.keylesspalace.tusky.util.CustomEmojiHelper;
import com.keylesspalace.tusky.util.ImageLoadingHelper;
import com.keylesspalace.tusky.util.LinkHelper;
import com.keylesspalace.tusky.util.LocaleUtilsKt;
import com.keylesspalace.tusky.util.NumberUtils;
import com.keylesspalace.tusky.util.StatusDisplayOptions;
import com.keylesspalace.tusky.util.TimestampUtils;
Expand Down Expand Up @@ -845,9 +846,8 @@ private void setTranslationStatus(StatusViewData.Concrete status, StatusActionLi
if (translationViewData instanceof TranslationViewData.Loaded) {
Translation translation = ((TranslationViewData.Loaded) translationViewData).getData();
translationStatusView.setVisibility(View.VISIBLE);
// FIXME error handling
var locale = new Locale(translation.getDetectedSourceLanguage());
translationStatusView.setText(translationStatusView.getContext().getString(R.string.label_translated, locale.getDisplayName(), translation.getProvider()));
var langName = LocaleUtilsKt.localeNameForUntrustedISO639LangCode(translation.getDetectedSourceLanguage());
translationStatusView.setText(translationStatusView.getContext().getString(R.string.label_translated, langName, translation.getProvider()));
untranslateButton.setVisibility(View.VISIBLE);
untranslateButton.setOnClickListener((v) -> listener.onUntranslate(getBindingAdapterPosition()));
} else {
Expand Down Expand Up @@ -943,14 +943,14 @@ private String getTranslatedDescription(Context context, TranslationViewData tra
return context.getString(R.string.label_translating);
} else {
Translation translation = ((TranslationViewData.Loaded) translationViewData).getData();
// FIXME error handling
var locale = new Locale(translation.getDetectedSourceLanguage());
return context.getString(R.string.label_translated, locale.getDisplayName(), translation.getProvider());
var langName = LocaleUtilsKt.localeNameForUntrustedISO639LangCode(translation.getDetectedSourceLanguage());
return context.getString(R.string.label_translated, langName, translation.getProvider());
}
}

private static CharSequence getReblogDescription(Context context,
@NonNull StatusViewData.Concrete status) {
@Nullable
Status reblog = status.getRebloggingStatus();
if (reblog != null) {
return context
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/com/keylesspalace/tusky/util/LocaleUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,8 @@ fun getLocaleList(initialLanguages: List<String>): List<Locale> {
ensureLanguagesAreFirst(locales, initialLanguages)
return locales
}

fun localeNameForUntrustedISO639LangCode(code: String): String {
// It seems like it never throws?
return Locale(code).displayName
}

0 comments on commit abbd30a

Please sign in to comment.