Use Locale.forLanguageTag().

This commit is contained in:
Isira Seneviratne 2022-08-22 21:26:19 +05:30
parent a822e91909
commit b232c29d22
2 changed files with 7 additions and 36 deletions

View File

@ -2,9 +2,6 @@ package org.schabi.newpipe.extractor.localization;
import org.schabi.newpipe.extractor.exceptions.ParsingException;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
@ -14,6 +11,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 +38,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(Locale.forLanguageTag(localizationCode));
}
public Localization(@Nonnull final String languageCode, @Nullable final String countryCode) {

View File

@ -1,5 +1,7 @@
package org.schabi.newpipe.extractor.stream;
import static org.schabi.newpipe.extractor.utils.Utils.EMPTY_STRING;
import org.schabi.newpipe.extractor.MediaFormat;
import org.schabi.newpipe.extractor.services.youtube.ItagItem;
@ -230,26 +232,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 = Locale.forLanguageTag(languageCode);
this.code = languageCode;
this.format = mediaFormat;
this.autoGenerated = autoGenerated;