Add in-app language selection support.

This commit is contained in:
Moxie Marlinspike 2013-06-29 18:03:55 -07:00
parent 4977092f7a
commit 68b82c168e
7 changed files with 143 additions and 10 deletions

View File

@ -1,6 +1,52 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="language_entries">
<item>@string/preferences__default</item>
<item>English</item>
<item>Arabic العربية</item>
<item>Burmese မြန်မာစာ</item>
<item>Chinese 中国的</item>
<item>Deutsch</item>
<item>Español</item>
<item>Français</item>
<item>Italiano</item>
<item>Japanese 日本人</item>
<item>Magyar</item>
<item>Nederlands</item>
<item>Norsk</item>
<item>Português</item>
<item>Português (Brasil)</item>
<item>Russian Pусский</item>
<item>Slovenščina</item>
<item>Slovenský</item>
<item>Svenska</item>
<item>Tibetan བོད་སྐད།</item>
</string-array>
<string-array name="language_values">
<item>zz</item>
<item>en</item>
<item>ar</item>
<item>my</item>
<item>zh_CN</item>
<item>de</item>
<item>es</item>
<item>fr</item>
<item>it</item>
<item>ja</item>
<item>he</item>
<item>nl</item>
<item>no</item>
<item>pt</item>
<item>pt_BR</item>
<item>ru</item>
<item>sl</item>
<item>sk</item>
<item>sv</item>
<item>bo</item>
</string-array>
<string-array name="minutes_hours">
<item>@string/preferences__minutes</item>
<item>@string/preferences__hours</item>

View File

@ -445,6 +445,7 @@
<string name="preferences__dark_theme">Dark Theme</string>
<string name="preferences__appearance">Appearance</string>
<string name="preferences__theme">Theme</string>
<string name="preferences__default">Default</string>
<!-- **************************************** -->
<!-- menus -->
@ -511,6 +512,7 @@
<!-- verify_keys -->
<string name="verify_keys__menu_verified">Verified</string>
<string name="preferences__language">Language</string>
<!-- EOF -->
</resources>

View File

@ -78,6 +78,12 @@
android:entryValues="@array/pref_theme_values"
android:defaultValue="light">
</ListPreference>
<ListPreference android:key="pref_language"
android:title="@string/preferences__language"
android:entries="@array/language_entries"
android:entryValues="@array/language_values"
android:defaultValue="zz"/>
</PreferenceCategory>
<PreferenceCategory android:title="@string/preferences__storage">

View File

