mirror of https://github.com/tasks/tasks
Add language preference
parent
c7e3f4148d
commit
d18f35965e
@ -0,0 +1,81 @@
|
|||||||
|
package org.tasks.locale;
|
||||||
|
|
||||||
|
import android.app.Activity;
|
||||||
|
import android.app.Dialog;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.DialogInterface;
|
||||||
|
import android.os.Bundle;
|
||||||
|
|
||||||
|
import com.google.common.base.Function;
|
||||||
|
import com.google.common.collect.Lists;
|
||||||
|
|
||||||
|
import org.tasks.R;
|
||||||
|
import org.tasks.dialogs.DialogBuilder;
|
||||||
|
import org.tasks.injection.DialogFragmentComponent;
|
||||||
|
import org.tasks.injection.ForApplication;
|
||||||
|
import org.tasks.injection.InjectingDialogFragment;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import javax.inject.Inject;
|
||||||
|
|
||||||
|
import static com.google.common.collect.Iterables.toArray;
|
||||||
|
import static org.tasks.locale.LocaleUtils.localeFromString;
|
||||||
|
|
||||||
|
|
||||||
|
public class LocalePickerDialog extends InjectingDialogFragment {
|
||||||
|
|
||||||
|
public static LocalePickerDialog newLocalePickerDialog() {
|
||||||
|
return new LocalePickerDialog();
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface LocaleSelectionHandler {
|
||||||
|
void onLocaleSelected(String locale);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Inject DialogBuilder dialogBuilder;
|
||||||
|
@Inject @ForApplication Context context;
|
||||||
|
|
||||||
|
private LocaleSelectionHandler callback;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
||||||
|
final String[] translations = context.getResources().getStringArray(R.array.localization);
|
||||||
|
final List<String> display = Lists.transform(Arrays.asList(translations), new Function<String, String>() {
|
||||||
|
@Override
|
||||||
|
public String apply(String locale) {
|
||||||
|
return localeFromString(locale).getDisplayName();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return dialogBuilder.newDialog()
|
||||||
|
.setItems(toArray(display, String.class), new DialogInterface.OnClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onClick(DialogInterface dialogInterface, int i) {
|
||||||
|
dialogInterface.dismiss();
|
||||||
|
callback.onLocaleSelected(translations[i]);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.setNegativeButton(android.R.string.cancel, null)
|
||||||
|
.setNeutralButton(R.string.default_value, new DialogInterface.OnClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onClick(DialogInterface dialogInterface, int i) {
|
||||||
|
dialogInterface.dismiss();
|
||||||
|
callback.onLocaleSelected(null);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.show();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onAttach(Activity activity) {
|
||||||
|
super.onAttach(activity);
|
||||||
|
|
||||||
|
callback = (LocaleSelectionHandler) activity;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void inject(DialogFragmentComponent component) {
|
||||||
|
component.inject(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,62 @@
|
|||||||
|
package org.tasks.locale;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.res.Configuration;
|
||||||
|
import android.view.ContextThemeWrapper;
|
||||||
|
|
||||||
|
import com.google.api.client.repackaged.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 String sLocaleString;
|
||||||
|
private static Locale sLocale;
|
||||||
|
|
||||||
|
public static Locale getLocale() {
|
||||||
|
return sLocale == null ? Locale.getDefault() : sLocale;
|
||||||
|
}
|
||||||
|
|
||||||
|
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 updateConfig(ContextThemeWrapper wrapper) {
|
||||||
|
if (sLocale != null && atLeastJellybeanMR1()) {
|
||||||
|
wrapper.applyOverrideConfiguration(getLocaleConfiguration());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Context withLocale(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