Merge pull request #910 from Isira-Seneviratne/Locale_forLanguageTag
Add compat Locale.forLanguageTag() implementation.
This commit is contained in:
commit
f45966d449
|
@ -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;
|
||||
|
@ -14,6 +12,9 @@ import java.util.Locale;
|
|||
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");
|
||||
|
||||
|
@ -38,19 +39,7 @@ public class Localization implements Serializable {
|
|||
* @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) {
|
||||
|
|
|
@ -2,6 +2,7 @@ package org.schabi.newpipe.extractor.stream;
|
|||
|
||||
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;
|
||||
|
||||
|
@ -230,26 +231,7 @@ public final class SubtitlesStream extends Stream {
|
|||
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;
|
||||
|
|
|
@ -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 + "]");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue