-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
moved LanguageUtils to Utils. + split getDeviceLanguage into Code (fo…
…r string) and Locale for reuse as suggested in code review by @maxme
- Loading branch information
Showing
1 changed file
with
52 additions
and
0 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
WordPressUtils/src/main/java/org/wordpress/android/util/LanguageUtils.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,52 @@ | ||
package org.wordpress.android.util; | ||
|
||
import android.content.Context; | ||
|
||
import java.util.Locale; | ||
|
||
/** | ||
* Methods for dealing with i18n messages | ||
*/ | ||
public class LanguageUtils { | ||
|
||
public static Locale getCurrentDeviceLanguage(Context context) { | ||
//better use getConfiguration as it has the latest locale configuration change. | ||
//Otherwise Locale.getDefault().getLanguage() gets | ||
//the config upon application launch. | ||
Locale deviceLocale = context != null ? context.getResources().getConfiguration().locale : Locale.getDefault(); | ||
return deviceLocale; | ||
} | ||
|
||
public static String getCurrentDeviceLanguageCode(Context context) { | ||
String deviceLanguageCode = getCurrentDeviceLanguage(context).toString(); | ||
return deviceLanguageCode; | ||
} | ||
|
||
public static String getPatchedCurrentDeviceLanguage(Context context) { | ||
return patchDeviceLanguageCode(getCurrentDeviceLanguageCode(context)); | ||
} | ||
|
||
/** | ||
* Patches a deviceLanguageCode if any of deprecated values iw, id, or yi | ||
*/ | ||
public static String patchDeviceLanguageCode(String deviceLanguageCode){ | ||
String patchedCode = deviceLanguageCode; | ||
/* | ||
<p>Note that Java uses several deprecated two-letter codes. The Hebrew ("he") language | ||
* code is rewritten as "iw", Indonesian ("id") as "in", and Yiddish ("yi") as "ji". This | ||
* rewriting happens even if you construct your own {@code Locale} object, not just for | ||
* instances returned by the various lookup methods. | ||
*/ | ||
if (deviceLanguageCode != null) { | ||
if (deviceLanguageCode.startsWith("iw")) | ||
patchedCode = deviceLanguageCode.replace("iw", "he"); | ||
else if (deviceLanguageCode.startsWith("in")) | ||
patchedCode = deviceLanguageCode.replace("in", "id"); | ||
else if (deviceLanguageCode.startsWith("ji")) | ||
patchedCode = deviceLanguageCode.replace("ji", "yi"); | ||
} | ||
|
||
return patchedCode; | ||
} | ||
|
||
} |