mirror of https://github.com/tasks/tasks
Add Locale wrapper
parent
b203cbd9ce
commit
44e894f97b
@ -0,0 +1,135 @@
|
||||
package org.tasks.locale;
|
||||
|
||||
import android.app.Dialog;
|
||||
import android.content.Context;
|
||||
import android.content.res.Configuration;
|
||||
import android.text.TextUtils;
|
||||
import android.view.ContextThemeWrapper;
|
||||
import android.view.View;
|
||||
import android.view.ViewParent;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
|
||||
import static com.todoroo.andlib.utility.AndroidUtilities.atLeastJellybeanMR1;
|
||||
|
||||
public class Locale {
|
||||
|
||||
public static Locale INSTANCE;
|
||||
|
||||
private static final int[] sDialogButtons = new int[] { android.R.id.button1, android.R.id.button2, android.R.id.button3 };
|
||||
private static final char LEFT_TO_RIGHT_MARK = '\u200e';
|
||||
private static final char RIGHT_TO_LEFT_MARK = '\u200f';
|
||||
|
||||
public static java.util.Locale localeFromString(String locale) {
|
||||
if (Strings.isNullOrEmpty(locale)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String[] split = locale.split("-");
|
||||
if (split.length == 1) {
|
||||
return new java.util.Locale(split[0]);
|
||||
} else if (split.length == 2) {
|
||||
return new java.util.Locale(split[0], split[1]);
|
||||
}
|
||||
throw new RuntimeException();
|
||||
}
|
||||
|
||||
private final java.util.Locale deviceLocale;
|
||||
private final java.util.Locale appLocale;
|
||||
private final int deviceDirectionality;
|
||||
private final int appDirectionality;
|
||||
private final String override;
|
||||
|
||||
public Locale(java.util.Locale deviceLocale, String override) {
|
||||
this.deviceLocale = deviceLocale;
|
||||
this.appLocale = localeFromString(override);
|
||||
this.override = override;
|
||||
|
||||
deviceDirectionality = TextUtils.getLayoutDirectionFromLocale(deviceLocale);
|
||||
|
||||
if (appLocale != null) {
|
||||
java.util.Locale.setDefault(appLocale);
|
||||
appDirectionality = TextUtils.getLayoutDirectionFromLocale(appLocale);
|
||||
} else {
|
||||
appDirectionality = deviceDirectionality;
|
||||
}
|
||||
}
|
||||
|
||||
public java.util.Locale getLocale() {
|
||||
return appLocale == null ? deviceLocale : appLocale;
|
||||
}
|
||||
|
||||
public char getDeviceDirectionalityMark() {
|
||||
return getDirectionalityMark(deviceDirectionality);
|
||||
}
|
||||
|
||||
public char getDirectionalityMark() {
|
||||
return getDirectionalityMark(appDirectionality);
|
||||
}
|
||||
|
||||
private char getDirectionalityMark(int directionality) {
|
||||
return directionality == View.LAYOUT_DIRECTION_RTL ? RIGHT_TO_LEFT_MARK : LEFT_TO_RIGHT_MARK;
|
||||
}
|
||||
|
||||
public String getOverride() {
|
||||
return override;
|
||||
}
|
||||
|
||||
public Context createConfigurationContext(Context context) {
|
||||
return appLocale == null ? context : context.createConfigurationContext(getLocaleConfiguration());
|
||||
}
|
||||
|
||||
private Configuration getLocaleConfiguration() {
|
||||
Configuration configuration = new Configuration();
|
||||
configuration.setLocale(appLocale);
|
||||
return configuration;
|
||||
}
|
||||
|
||||
public void fixDialogButtonDirectionality(Dialog dialog) {
|
||||
if (appDirectionality != deviceDirectionality) {
|
||||
for (int id : sDialogButtons) {
|
||||
ViewParent parent = dialog.findViewById(id).getParent();
|
||||
((View) parent).setLayoutDirection(appDirectionality);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void applyOverrideConfiguration(ContextThemeWrapper wrapper) {
|
||||
if (appLocale != null && atLeastJellybeanMR1()) {
|
||||
wrapper.applyOverrideConfiguration(getLocaleConfiguration());
|
||||
}
|
||||
}
|
||||
|
||||
public Locale withOverride(String language) {
|
||||
return new Locale(deviceLocale, language);
|
||||
}
|
||||
|
||||
public String getDisplayName() {
|
||||
java.util.Locale locale = getLocale();
|
||||
return locale.getDisplayName(locale);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
Locale locale = (Locale) o;
|
||||
|
||||
return override != null ? override.equals(locale.override) : locale.override == null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return override != null ? override.hashCode() : 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Locale{" +
|
||||
"deviceLocale=" + deviceLocale +
|
||||
", appLocale=" + appLocale +
|
||||
", override='" + override + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@ -1,89 +0,0 @@
|
||||
package org.tasks.locale;
|
||||
|
||||
import android.app.Dialog;
|
||||
import android.content.Context;
|
||||
import android.content.res.Configuration;
|
||||
import android.text.TextUtils;
|
||||
import android.view.ContextThemeWrapper;
|
||||
import android.view.View;
|
||||
import android.view.ViewParent;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
import static com.todoroo.andlib.utility.AndroidUtilities.atLeastJellybeanMR1;
|
||||
|
||||
public class LocaleUtils {
|
||||
public static Locale localeFromString(String locale) {
|
||||
if (Strings.isNullOrEmpty(locale)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String[] split = locale.split("-");
|
||||
if (split.length == 1) {
|
||||
return new Locale(split[0]);
|
||||
} else if (split.length == 2) {
|
||||
return new Locale(split[0], split[1]);
|
||||
}
|
||||
throw new RuntimeException();
|
||||
}
|
||||
|
||||
private static final int[] sDialogButtons = new int[] { android.R.id.button1, android.R.id.button2, android.R.id.button3 };
|
||||
private static final char LEFT_TO_RIGHT_MARK = '\u200e';
|
||||
private static final char RIGHT_TO_LEFT_MARK = '\u200f';
|
||||
private static String sLocaleString;
|
||||
private static Locale sLocale;
|
||||
|
||||
public static Locale getLocale() {
|
||||
return sLocale == null ? Locale.getDefault() : sLocale;
|
||||
}
|
||||
|
||||
public static char getDirectionalityMark() {
|
||||
return TextUtils.getLayoutDirectionFromLocale(getLocale()) == View.LAYOUT_DIRECTION_LTR
|
||||
? LEFT_TO_RIGHT_MARK
|
||||
: RIGHT_TO_LEFT_MARK;
|
||||
}
|
||||
|
||||
public static String getsLocaleString() {
|
||||
return sLocaleString;
|
||||
}
|
||||
|
||||
public static void setLocale(String locale) {
|
||||
sLocaleString = locale;
|
||||
sLocale = localeFromString(locale);
|
||||
if (sLocale != null) {
|
||||
Locale.setDefault(sLocale);
|
||||
}
|
||||
}
|
||||
|
||||
public static void fixDialogButtons(Dialog dialog) {
|
||||
if (sLocale != null) {
|
||||
int layoutDirectionFromLocale = TextUtils.getLayoutDirectionFromLocale(sLocale);
|
||||
for (int id : sDialogButtons) {
|
||||
ViewParent parent = dialog.findViewById(id).getParent();
|
||||
setDirection((View) parent, layoutDirectionFromLocale);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void setDirection(View view, int direction) {
|
||||
view.setLayoutDirection(direction);
|
||||
}
|
||||
|
||||
public static void applyOverrideConfiguration(ContextThemeWrapper wrapper) {
|
||||
if (sLocale != null && atLeastJellybeanMR1()) {
|
||||
wrapper.applyOverrideConfiguration(getLocaleConfiguration());
|
||||
}
|
||||
}
|
||||
|
||||
public static Context createConfigurationContext(Context context) {
|
||||
return sLocale == null ? context : context.createConfigurationContext(getLocaleConfiguration());
|
||||
}
|
||||
|
||||
private static Configuration getLocaleConfiguration() {
|
||||
Configuration configuration = new Configuration();
|
||||
configuration.setLocale(sLocale);
|
||||
return configuration;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue