Skip to content

Commit

Permalink
Add in-app language selection support.
Browse files Browse the repository at this point in the history
  • Loading branch information
moxie0 committed Jun 30, 2013
1 parent 4977092 commit 68b82c1
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 10 deletions.
46 changes: 46 additions & 0 deletions res/values/arrays.xml
Original file line number Diff line number Diff line change
@@ -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>
Expand Down
2 changes: 2 additions & 0 deletions res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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 -->
Expand Down Expand Up @@ -511,6 +512,7 @@

<!-- verify_keys -->
<string name="verify_keys__menu_verified">Verified</string>
<string name="preferences__language">Language</string>
<!-- EOF -->

</resources>
6 changes: 6 additions & 0 deletions res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down
19 changes: 10 additions & 9 deletions src/org/thoughtcrime/securesms/ApplicationPreferencesActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,22 @@
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.
Expand All @@ -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";

Expand All @@ -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);
Expand Down Expand Up @@ -134,6 +132,7 @@ public void onStart() {
public void onResume() {
super.onResume();
dynamicTheme.onResume(this);
dynamicLanguage.onResume(this);
}

@Override
Expand Down Expand Up @@ -233,6 +232,8 @@ private void handleIdentitySelection(Intent data) {
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (key.equals(THEME_PREF)) {
dynamicTheme.onResume(this);
} else if (key.equals(LANGUAGE_PREF)) {
dynamicLanguage.onResume(this);
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/org/thoughtcrime/securesms/ConversationActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
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;
Expand Down Expand Up @@ -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);
Expand All @@ -165,6 +168,7 @@ protected void onStart() {
protected void onResume() {
super.onResume();
dynamicTheme.onResume(this);
dynamicLanguage.onResume(this);

initializeSecurity();
initializeTitleBar();
Expand Down
6 changes: 5 additions & 1 deletion src/org/thoughtcrime/securesms/ConversationListActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
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;

Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -64,6 +67,7 @@ public void onCreate(Bundle icicle) {
public void onResume() {
super.onResume();
dynamicTheme.onResume(this);
dynamicLanguage.onResume(this);
}

@Override
Expand Down
70 changes: 70 additions & 0 deletions src/org/thoughtcrime/securesms/util/DynamicLanguage.java
Original file line number Diff line number Diff line change
@@ -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);
}
}

}

0 comments on commit 68b82c1

Please sign in to comment.