-
Notifications
You must be signed in to change notification settings - Fork 425
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #910 from Isira-Seneviratne/Locale_forLanguageTag
Add compat Locale.forLanguageTag() implementation.
- Loading branch information
Showing
3 changed files
with
50 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
extractor/src/main/java/org/schabi/newpipe/extractor/utils/LocaleCompat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 + "]"); | ||
} | ||
} |