Merge pull request #910 from Isira-Seneviratne/Locale_forLanguageTag

Add compat Locale.forLanguageTag() implementation.
This commit is contained in:
AudricV 2022-12-24 23:53:30 +01:00 committed by GitHub
commit f45966d449
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 36 deletions

View file

@ -1,9 +1,7 @@
package org.schabi.newpipe.extractor.localization; package org.schabi.newpipe.extractor.localization;
import org.schabi.newpipe.extractor.exceptions.ParsingException; import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.utils.LocaleCompat;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
@ -14,6 +12,9 @@ import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
public class Localization implements Serializable { public class Localization implements Serializable {
public static final Localization DEFAULT = new Localization("en", "GB"); 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()} * @param localizationCode a localization code, formatted like {@link #getLocalizationCode()}
*/ */
public static Localization fromLocalizationCode(final String localizationCode) { public static Localization fromLocalizationCode(final String localizationCode) {
final int indexSeparator = localizationCode.indexOf("-"); return fromLocale(LocaleCompat.forLanguageTag(localizationCode));
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);
} }
public Localization(@Nonnull final String languageCode, @Nullable final String countryCode) { public Localization(@Nonnull final String languageCode, @Nullable final String countryCode) {

View file

@ -2,6 +2,7 @@ package org.schabi.newpipe.extractor.stream;
import org.schabi.newpipe.extractor.MediaFormat; import org.schabi.newpipe.extractor.MediaFormat;
import org.schabi.newpipe.extractor.services.youtube.ItagItem; import org.schabi.newpipe.extractor.services.youtube.ItagItem;
import org.schabi.newpipe.extractor.utils.LocaleCompat;
import java.util.Locale; import java.util.Locale;
@ -230,26 +231,7 @@ public final class SubtitlesStream extends Stream {
final boolean autoGenerated, final boolean autoGenerated,
@Nullable final String manifestUrl) { @Nullable final String manifestUrl) {
super(id, content, isUrl, mediaFormat, deliveryMethod, manifestUrl); super(id, content, isUrl, mediaFormat, deliveryMethod, manifestUrl);
this.locale = LocaleCompat.forLanguageTag(languageCode);
/*
* 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.code = languageCode; this.code = languageCode;
this.format = mediaFormat; this.format = mediaFormat;
this.autoGenerated = autoGenerated; this.autoGenerated = autoGenerated;

View file

@ -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 + "]");
}
}