Add backport implementation of Locale.forLanguageTag().
This commit is contained in:
parent
b232c29d22
commit
b90a566dd8
3 changed files with 40 additions and 2 deletions
|
@ -1,6 +1,7 @@
|
|||
package org.schabi.newpipe.extractor.localization;
|
||||
|
||||
import org.schabi.newpipe.extractor.exceptions.ParsingException;
|
||||
import org.schabi.newpipe.extractor.utils.LocaleCompat;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
|
@ -38,7 +39,7 @@ public class Localization implements Serializable {
|
|||
* @param localizationCode a localization code, formatted like {@link #getLocalizationCode()}
|
||||
*/
|
||||
public static Localization fromLocalizationCode(final String localizationCode) {
|
||||
return fromLocale(Locale.forLanguageTag(localizationCode));
|
||||
return fromLocale(LocaleCompat.forLanguageTag(localizationCode));
|
||||
}
|
||||
|
||||
public Localization(@Nonnull final String languageCode, @Nullable final String countryCode) {
|
||||
|
|
|
@ -4,6 +4,7 @@ import static org.schabi.newpipe.extractor.utils.Utils.EMPTY_STRING;
|
|||
|
||||
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;
|
||||
|
||||
|
@ -232,7 +233,7 @@ public final class SubtitlesStream extends Stream {
|
|||
final boolean autoGenerated,
|
||||
@Nullable final String manifestUrl) {
|
||||
super(id, content, isUrl, mediaFormat, deliveryMethod, manifestUrl);
|
||||
this.locale = Locale.forLanguageTag(languageCode);
|
||||
this.locale = LocaleCompat.forLanguageTag(languageCode);
|
||||
this.code = languageCode;
|
||||
this.format = mediaFormat;
|
||||
this.autoGenerated = autoGenerated;
|
||||
|
|
|
@ -0,0 +1,36 @@
|
|||
package org.schabi.newpipe.extractor.utils;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
public class LocaleCompat {
|
||||
private LocaleCompat() {
|
||||
}
|
||||
|
||||
// Source: LocaleListCompat's private forLanguageTagCompat() method.
|
||||
// Use Locale.forLanguageTag() on API level >= 21 instead.
|
||||
public static Locale forLanguageTag(final String str) {
|
||||
if (str.contains("-")) {
|
||||
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("_")) {
|
||||
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 + "]");
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue