Skip to content

Commit

Permalink
Merge pull request #910 from Isira-Seneviratne/Locale_forLanguageTag
Browse files Browse the repository at this point in the history
Add compat Locale.forLanguageTag() implementation.
  • Loading branch information
AudricV authored Dec 24, 2022
2 parents d5437e0 + 3b80547 commit f45966d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package org.schabi.newpipe.extractor.localization;

import org.schabi.newpipe.extractor.exceptions.ParsingException;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.schabi.newpipe.extractor.utils.LocaleCompat;

import java.io.Serializable;
import java.util.ArrayList;
Expand All @@ -14,6 +12,9 @@
import java.util.Map;
import java.util.Objects;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public class Localization implements Serializable {
public static final Localization DEFAULT = new Localization("en", "GB");

Expand All @@ -38,19 +39,7 @@ public static List<Localization> listFrom(final String... localizationCodeList)
* @param localizationCode a localization code, formatted like {@link #getLocalizationCode()}
*/
public static Localization fromLocalizationCode(final String localizationCode) {
final int indexSeparator = localizationCode.indexOf("-");

final String languageCode;
final String countryCode;
if (indexSeparator != -1) {
languageCode = localizationCode.substring(0, indexSeparator);
countryCode = localizationCode.substring(indexSeparator + 1);
} else {
languageCode = localizationCode;
countryCode = null;
}

return new Localization(languageCode, countryCode);
return fromLocale(LocaleCompat.forLanguageTag(localizationCode));
}

public Localization(@Nonnull final String languageCode, @Nullable final String countryCode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.schabi.newpipe.extractor.MediaFormat;
import org.schabi.newpipe.extractor.services.youtube.ItagItem;
import org.schabi.newpipe.extractor.utils.LocaleCompat;

import java.util.Locale;

Expand Down Expand Up @@ -230,26 +231,7 @@ private SubtitlesStream(@Nonnull final String id,
final boolean autoGenerated,
@Nullable final String manifestUrl) {
super(id, content, isUrl, mediaFormat, deliveryMethod, manifestUrl);

/*
* Locale.forLanguageTag only for Android API >= 21
* Locale.Builder only for Android API >= 21
* Country codes doesn't work well without
*/
final String[] splits = languageCode.split("-");
switch (splits.length) {
case 2:
this.locale = new Locale(splits[0], splits[1]);
break;
case 3:
// Complex variants don't work!
this.locale = new Locale(splits[0], splits[1], splits[2]);
break;
default:
this.locale = new Locale(splits[0]);
break;
}

this.locale = LocaleCompat.forLanguageTag(languageCode);
this.code = languageCode;
this.format = mediaFormat;
this.autoGenerated = autoGenerated;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.schabi.newpipe.extractor.utils;

import java.util.Locale;

/**
* This class contains a simple implementation of {@link Locale#forLanguageTag(String)} for Android
* API levels below 21 (Lollipop). This is needed as core library desugaring does not backport that
* method as of this writing.
*
* Relevant issue: https://issuetracker.google.com/issues/171182330
*/
public final class LocaleCompat {
private LocaleCompat() {
}

// Source: The AndroidX LocaleListCompat class's private forLanguageTagCompat() method.
// Use Locale.forLanguageTag() on Android API level >= 21 / Java instead.
public static Locale forLanguageTag(final String str) {
if (str.contains("-")) {
final String[] args = str.split("-", -1);
if (args.length > 2) {
return new Locale(args[0], args[1], args[2]);
} else if (args.length > 1) {
return new Locale(args[0], args[1]);
} else if (args.length == 1) {
return new Locale(args[0]);
}
} else if (str.contains("_")) {
final String[] args = str.split("_", -1);
if (args.length > 2) {
return new Locale(args[0], args[1], args[2]);
} else if (args.length > 1) {
return new Locale(args[0], args[1]);
} else if (args.length == 1) {
return new Locale(args[0]);
}
} else {
return new Locale(str);
}

throw new IllegalArgumentException("Can not parse language tag: [" + str + "]");
}
}

0 comments on commit f45966d

Please sign in to comment.