Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add compat Locale.forLanguageTag() implementation. #910

Merged
merged 3 commits into from
Dec 24, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
@@ -1,7 +1,10 @@
package org.schabi.newpipe.extractor.stream;

import static org.schabi.newpipe.extractor.utils.Utils.EMPTY_STRING;

Isira-Seneviratne marked this conversation as resolved.
Show resolved Hide resolved
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 +233,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,36 @@
package org.schabi.newpipe.extractor.utils;

import java.util.Locale;

public class LocaleCompat {
Isira-Seneviratne marked this conversation as resolved.
Show resolved Hide resolved
private LocaleCompat() {
}

// Source: LocaleListCompat's private forLanguageTagCompat() method.
// Use Locale.forLanguageTag() on API level >= 21 instead.
Isira-Seneviratne marked this conversation as resolved.
Show resolved Hide resolved
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 + "]");
AudricV marked this conversation as resolved.
Show resolved Hide resolved
}
}