@ -25,27 +25,22 @@ import android.net.Uri;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.PreferenceScreen;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;
import android.provider.ContactsContract;
import android.util.Log;
import android.widget.Toast;
import com.actionbarsherlock.view.MenuItem;
import org.thoughtcrime.securesms.contacts.ContactAccessor;
import org.thoughtcrime.securesms.contacts.ContactIdentityManager;
import org.thoughtcrime.securesms.crypto.IdentityKey;
import org.thoughtcrime.securesms.crypto.IdentityKeyUtil;
import org.thoughtcrime.securesms.crypto.MasterSecret;
import org.thoughtcrime.securesms.crypto.MasterSecretUtil;
import org.thoughtcrime.securesms.util.Dialogs;
import org.thoughtcrime.securesms.util.DynamicLanguage;
import org.thoughtcrime.securesms.util.DynamicTheme;
import org.thoughtcrime.securesms.util.MemoryCleaner;
import org.thoughtcrime.securesms.util.Trimmer;
import org.thoughtcrime.securesms.util.Util;
import com.actionbarsherlock.view.MenuItem;
import java.util.List;
/**
* The Activity for application preference display and management.
@ -72,6 +67,7 @@ public class ApplicationPreferencesActivity extends PassphraseRequiredSherlockPr
public static final String PASSPHRASE_TIMEOUT_PREF = "pref_timeout_passphrase";
public static final String AUTO_KEY_EXCHANGE_PREF = "pref_auto_complete_key_exchange";
public static final String THEME_PREF = "pref_theme";
public static final String LANGUAGE_PREF = "pref_language";
public static final String ENTER_SENDS_PREF = "pref_enter_sends";
public static final String ENTER_PRESENT_PREF = "pref_enter_key";
@ -98,11 +94,13 @@ public class ApplicationPreferencesActivity extends PassphraseRequiredSherlockPr
public static final String REGISTERED_GCM_PREF = "pref_gcm_registered";
public static final String GCM_PASSWORD_PREF = "pref_gcm_password";
private final DynamicTheme dynamicTheme = new DynamicTheme();
private final DynamicTheme dynamicTheme = new DynamicTheme();
private final DynamicLanguage dynamicLanguage = new DynamicLanguage();
@Override
protected void onCreate(Bundle icicle) {
dynamicTheme.onCreate(this);
dynamicLanguage.onCreate(this);
super.onCreate(icicle);
this.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
@ -134,6 +132,7 @@ public class ApplicationPreferencesActivity extends PassphraseRequiredSherlockPr
public void onResume() {
super.onResume();
dynamicTheme.onResume(this);
dynamicLanguage.onResume(this);
}
@Override
@ -233,6 +232,8 @@ public class ApplicationPreferencesActivity extends PassphraseRequiredSherlockPr
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (key.equals(THEME_PREF)) {
dynamicTheme.onResume(this);
} else if (key.equals(LANGUAGE_PREF)) {
dynamicLanguage.onResume(this);
}
}

View File

@ -80,6 +80,7 @@ import org.thoughtcrime.securesms.sms.OutgoingEncryptedMessage;
import org.thoughtcrime.securesms.sms.OutgoingTextMessage;
import org.thoughtcrime.securesms.util.BitmapDecodingException;
import org.thoughtcrime.securesms.util.CharacterCalculator;
import org.thoughtcrime.securesms.util.DynamicLanguage;
import org.thoughtcrime.securesms.util.DynamicTheme;
import org.thoughtcrime.securesms.util.EncryptedCharacterCalculator;
import org.thoughtcrime.securesms.util.InvalidMessageException;
@ -138,10 +139,12 @@ public class ConversationActivity extends PassphraseRequiredSherlockFragmentActi
private CharacterCalculator characterCalculator = new CharacterCalculator();
private DynamicTheme dynamicTheme = new DynamicTheme();
private DynamicLanguage dynamicLanguage = new DynamicLanguage();
@Override
protected void onCreate(Bundle state) {
dynamicTheme.onCreate(this);
dynamicLanguage.onCreate(this);
super.onCreate(state);
setContentView(R.layout.conversation_activity);
@ -165,6 +168,7 @@ public class ConversationActivity extends PassphraseRequiredSherlockFragmentActi
protected void onResume() {
super.onResume();
dynamicTheme.onResume(this);
dynamicLanguage.onResume(this);
initializeSecurity();
initializeTitleBar();

View File

@ -28,6 +28,7 @@ import org.thoughtcrime.securesms.recipients.RecipientFactory;
import org.thoughtcrime.securesms.recipients.Recipients;
import org.thoughtcrime.securesms.service.KeyCachingService;
import org.thoughtcrime.securesms.service.SendReceiveService;
import org.thoughtcrime.securesms.util.DynamicLanguage;
import org.thoughtcrime.securesms.util.DynamicTheme;
import org.thoughtcrime.securesms.util.MemoryCleaner;
@ -39,7 +40,8 @@ public class ConversationListActivity extends PassphraseRequiredSherlockFragment
implements ConversationListFragment.ConversationSelectedListener,
ListView.OnItemClickListener
{
private final DynamicTheme dynamicTheme = new DynamicTheme();
private final DynamicTheme dynamicTheme = new DynamicTheme ();
private final DynamicLanguage dynamicLanguage = new DynamicLanguage();
private ConversationListFragment fragment;
private MasterSecret masterSecret;
@ -49,6 +51,7 @@ public class ConversationListActivity extends PassphraseRequiredSherlockFragment
@Override
public void onCreate(Bundle icicle) {
dynamicTheme.onCreate(this);
dynamicLanguage.onCreate(this);
super.onCreate(icicle);
setContentView(R.layout.conversation_list_activity);
@ -64,6 +67,7 @@ public class ConversationListActivity extends PassphraseRequiredSherlockFragment
public void onResume() {
super.onResume();
dynamicTheme.onResume(this);
dynamicLanguage.onResume(this);
}
@Override

View File

@ -0,0 +1,70 @@
package org.thoughtcrime.securesms.util;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Build;
import android.preference.PreferenceManager;
import android.util.Log;
import org.thoughtcrime.securesms.ApplicationPreferencesActivity;
import java.util.Locale;
public class DynamicLanguage {
private static final String DEFAULT = "zz";
private Locale currentLocale;
public void onCreate(Activity activity) {
currentLocale = getSelectedLocale(activity);
setActivityLocale(activity, currentLocale);
}
public void onResume(Activity activity) {
if (!currentLocale.getLanguage().equalsIgnoreCase(getSelectedLocale(activity).getLanguage())) {
Intent intent = activity.getIntent();
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
activity.startActivity(intent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR) {
OverridePendingTransition.invoke(activity);
}
activity.finish();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR) {
OverridePendingTransition.invoke(activity);
}
}
}
private static void setActivityLocale(Activity activity, Locale selectedLocale) {
Configuration configuration = activity.getResources().getConfiguration();
if (!configuration.locale.getLanguage().equalsIgnoreCase(selectedLocale.getLanguage())) {
configuration.locale = selectedLocale;
activity.getResources().updateConfiguration(configuration,
activity.getResources().getDisplayMetrics());
}
}
private static Locale getActivityLocale(Activity activity) {
return activity.getResources().getConfiguration().locale;
}
private static Locale getSelectedLocale(Activity activity) {
String language = PreferenceManager.getDefaultSharedPreferences(activity)
.getString(ApplicationPreferencesActivity.LANGUAGE_PREF, DEFAULT);
if (language.equals(DEFAULT)) return Locale.getDefault();
else return new Locale(language);
}
private static final class OverridePendingTransition {
static void invoke(Activity activity) {
activity.overridePendingTransition(0, 0);
}
}
}