Skip to content

Commit 1f26212

Browse files
Replace Localization with Locale
1 parent 3501172 commit 1f26212

51 files changed

Lines changed: 388 additions & 609 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

extractor/src/main/java/org/schabi/newpipe/extractor/Extractor.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@
55
import org.schabi.newpipe.extractor.exceptions.ParsingException;
66
import org.schabi.newpipe.extractor.linkhandler.LinkHandler;
77
import org.schabi.newpipe.extractor.localization.ContentCountry;
8-
import org.schabi.newpipe.extractor.localization.Localization;
98
import org.schabi.newpipe.extractor.localization.TimeAgoParser;
109

1110
import javax.annotation.Nonnull;
1211
import javax.annotation.Nullable;
13-
1412
import java.io.IOException;
13+
import java.util.Locale;
1514
import java.util.Objects;
1615

1716
public abstract class Extractor {
@@ -24,7 +23,7 @@ public abstract class Extractor {
2423
private final LinkHandler linkHandler;
2524

2625
@Nullable
27-
private Localization forcedLocalization = null;
26+
private Locale forcedLocale = null;
2827
@Nullable
2928
private ContentCountry forcedContentCountry = null;
3029

@@ -128,17 +127,17 @@ public Downloader getDownloader() {
128127
// Localization
129128
//////////////////////////////////////////////////////////////////////////*/
130129

131-
public void forceLocalization(final Localization localization) {
132-
this.forcedLocalization = localization;
130+
public void forceLocale(final Locale locale) {
131+
this.forcedLocale = locale;
133132
}
134133

135134
public void forceContentCountry(final ContentCountry contentCountry) {
136135
this.forcedContentCountry = contentCountry;
137136
}
138137

139138
@Nonnull
140-
public Localization getExtractorLocalization() {
141-
return forcedLocalization == null ? getService().getLocalization() : forcedLocalization;
139+
public Locale getExtractorLocale() {
140+
return forcedLocale == null ? getService().getLocale() : forcedLocale;
142141
}
143142

144143
@Nonnull
@@ -149,6 +148,6 @@ public ContentCountry getExtractorContentCountry() {
149148

150149
@Nonnull
151150
public TimeAgoParser getTimeAgoParser() {
152-
return getService().getTimeAgoParser(getExtractorLocalization());
151+
return getService().getTimeAgoParser(getExtractorLocale());
153152
}
154153
}

extractor/src/main/java/org/schabi/newpipe/extractor/NewPipe.java

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,36 +23,34 @@
2323
import org.schabi.newpipe.extractor.downloader.Downloader;
2424
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
2525
import org.schabi.newpipe.extractor.localization.ContentCountry;
26-
import org.schabi.newpipe.extractor.localization.Localization;
27-
28-
import java.util.List;
2926

3027
import javax.annotation.Nonnull;
3128
import javax.annotation.Nullable;
29+
import java.util.List;
30+
import java.util.Locale;
3231

3332
/**
3433
* Provides access to streaming services supported by NewPipe.
3534
*/
3635
public final class NewPipe {
3736
private static Downloader downloader;
38-
private static Localization preferredLocalization;
37+
private static Locale preferredLocale;
3938
private static ContentCountry preferredContentCountry;
4039

4140
private NewPipe() {
4241
}
4342

4443
public static void init(final Downloader d) {
45-
init(d, Localization.DEFAULT);
44+
init(d, Locale.UK);
4645
}
4746

48-
public static void init(final Downloader d, final Localization l) {
49-
init(d, l, l.getCountryCode().isEmpty()
50-
? ContentCountry.DEFAULT : new ContentCountry(l.getCountryCode()));
47+
public static void init(final Downloader d, final Locale l) {
48+
init(d, l, ContentCountry.fromLocale(l));
5149
}
5250

53-
public static void init(final Downloader d, final Localization l, final ContentCountry c) {
51+
public static void init(final Downloader d, final Locale l, final ContentCountry c) {
5452
downloader = d;
55-
preferredLocalization = l;
53+
preferredLocale = l;
5654
preferredContentCountry = c;
5755
}
5856

@@ -97,39 +95,31 @@ public static StreamingService getServiceByUrl(final String url) throws Extracti
9795
// Localization
9896
//////////////////////////////////////////////////////////////////////////*/
9997

100-
public static void setupLocalization(final Localization thePreferredLocalization) {
101-
setupLocalization(thePreferredLocalization, null);
98+
public static void setupLocalization(final Locale locale) {
99+
setupLocalization(locale, null);
102100
}
103101

104-
public static void setupLocalization(
105-
final Localization thePreferredLocalization,
106-
@Nullable final ContentCountry thePreferredContentCountry) {
107-
NewPipe.preferredLocalization = thePreferredLocalization;
108-
109-
if (thePreferredContentCountry != null) {
110-
NewPipe.preferredContentCountry = thePreferredContentCountry;
111-
} else {
112-
NewPipe.preferredContentCountry = thePreferredLocalization.getCountryCode().isEmpty()
113-
? ContentCountry.DEFAULT
114-
: new ContentCountry(thePreferredLocalization.getCountryCode());
115-
}
102+
public static void setupLocalization(final Locale locale,
103+
@Nullable final ContentCountry country) {
104+
preferredLocale = locale;
105+
preferredContentCountry = country != null ? country : ContentCountry.fromLocale(locale);
116106
}
117107

118108
@Nonnull
119-
public static Localization getPreferredLocalization() {
120-
return preferredLocalization == null ? Localization.DEFAULT : preferredLocalization;
109+
public static Locale getPreferredLocale() {
110+
return preferredLocale == null ? Locale.UK : preferredLocale;
121111
}
122112

123-
public static void setPreferredLocalization(final Localization preferredLocalization) {
124-
NewPipe.preferredLocalization = preferredLocalization;
113+
public static void setPreferredLocale(final Locale locale) {
114+
preferredLocale = locale;
125115
}
126116

127117
@Nonnull
128118
public static ContentCountry getPreferredContentCountry() {
129119
return preferredContentCountry == null ? ContentCountry.DEFAULT : preferredContentCountry;
130120
}
131121

132-
public static void setPreferredContentCountry(final ContentCountry preferredContentCountry) {
133-
NewPipe.preferredContentCountry = preferredContentCountry;
122+
public static void setPreferredContentCountry(final ContentCountry contentCountry) {
123+
preferredContentCountry = contentCountry;
134124
}
135125
}

extractor/src/main/java/org/schabi/newpipe/extractor/StreamingService.java

Lines changed: 23 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import org.schabi.newpipe.extractor.linkhandler.SearchQueryHandler;
1515
import org.schabi.newpipe.extractor.linkhandler.SearchQueryHandlerFactory;
1616
import org.schabi.newpipe.extractor.localization.ContentCountry;
17-
import org.schabi.newpipe.extractor.localization.Localization;
1817
import org.schabi.newpipe.extractor.localization.TimeAgoParser;
1918
import org.schabi.newpipe.extractor.localization.TimeAgoPatternsManager;
2019
import org.schabi.newpipe.extractor.playlist.PlaylistExtractor;
@@ -27,6 +26,7 @@
2726
import javax.annotation.Nullable;
2827
import java.util.Collections;
2928
import java.util.List;
29+
import java.util.Locale;
3030

3131
/*
3232
* Copyright (C) 2018 Christian Schabesberger <chris.schabesberger@mailbox.org>
@@ -344,44 +344,38 @@ public final LinkType getLinkTypeByUrl(final String url) throws ParsingException
344344
/**
345345
* Returns a list of localizations that this service supports.
346346
*/
347-
public List<Localization> getSupportedLocalizations() {
348-
return Collections.singletonList(Localization.DEFAULT);
347+
public List<Locale> getSupportedLocales() {
348+
return List.of(Locale.UK);
349349
}
350350

351351
/**
352352
* Returns a list of countries that this service supports.<br>
353353
*/
354354
public List<ContentCountry> getSupportedCountries() {
355-
return Collections.singletonList(ContentCountry.DEFAULT);
355+
return List.of(ContentCountry.DEFAULT);
356356
}
357357

358358
/**
359-
* Returns the localization that should be used in this service. It will get which localization
360-
* the user prefer (using {@link NewPipe#getPreferredLocalization()}), then it will:
359+
* Returns the localization that should be used in this service. It will get which locale
360+
* the user prefers (using {@link NewPipe#getPreferredLocale()}), then it will:
361361
* <ul>
362-
* <li>Check if the exactly localization is supported by this service.</li>
362+
* <li>Check if the exact locale is supported by this service.</li>
363363
* <li>If not, check if a less specific localization is available, using only the language
364364
* code.</li>
365-
* <li>Fallback to the {@link Localization#DEFAULT default} localization.</li>
365+
* <li>Fallback to the {@link Locale#UK default} locale.</li>
366366
* </ul>
367367
*/
368-
public Localization getLocalization() {
369-
final Localization preferredLocalization = NewPipe.getPreferredLocalization();
370-
371-
// Check the localization's language and country
372-
if (getSupportedLocalizations().contains(preferredLocalization)) {
373-
return preferredLocalization;
374-
}
375-
376-
// Fallback to the first supported language that matches the preferred language
377-
for (final Localization supportedLanguage : getSupportedLocalizations()) {
378-
if (supportedLanguage.getLanguageCode()
379-
.equals(preferredLocalization.getLanguageCode())) {
380-
return supportedLanguage;
381-
}
382-
}
383-
384-
return Localization.DEFAULT;
368+
public Locale getLocale() {
369+
final var preferredLocale = NewPipe.getPreferredLocale();
370+
return getSupportedLocales().stream()
371+
.filter(locale -> {
372+
// Check the localization's language and country
373+
return preferredLocale.equals(locale)
374+
// Fallback to the first supported language that matches the preferred
375+
// language
376+
|| preferredLocale.getLanguage().equals(locale.getLanguage());
377+
})
378+
.findFirst().orElse(Locale.UK);
385379
}
386380

387381
/**
@@ -405,32 +399,18 @@ public ContentCountry getContentCountry() {
405399
/**
406400
* Get an instance of the time ago parser using the patterns related to the passed localization.
407401
* <br><br>
408-
* Just like {@link #getLocalization()}, it will also try to fallback to a less specific
402+
* Just like {@link #getLocale()}, it will also try to fallback to a less specific
409403
* localization if the exact one is not available/supported.
410404
*
411405
* @throws IllegalArgumentException if the localization is not supported (parsing patterns are
412406
* not present).
413407
*/
414-
public TimeAgoParser getTimeAgoParser(final Localization localization) {
415-
final TimeAgoParser targetParser = TimeAgoPatternsManager.getTimeAgoParserFor(localization);
416-
408+
public TimeAgoParser getTimeAgoParser(final Locale locale) {
409+
final var targetParser = TimeAgoPatternsManager.getTimeAgoParserFor(locale);
417410
if (targetParser != null) {
418411
return targetParser;
419412
}
420-
421-
if (!localization.getCountryCode().isEmpty()) {
422-
final Localization lessSpecificLocalization
423-
= new Localization(localization.getLanguageCode());
424-
final TimeAgoParser lessSpecificParser
425-
= TimeAgoPatternsManager.getTimeAgoParserFor(lessSpecificLocalization);
426-
427-
if (lessSpecificParser != null) {
428-
return lessSpecificParser;
429-
}
430-
}
431-
432-
throw new IllegalArgumentException(
433-
"Localization is not supported (\"" + localization + "\")");
413+
throw new IllegalArgumentException("Locale is not supported (\"" + locale + "\")");
434414
}
435415

436416
}

0 commit comments

Comments
 (0)