diff --git a/WordPress/src/main/java/org/wordpress/android/WordPress.java b/WordPress/src/main/java/org/wordpress/android/WordPress.java index 67a96c616008..b1d82706d4de 100644 --- a/WordPress/src/main/java/org/wordpress/android/WordPress.java +++ b/WordPress/src/main/java/org/wordpress/android/WordPress.java @@ -77,12 +77,12 @@ import org.wordpress.android.util.DateTimeUtils; import org.wordpress.android.util.FluxCUtils; import org.wordpress.android.util.HelpshiftHelper; +import org.wordpress.android.util.LocaleManager; import org.wordpress.android.util.NetworkUtils; import org.wordpress.android.util.PackageUtils; import org.wordpress.android.util.ProfilingUtils; import org.wordpress.android.util.RateLimitedTask; import org.wordpress.android.util.VolleyUtils; -import org.wordpress.android.util.WPActivityUtils; import org.wordpress.passcodelock.AbstractAppLock; import org.wordpress.passcodelock.AppLockManager; @@ -199,6 +199,11 @@ public static BitmapLruCache getBitmapCache() { return mBitmapCache; } + @Override + protected void attachBaseContext(Context base) { + super.attachBaseContext(LocaleManager.setLocale(base)); + } + @Override public void onCreate() { super.onCreate(); @@ -272,9 +277,6 @@ public void onLog(T tag, LogLevel logLevel, String message) { initAnalytics(SystemClock.elapsedRealtime() - startDate); - // If users uses a custom locale set it on start of application - WPActivityUtils.applyLocale(getContext()); - disableRtlLayoutDirectionOnSdk17(); // Allows vector drawable from resources (in selectors for instance) on Android < 21 (can cause issues @@ -676,7 +678,7 @@ private class ApplicationLifecycleMonitor implements Application.ActivityLifecyc @Override public void onConfigurationChanged(final Configuration newConfig) { // Reapply locale on configuration change - WPActivityUtils.applyLocale(getContext()); + LocaleManager.setLocale(getContext()); } @Override diff --git a/WordPress/src/main/java/org/wordpress/android/ui/AddQuickPressShortcutActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/AddQuickPressShortcutActivity.java index b1b3d982f69f..6680c0cfa5ed 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/AddQuickPressShortcutActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/AddQuickPressShortcutActivity.java @@ -2,6 +2,7 @@ import android.app.AlertDialog; import android.app.ListActivity; +import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; @@ -32,6 +33,7 @@ import org.wordpress.android.fluxc.store.SiteStore; import org.wordpress.android.fluxc.tools.FluxCImageLoader; import org.wordpress.android.ui.posts.EditPostActivity; +import org.wordpress.android.util.LocaleManager; import org.wordpress.android.util.SiteUtils; import org.wordpress.android.util.ToastUtils; @@ -50,6 +52,11 @@ public class AddQuickPressShortcutActivity extends ListActivity { @Inject SiteStore mSiteStore; @Inject FluxCImageLoader mImageLoader; + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(LocaleManager.setLocale(newBase)); + } + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/WordPress/src/main/java/org/wordpress/android/ui/AppLogViewerActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/AppLogViewerActivity.java index 4da3b53c8fb4..b6eda8edd9d2 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/AppLogViewerActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/AppLogViewerActivity.java @@ -20,6 +20,7 @@ import org.wordpress.android.R; import org.wordpress.android.util.AppLog; import org.wordpress.android.util.AppLog.T; +import org.wordpress.android.util.LocaleManager; import org.wordpress.android.util.ToastUtils; import java.util.ArrayList; @@ -34,6 +35,11 @@ public class AppLogViewerActivity extends AppCompatActivity { private static final int ID_SHARE = 1; private static final int ID_COPY_TO_CLIPBOARD = 2; + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(LocaleManager.setLocale(newBase)); + } + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/WordPress/src/main/java/org/wordpress/android/ui/DeepLinkingIntentReceiverActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/DeepLinkingIntentReceiverActivity.java index 674ebfbcf4bc..3cdc6cec9374 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/DeepLinkingIntentReceiverActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/DeepLinkingIntentReceiverActivity.java @@ -1,5 +1,6 @@ package org.wordpress.android.ui; +import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.Bundle; @@ -14,6 +15,7 @@ import org.wordpress.android.util.AnalyticsUtils; import org.wordpress.android.util.AppLog; import org.wordpress.android.util.AppLog.T; +import org.wordpress.android.util.LocaleManager; import org.wordpress.android.util.ToastUtils; import javax.inject.Inject; @@ -32,6 +34,11 @@ public class DeepLinkingIntentReceiverActivity extends AppCompatActivity { @Inject AccountStore mAccountStore; + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(LocaleManager.setLocale(newBase)); + } + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/WordPress/src/main/java/org/wordpress/android/ui/HelpshiftDeepLinkReceiver.java b/WordPress/src/main/java/org/wordpress/android/ui/HelpshiftDeepLinkReceiver.java index a2c2328a5112..dde7cd567a2a 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/HelpshiftDeepLinkReceiver.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/HelpshiftDeepLinkReceiver.java @@ -1,5 +1,6 @@ package org.wordpress.android.ui; +import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.Bundle; @@ -7,7 +8,14 @@ import com.helpshift.support.Support; +import org.wordpress.android.util.LocaleManager; + public class HelpshiftDeepLinkReceiver extends AppCompatActivity { + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(LocaleManager.setLocale(newBase)); + } + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/WordPress/src/main/java/org/wordpress/android/ui/ShareIntentReceiverActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/ShareIntentReceiverActivity.java index 1bda64fecb13..849a10ed78ac 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/ShareIntentReceiverActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/ShareIntentReceiverActivity.java @@ -1,5 +1,6 @@ package org.wordpress.android.ui; +import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.net.Uri; @@ -24,6 +25,7 @@ import org.wordpress.android.ui.media.MediaBrowserType; import org.wordpress.android.util.AnalyticsUtils; import org.wordpress.android.util.FluxCUtils; +import org.wordpress.android.util.LocaleManager; import org.wordpress.android.util.PermissionUtils; import org.wordpress.android.util.ToastUtils; import org.wordpress.android.util.WPPermissionUtils; @@ -60,6 +62,11 @@ protected void onNewIntent(Intent intent) { refreshContent(); } + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(LocaleManager.setLocale(newBase)); + } + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/WordPress/src/main/java/org/wordpress/android/ui/VisualEditorOptionsReceiver.java b/WordPress/src/main/java/org/wordpress/android/ui/VisualEditorOptionsReceiver.java index b92e1da61b69..22fadabf5063 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/VisualEditorOptionsReceiver.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/VisualEditorOptionsReceiver.java @@ -1,5 +1,6 @@ package org.wordpress.android.ui; +import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.Bundle; @@ -9,9 +10,15 @@ import org.wordpress.android.ui.prefs.AppPrefs; import org.wordpress.android.util.AppLog; import org.wordpress.android.util.AppLog.T; +import org.wordpress.android.util.LocaleManager; import org.wordpress.android.util.ToastUtils; public class VisualEditorOptionsReceiver extends AppCompatActivity { + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(LocaleManager.setLocale(newBase)); + } + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/WordPress/src/main/java/org/wordpress/android/ui/WPLaunchActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/WPLaunchActivity.java index 7fcb4df3e2fa..127114818140 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/WPLaunchActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/WPLaunchActivity.java @@ -1,5 +1,6 @@ package org.wordpress.android.ui; +import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; @@ -7,6 +8,7 @@ import org.wordpress.android.R; import org.wordpress.android.WordPress; import org.wordpress.android.ui.main.WPMainActivity; +import org.wordpress.android.util.LocaleManager; import org.wordpress.android.util.ProfilingUtils; import org.wordpress.android.util.ToastUtils; @@ -17,6 +19,11 @@ public class WPLaunchActivity extends AppCompatActivity { * manifest to have no UI */ + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(LocaleManager.setLocale(newBase)); + } + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/WordPress/src/main/java/org/wordpress/android/ui/WebViewActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/WebViewActivity.java index ebf3f8e61c07..255fc4bfee42 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/WebViewActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/WebViewActivity.java @@ -1,5 +1,6 @@ package org.wordpress.android.ui; +import android.content.Context; import android.os.Bundle; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; @@ -10,6 +11,7 @@ import android.webkit.WebView; import org.wordpress.android.R; +import org.wordpress.android.util.LocaleManager; import java.util.Map; @@ -25,6 +27,11 @@ public abstract class WebViewActivity extends AppCompatActivity { protected WebView mWebView; + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(LocaleManager.setLocale(newBase)); + } + @Override public void onCreate(Bundle savedInstanceState) { supportRequestWindowFeature(Window.FEATURE_PROGRESS); diff --git a/WordPress/src/main/java/org/wordpress/android/ui/accounts/HelpActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/accounts/HelpActivity.java index 1b0ac7ece537..48b00ffb7df3 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/accounts/HelpActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/accounts/HelpActivity.java @@ -1,5 +1,6 @@ package org.wordpress.android.ui.accounts; +import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.ActionBar; @@ -17,6 +18,7 @@ import org.wordpress.android.util.HelpshiftHelper; import org.wordpress.android.util.HelpshiftHelper.MetadataKey; import org.wordpress.android.util.HelpshiftHelper.Tag; +import org.wordpress.android.util.LocaleManager; import org.wordpress.android.widgets.WPTextView; import javax.inject.Inject; @@ -25,6 +27,11 @@ public class HelpActivity extends AppCompatActivity { @Inject AccountStore mAccountStore; @Inject SiteStore mSiteStore; + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(LocaleManager.setLocale(newBase)); + } + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/WordPress/src/main/java/org/wordpress/android/ui/accounts/LoginActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/accounts/LoginActivity.java index 0ff72d953a60..c821eb74d4d7 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/accounts/LoginActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/accounts/LoginActivity.java @@ -1,6 +1,7 @@ package org.wordpress.android.ui.accounts; import android.app.Activity; +import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.Bundle; @@ -51,6 +52,7 @@ import org.wordpress.android.util.HelpshiftHelper; import org.wordpress.android.util.HelpshiftHelper.Tag; import org.wordpress.android.util.LanguageUtils; +import org.wordpress.android.util.LocaleManager; import org.wordpress.android.util.NetworkUtils; import org.wordpress.android.util.SelfSignedSSLUtils; import org.wordpress.android.util.StringUtils; @@ -92,6 +94,11 @@ private enum SmartLockHelperState { @Inject DispatchingAndroidInjector mFragmentInjector; + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(LocaleManager.setLocale(newBase)); + } + @Override protected void onCreate(Bundle savedInstanceState) { ((WordPress) getApplication()).component().inject(this); diff --git a/WordPress/src/main/java/org/wordpress/android/ui/accounts/LoginEpilogueActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/accounts/LoginEpilogueActivity.java index 79608706e514..0c9d721e77c9 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/accounts/LoginEpilogueActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/accounts/LoginEpilogueActivity.java @@ -1,5 +1,6 @@ package org.wordpress.android.ui.accounts; +import android.content.Context; import android.os.Bundle; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.AppCompatActivity; @@ -10,6 +11,7 @@ import org.wordpress.android.ui.ActivityLauncher; import org.wordpress.android.ui.accounts.login.LoginEpilogueFragment; import org.wordpress.android.ui.accounts.login.LoginEpilogueListener; +import org.wordpress.android.util.LocaleManager; import java.util.ArrayList; @@ -22,6 +24,11 @@ public class LoginEpilogueActivity extends AppCompatActivity implements LoginEpi @Inject AccountStore mAccountStore; + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(LocaleManager.setLocale(newBase)); + } + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/WordPress/src/main/java/org/wordpress/android/ui/accounts/NewBlogActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/accounts/NewBlogActivity.java index dfa1cd9dfd4b..4249433112ee 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/accounts/NewBlogActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/accounts/NewBlogActivity.java @@ -1,13 +1,20 @@ package org.wordpress.android.ui.accounts; +import android.content.Context; import android.os.Bundle; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.AppCompatActivity; import org.wordpress.android.R; import org.wordpress.android.ui.accounts.signup.SiteCreationCreatingFragment; +import org.wordpress.android.util.LocaleManager; public class NewBlogActivity extends AppCompatActivity { + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(LocaleManager.setLocale(newBase)); + } + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/WordPress/src/main/java/org/wordpress/android/ui/accounts/SignupEpilogueActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/accounts/SignupEpilogueActivity.java index 33ed3ea21d4a..faade651b630 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/accounts/SignupEpilogueActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/accounts/SignupEpilogueActivity.java @@ -1,5 +1,6 @@ package org.wordpress.android.ui.accounts; +import android.content.Context; import android.os.Bundle; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.AppCompatActivity; @@ -7,6 +8,7 @@ import org.wordpress.android.R; import org.wordpress.android.ui.accounts.signup.SignupEpilogueFragment; import org.wordpress.android.ui.accounts.signup.SignupEpilogueListener; +import org.wordpress.android.util.LocaleManager; public class SignupEpilogueActivity extends AppCompatActivity implements SignupEpilogueListener { public static final String EXTRA_SIGNUP_DISPLAY_NAME = "EXTRA_SIGNUP_DISPLAY_NAME"; @@ -17,6 +19,11 @@ public class SignupEpilogueActivity extends AppCompatActivity implements SignupE public static final String MAGIC_SIGNUP_PARAMETER = "new_user"; public static final String MAGIC_SIGNUP_VALUE = "1"; + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(LocaleManager.setLocale(newBase)); + } + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/WordPress/src/main/java/org/wordpress/android/ui/accounts/SiteCreationActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/accounts/SiteCreationActivity.java index 717d173f925c..40c4068e61ab 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/accounts/SiteCreationActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/accounts/SiteCreationActivity.java @@ -1,5 +1,6 @@ package org.wordpress.android.ui.accounts; +import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.Fragment; @@ -19,6 +20,7 @@ import org.wordpress.android.ui.accounts.signup.SiteCreationThemeLoaderFragment; import org.wordpress.android.ui.main.SitePickerActivity; import org.wordpress.android.util.HelpshiftHelper; +import org.wordpress.android.util.LocaleManager; import org.wordpress.android.util.ToastUtils; import org.wordpress.android.util.UrlUtils; @@ -35,6 +37,11 @@ public class SiteCreationActivity extends AppCompatActivity implements SiteCreat private String mSiteTitle; private String mSiteTagline; + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(LocaleManager.setLocale(newBase)); + } + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/WordPress/src/main/java/org/wordpress/android/ui/comments/CommentsActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/comments/CommentsActivity.java index d991a8bbd172..a9913137735b 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/comments/CommentsActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/comments/CommentsActivity.java @@ -4,6 +4,7 @@ import android.app.Fragment; import android.app.FragmentManager; import android.app.FragmentTransaction; +import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.support.annotation.NonNull; @@ -32,6 +33,7 @@ import org.wordpress.android.ui.prefs.AppPrefs; import org.wordpress.android.ui.reader.ReaderPostDetailFragment; import org.wordpress.android.util.AppLog; +import org.wordpress.android.util.LocaleManager; import org.wordpress.android.util.ToastUtils; import javax.inject.Inject; @@ -53,6 +55,11 @@ public class CommentsActivity extends AppCompatActivity @Inject Dispatcher mDispatcher; @Inject CommentStore mCommentStore; + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(LocaleManager.setLocale(newBase)); + } + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/WordPress/src/main/java/org/wordpress/android/ui/comments/CommentsDetailActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/comments/CommentsDetailActivity.java index 30f7db0b0d99..80bef38de6ff 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/comments/CommentsDetailActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/comments/CommentsDetailActivity.java @@ -1,5 +1,6 @@ package org.wordpress.android.ui.comments; +import android.content.Context; import android.content.Intent; import android.os.AsyncTask; import android.os.Bundle; @@ -25,6 +26,7 @@ import org.wordpress.android.fluxc.store.CommentStore.FetchCommentsPayload; import org.wordpress.android.models.CommentList; import org.wordpress.android.util.AppLog; +import org.wordpress.android.util.LocaleManager; import org.wordpress.android.util.NetworkUtils; import org.wordpress.android.util.ToastUtils; import org.wordpress.android.widgets.WPViewPager; @@ -56,6 +58,11 @@ public class CommentsDetailActivity extends AppCompatActivity private boolean mIsUpdatingComments; private boolean mCanLoadMoreComments = true; + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(LocaleManager.setLocale(newBase)); + } + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/WordPress/src/main/java/org/wordpress/android/ui/comments/EditCommentActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/comments/EditCommentActivity.java index 098bd645ad4b..7cf2d9d73a5d 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/comments/EditCommentActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/comments/EditCommentActivity.java @@ -3,6 +3,7 @@ import android.app.AlertDialog; import android.app.Dialog; import android.app.ProgressDialog; +import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; @@ -36,6 +37,7 @@ import org.wordpress.android.util.AppLog; import org.wordpress.android.util.AppLog.T; import org.wordpress.android.util.EditTextUtils; +import org.wordpress.android.util.LocaleManager; import org.wordpress.android.util.NetworkUtils; import org.wordpress.android.util.ToastUtils; @@ -56,6 +58,11 @@ public class EditCommentActivity extends AppCompatActivity { @Inject SiteStore mSiteStore; @Inject CommentStore mCommentStore; + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(LocaleManager.setLocale(newBase)); + } + @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); diff --git a/WordPress/src/main/java/org/wordpress/android/ui/main/SitePickerActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/main/SitePickerActivity.java index 0b196e5353e7..05fa2d85b430 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/main/SitePickerActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/main/SitePickerActivity.java @@ -54,6 +54,7 @@ import org.wordpress.android.ui.stats.datasets.StatsTable; import org.wordpress.android.util.ActivityUtils; import org.wordpress.android.util.AppLog; +import org.wordpress.android.util.LocaleManager; import org.wordpress.android.util.NetworkUtils; import org.wordpress.android.util.ToastUtils; import org.wordpress.android.util.helpers.Debouncer; @@ -95,6 +96,11 @@ public class SitePickerActivity extends AppCompatActivity @Inject SiteStore mSiteStore; @Inject Dispatcher mDispatcher; + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(LocaleManager.setLocale(newBase)); + } + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/WordPress/src/main/java/org/wordpress/android/ui/main/WPMainActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/main/WPMainActivity.java index 2743b0666542..e7e8ef230f5a 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/main/WPMainActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/main/WPMainActivity.java @@ -2,6 +2,7 @@ import android.animation.ObjectAnimator; import android.app.Fragment; +import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; @@ -66,6 +67,7 @@ import org.wordpress.android.util.AuthenticationDialogUtils; import org.wordpress.android.util.CoreEvents.MainViewPagerScrolled; import org.wordpress.android.util.FluxCUtils; +import org.wordpress.android.util.LocaleManager; import org.wordpress.android.util.NetworkUtils; import org.wordpress.android.util.ProfilingUtils; import org.wordpress.android.util.ToastUtils; @@ -117,6 +119,11 @@ public interface OnActivityBackPressedListener { boolean onActivityBackPressed(); } + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(LocaleManager.setLocale(newBase)); + } + @Override public void onCreate(Bundle savedInstanceState) { ProfilingUtils.split("WPMainActivity.onCreate"); @@ -653,7 +660,7 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) { break; case RequestCodes.APP_SETTINGS: if (resultCode == AppSettingsFragment.LANGUAGE_CHANGED) { - resetFragments(); + appLanguageChanged(); } break; case RequestCodes.NOTE_DETAIL: @@ -669,6 +676,13 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) { } } + private void appLanguageChanged() { + // Recreate this activity (much like a configuration change) + recreate(); + + resetFragments(); + } + private void startWithNewAccount() { startService(new Intent(this, GCMRegistrationIntentService.class)); resetFragments(); diff --git a/WordPress/src/main/java/org/wordpress/android/ui/media/MediaBrowserActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/media/MediaBrowserActivity.java index 4cc4d8c6ddcc..8ea240faaf9f 100755 --- a/WordPress/src/main/java/org/wordpress/android/ui/media/MediaBrowserActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/media/MediaBrowserActivity.java @@ -66,6 +66,7 @@ import org.wordpress.android.util.DisplayUtils; import org.wordpress.android.util.FluxCUtils; import org.wordpress.android.util.ListUtils; +import org.wordpress.android.util.LocaleManager; import org.wordpress.android.util.MediaUtils; import org.wordpress.android.util.NetworkUtils; import org.wordpress.android.util.PermissionUtils; @@ -121,6 +122,11 @@ private enum AddMenuItem { ITEM_CHOOSE_VIDEO } + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(LocaleManager.setLocale(newBase)); + } + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -157,6 +163,7 @@ public void onCreate(Bundle savedInstanceState) { actionBar.setDisplayShowTitleEnabled(true); actionBar.setDisplayHomeAsUpEnabled(true); } + actionBar.setTitle(R.string.wp_media_title); FragmentManager fm = getFragmentManager(); fm.addOnBackStackChangedListener(mOnBackStackChangedListener); diff --git a/WordPress/src/main/java/org/wordpress/android/ui/media/MediaPreviewActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/media/MediaPreviewActivity.java index 04bfef6f71a4..197be8dad86b 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/media/MediaPreviewActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/media/MediaPreviewActivity.java @@ -35,6 +35,7 @@ import org.wordpress.android.fluxc.tools.FluxCImageLoader; import org.wordpress.android.util.AniUtils; import org.wordpress.android.util.AppLog; +import org.wordpress.android.util.LocaleManager; import org.wordpress.android.util.ToastUtils; import org.wordpress.android.widgets.WPViewPagerTransformer; import org.wordpress.android.widgets.WPViewPagerTransformer.TransformType; @@ -119,6 +120,11 @@ private static void startIntent(Context context, Intent intent) { ActivityCompat.startActivity(context, intent, options.toBundle()); } + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(LocaleManager.setLocale(newBase)); + } + @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/WordPress/src/main/java/org/wordpress/android/ui/media/MediaSettingsActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/media/MediaSettingsActivity.java index 18e54c3e44ad..d53e9ed14f12 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/media/MediaSettingsActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/media/MediaSettingsActivity.java @@ -71,6 +71,7 @@ import org.wordpress.android.util.DisplayUtils; import org.wordpress.android.util.EditTextUtils; import org.wordpress.android.util.ImageUtils; +import org.wordpress.android.util.LocaleManager; import org.wordpress.android.util.MediaUtils; import org.wordpress.android.util.NetworkUtils; import org.wordpress.android.util.PermissionUtils; @@ -195,6 +196,11 @@ public static void showForResult(@NonNull Activity activity, ActivityCompat.startActivityForResult(activity, intent, RequestCodes.MEDIA_SETTINGS, options.toBundle()); } + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(LocaleManager.setLocale(newBase)); + } + @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/WordPress/src/main/java/org/wordpress/android/ui/notifications/NotificationsDetailActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/notifications/NotificationsDetailActivity.java index 055876f0c59b..59cc4f0984b0 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/notifications/NotificationsDetailActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/notifications/NotificationsDetailActivity.java @@ -2,6 +2,7 @@ import android.app.Fragment; import android.app.FragmentManager; +import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.os.Parcelable; @@ -44,6 +45,7 @@ import org.wordpress.android.ui.stats.StatsViewAllActivity; import org.wordpress.android.ui.stats.StatsViewType; import org.wordpress.android.util.AppLog; +import org.wordpress.android.util.LocaleManager; import org.wordpress.android.util.StringUtils; import org.wordpress.android.util.ToastUtils; import org.wordpress.android.widgets.WPSwipeSnackbar; @@ -78,6 +80,11 @@ public class NotificationsDetailActivity extends AppCompatActivity implements private NotificationDetailFragmentAdapter mAdapter; private boolean mIsReaderSwipeToNavigateShown; + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(LocaleManager.setLocale(newBase)); + } + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/WordPress/src/main/java/org/wordpress/android/ui/people/PeopleManagementActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/people/PeopleManagementActivity.java index 6890f3895b5b..16ddb9f9749c 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/people/PeopleManagementActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/people/PeopleManagementActivity.java @@ -4,6 +4,7 @@ import android.app.Fragment; import android.app.FragmentManager; import android.app.FragmentTransaction; +import android.content.Context; import android.content.DialogInterface; import android.os.Bundle; import android.support.v7.app.ActionBar; @@ -27,6 +28,7 @@ import org.wordpress.android.ui.people.utils.PeopleUtils; import org.wordpress.android.util.AnalyticsUtils; import org.wordpress.android.util.AppLog; +import org.wordpress.android.util.LocaleManager; import org.wordpress.android.util.NetworkUtils; import org.wordpress.android.util.ToastUtils; @@ -90,6 +92,11 @@ public class PeopleManagementActivity extends AppCompatActivity private SiteModel mSite; + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(LocaleManager.setLocale(newBase)); + } + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/WordPress/src/main/java/org/wordpress/android/ui/photopicker/PhotoPickerActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/photopicker/PhotoPickerActivity.java index 52ffb7ff874c..fd8c5bf584a6 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/photopicker/PhotoPickerActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/photopicker/PhotoPickerActivity.java @@ -4,6 +4,7 @@ import android.app.Fragment; import android.app.FragmentTransaction; import android.app.ProgressDialog; +import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.Bundle; @@ -33,6 +34,7 @@ import org.wordpress.android.util.AppLog; import org.wordpress.android.util.FluxCUtils; import org.wordpress.android.util.ListUtils; +import org.wordpress.android.util.LocaleManager; import org.wordpress.android.util.NetworkUtils; import org.wordpress.android.util.ToastUtils; import org.wordpress.android.util.WPMediaUtils; @@ -83,6 +85,11 @@ public static PhotoPickerMediaSource fromString(String strSource) { } } + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(LocaleManager.setLocale(newBase)); + } + @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/WordPress/src/main/java/org/wordpress/android/ui/plans/PlansActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/plans/PlansActivity.java index aaf7cd99db97..d836dc60e590 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/plans/PlansActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/plans/PlansActivity.java @@ -3,6 +3,7 @@ import android.animation.Animator; import android.animation.AnimatorListenerAdapter; import android.annotation.TargetApi; +import android.content.Context; import android.graphics.Point; import android.os.Build; import android.os.Bundle; @@ -33,6 +34,7 @@ import org.wordpress.android.util.AppLog; import org.wordpress.android.util.AppLog.T; import org.wordpress.android.util.DisplayUtils; +import org.wordpress.android.util.LocaleManager; import org.wordpress.android.util.NetworkUtils; import org.wordpress.android.util.ToastUtils; import org.wordpress.android.util.UrlUtils; @@ -59,6 +61,11 @@ public class PlansActivity extends AppCompatActivity { @Inject AccountStore mAccountStore; @Inject AccountStore mSiteStore; + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(LocaleManager.setLocale(newBase)); + } + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/WordPress/src/main/java/org/wordpress/android/ui/plugins/PluginBrowserActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/plugins/PluginBrowserActivity.java index 6d60f0ebee78..77b64fa0f8aa 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/plugins/PluginBrowserActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/plugins/PluginBrowserActivity.java @@ -38,6 +38,7 @@ import org.wordpress.android.util.ActivityUtils; import org.wordpress.android.util.AnalyticsUtils; import org.wordpress.android.util.AniUtils; +import org.wordpress.android.util.LocaleManager; import org.wordpress.android.util.NetworkUtils; import org.wordpress.android.util.ToastUtils; import org.wordpress.android.viewmodel.PluginBrowserViewModel; @@ -65,6 +66,11 @@ public class PluginBrowserActivity extends AppCompatActivity private MenuItem mSearchMenuItem; private SearchView mSearchView; + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(LocaleManager.setLocale(newBase)); + } + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/WordPress/src/main/java/org/wordpress/android/ui/plugins/PluginDetailActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/plugins/PluginDetailActivity.java index f7de16f18d43..51682a809717 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/plugins/PluginDetailActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/plugins/PluginDetailActivity.java @@ -2,6 +2,7 @@ import android.animation.ObjectAnimator; import android.app.ProgressDialog; +import android.content.Context; import android.content.DialogInterface; import android.graphics.drawable.Drawable; import android.os.Bundle; @@ -56,6 +57,7 @@ import org.wordpress.android.util.DateTimeUtils; import org.wordpress.android.util.DisplayUtils; import org.wordpress.android.util.FormatUtils; +import org.wordpress.android.util.LocaleManager; import org.wordpress.android.util.NetworkUtils; import org.wordpress.android.util.SiteUtils; import org.wordpress.android.util.StringUtils; @@ -135,6 +137,11 @@ public class PluginDetailActivity extends AppCompatActivity { @Inject PluginStore mPluginStore; @Inject Dispatcher mDispatcher; + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(LocaleManager.setLocale(newBase)); + } + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/WordPress/src/main/java/org/wordpress/android/ui/posts/EditPostActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/posts/EditPostActivity.java index b838f6c71d07..403af3df6f0f 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/posts/EditPostActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/posts/EditPostActivity.java @@ -120,6 +120,7 @@ import org.wordpress.android.util.FluxCUtils; import org.wordpress.android.util.ImageUtils; import org.wordpress.android.util.ListUtils; +import org.wordpress.android.util.LocaleManager; import org.wordpress.android.util.MediaUtils; import org.wordpress.android.util.NetworkUtils; import org.wordpress.android.util.PermissionUtils; @@ -263,6 +264,11 @@ public void run() { } }; + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(LocaleManager.setLocale(newBase)); + } + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/WordPress/src/main/java/org/wordpress/android/ui/posts/PostPreviewActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/posts/PostPreviewActivity.java index aa958d4faede..3e2de73b318f 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/posts/PostPreviewActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/posts/PostPreviewActivity.java @@ -3,6 +3,7 @@ import android.app.Fragment; import android.app.FragmentManager; import android.app.FragmentTransaction; +import android.content.Context; import android.os.Bundle; import android.os.Handler; import android.support.v7.app.ActionBar; @@ -36,6 +37,7 @@ import org.wordpress.android.util.AnalyticsUtils; import org.wordpress.android.util.AniUtils; import org.wordpress.android.util.AppLog; +import org.wordpress.android.util.LocaleManager; import org.wordpress.android.util.NetworkUtils; import org.wordpress.android.util.ToastUtils; @@ -54,6 +56,11 @@ public class PostPreviewActivity extends AppCompatActivity { @Inject Dispatcher mDispatcher; @Inject PostStore mPostStore; + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(LocaleManager.setLocale(newBase)); + } + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/WordPress/src/main/java/org/wordpress/android/ui/posts/PostSettingsTagsActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/posts/PostSettingsTagsActivity.java index 4d9f2cd4adb1..2626cbd02680 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/posts/PostSettingsTagsActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/posts/PostSettingsTagsActivity.java @@ -31,6 +31,7 @@ import org.wordpress.android.fluxc.model.TermModel; import org.wordpress.android.fluxc.store.TaxonomyStore; import org.wordpress.android.util.ActivityUtils; +import org.wordpress.android.util.LocaleManager; import org.wordpress.android.util.ToastUtils; import java.util.ArrayList; @@ -50,6 +51,11 @@ public class PostSettingsTagsActivity extends AppCompatActivity implements TextW @Inject Dispatcher mDispatcher; @Inject TaxonomyStore mTaxonomyStore; + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(LocaleManager.setLocale(newBase)); + } + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/WordPress/src/main/java/org/wordpress/android/ui/posts/PostsListActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/posts/PostsListActivity.java index d9d85897c2e3..dd1223f62560 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/posts/PostsListActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/posts/PostsListActivity.java @@ -1,5 +1,6 @@ package org.wordpress.android.ui.posts; +import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.ActionBar; @@ -15,6 +16,7 @@ import org.wordpress.android.fluxc.store.SiteStore; import org.wordpress.android.ui.ActivityId; import org.wordpress.android.ui.RequestCodes; +import org.wordpress.android.util.LocaleManager; import org.wordpress.android.util.ToastUtils; import javax.inject.Inject; @@ -30,6 +32,11 @@ public class PostsListActivity extends AppCompatActivity { @Inject SiteStore mSiteStore; @Inject PostStore mPostStore; + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(LocaleManager.setLocale(newBase)); + } + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/WordPress/src/main/java/org/wordpress/android/ui/posts/SelectCategoriesActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/posts/SelectCategoriesActivity.java index d44e13198fa3..d911f7a11107 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/posts/SelectCategoriesActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/posts/SelectCategoriesActivity.java @@ -1,5 +1,6 @@ package org.wordpress.android.ui.posts; +import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.Fragment; @@ -30,6 +31,7 @@ import org.wordpress.android.fluxc.store.TaxonomyStore.OnTermUploaded; import org.wordpress.android.fluxc.store.TaxonomyStore.RemoteTermPayload; import org.wordpress.android.models.CategoryNode; +import org.wordpress.android.util.LocaleManager; import org.wordpress.android.util.NetworkUtils; import org.wordpress.android.util.ToastUtils; import org.wordpress.android.util.ToastUtils.Duration; @@ -63,6 +65,11 @@ public class SelectCategoriesActivity extends AppCompatActivity { @Inject TaxonomyStore mTaxonomyStore; @Inject Dispatcher mDispatcher; + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(LocaleManager.setLocale(newBase)); + } + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/WordPress/src/main/java/org/wordpress/android/ui/prefs/AboutActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/prefs/AboutActivity.java index 97ac73960566..ee16e5dac193 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/prefs/AboutActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/prefs/AboutActivity.java @@ -1,5 +1,6 @@ package org.wordpress.android.ui.prefs; +import android.content.Context; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.MenuItem; @@ -10,11 +11,17 @@ import org.wordpress.android.R; import org.wordpress.android.WordPress; import org.wordpress.android.ui.ActivityLauncher; +import org.wordpress.android.util.LocaleManager; import org.wordpress.android.widgets.WPTextView; import java.util.Calendar; public class AboutActivity extends AppCompatActivity implements OnClickListener { + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(LocaleManager.setLocale(newBase)); + } + @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); diff --git a/WordPress/src/main/java/org/wordpress/android/ui/prefs/AccountSettingsActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/prefs/AccountSettingsActivity.java index c9f4edc4a118..fd5a85aaad57 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/prefs/AccountSettingsActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/prefs/AccountSettingsActivity.java @@ -1,16 +1,23 @@ package org.wordpress.android.ui.prefs; import android.app.FragmentManager; +import android.content.Context; import android.os.Bundle; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; import android.view.MenuItem; import org.wordpress.android.R; +import org.wordpress.android.util.LocaleManager; public class AccountSettingsActivity extends AppCompatActivity { private static final String KEY_ACCOUNT_SETTINGS_FRAGMENT = "account-settings-fragment"; + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(LocaleManager.setLocale(newBase)); + } + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/WordPress/src/main/java/org/wordpress/android/ui/prefs/AppSettingsActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/prefs/AppSettingsActivity.java index 0a6260b9e811..55ed42595911 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/prefs/AppSettingsActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/prefs/AppSettingsActivity.java @@ -1,6 +1,7 @@ package org.wordpress.android.ui.prefs; import android.app.FragmentManager; +import android.content.Context; import android.os.Bundle; import android.preference.Preference; import android.preference.SwitchPreference; @@ -10,6 +11,7 @@ import org.wordpress.android.R; import org.wordpress.android.push.GCMMessageService; +import org.wordpress.android.util.LocaleManager; import org.wordpress.passcodelock.AppLockManager; import org.wordpress.passcodelock.PasscodePreferenceFragment; @@ -20,6 +22,11 @@ public class AppSettingsActivity extends AppCompatActivity { private AppSettingsFragment mAppSettingsFragment; private PasscodePreferenceFragment mPasscodePreferenceFragment; + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(LocaleManager.setLocale(newBase)); + } + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/WordPress/src/main/java/org/wordpress/android/ui/prefs/AppSettingsFragment.java b/WordPress/src/main/java/org/wordpress/android/ui/prefs/AppSettingsFragment.java index 9d602f5993cd..3f9f2462bbf2 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/prefs/AppSettingsFragment.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/prefs/AppSettingsFragment.java @@ -2,18 +2,13 @@ import android.content.ActivityNotFoundException; import android.content.Intent; -import android.content.SharedPreferences; -import android.content.res.Configuration; -import android.content.res.Resources; import android.net.Uri; -import android.os.Build; import android.os.Bundle; import android.preference.ListPreference; import android.preference.Preference; import android.preference.Preference.OnPreferenceClickListener; import android.preference.PreferenceCategory; import android.preference.PreferenceFragment; -import android.preference.PreferenceManager; import android.preference.PreferenceScreen; import android.text.TextUtils; import android.util.Pair; @@ -27,7 +22,7 @@ import org.wordpress.android.fluxc.store.SiteStore; import org.wordpress.android.util.AnalyticsUtils; import org.wordpress.android.util.AppLog; -import org.wordpress.android.util.LanguageUtils; +import org.wordpress.android.util.LocaleManager; import org.wordpress.android.util.WPMediaUtils; import org.wordpress.android.util.WPPrefUtils; @@ -39,7 +34,6 @@ public class AppSettingsFragment extends PreferenceFragment implements OnPreferenceClickListener, Preference.OnPreferenceChangeListener { - public static final String LANGUAGE_PREF_KEY = "language-pref"; public static final int LANGUAGE_CHANGED = 1000; private static final int IDX_LEGACY_EDITOR = 0; @@ -47,7 +41,6 @@ public class AppSettingsFragment extends PreferenceFragment private static final int IDX_AZTEC_EDITOR = 2; private DetailListPreference mLanguagePreference; - private SharedPreferences mSettings; // This Device settings private WPSwitchPreference mOptimizedImage; @@ -136,8 +129,6 @@ public boolean onPreferenceChange(Preference preference, Object newValue) { R.string.pref_key_site_video_encoder_bitrate); } - mSettings = PreferenceManager.getDefaultSharedPreferences(getActivity()); - updateEditorSettings(); } @@ -280,35 +271,17 @@ private void changeLanguage(String languageCode) { return; } - Resources res = getResources(); - Configuration conf = res.getConfiguration(); - // will return conf.locale if conf is non-null, or Locale.getDefault() - Locale currentLocale = LanguageUtils.getCurrentDeviceLanguage(WordPress.getContext()); - Locale newLocale = WPPrefUtils.languageLocale(languageCode); - - if (currentLocale.toString().equals(newLocale.getDisplayLanguage())) { + if (LocaleManager.isSameLanguage(languageCode)) { return; } - if (Locale.getDefault().toString().equals(newLocale.toString())) { - // remove custom locale key when original device locale is selected - mSettings.edit().remove(LANGUAGE_PREF_KEY).apply(); - } else { - mSettings.edit().putString(LANGUAGE_PREF_KEY, newLocale.toString()).apply(); - } + LocaleManager.setNewLocale(WordPress.getContext(), languageCode); updateLanguagePreference(languageCode); - // update configuration - conf.locale = newLocale; - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { - conf.setLayoutDirection(newLocale); - } - res.updateConfiguration(conf, res.getDisplayMetrics()); - // Track language change on Analytics because we have both the device language and app selected language // data in Tracks metadata. Map properties = new HashMap<>(); - properties.put("app_locale", conf.locale.toString()); + properties.put("app_locale", Locale.getDefault()); AnalyticsTracker.track(Stat.ACCOUNT_SETTINGS_LANGUAGE_CHANGED, properties); // Language is now part of metadata, so we need to refresh them @@ -326,11 +299,11 @@ private void updateLanguagePreference(String languageCode) { return; } - Locale languageLocale = WPPrefUtils.languageLocale(languageCode); + Locale languageLocale = LocaleManager.languageLocale(languageCode); String[] availableLocales = getResources().getStringArray(R.array.available_languages); Pair pair = - WPPrefUtils.createSortedLanguageDisplayStrings(availableLocales, languageLocale); + LocaleManager.createSortedLanguageDisplayStrings(availableLocales, languageLocale); // check for a possible NPE if (pair == null) { return; @@ -341,10 +314,10 @@ private void updateLanguagePreference(String languageCode) { mLanguagePreference.setEntries(sortedEntries); mLanguagePreference.setEntryValues(sortedValues); - mLanguagePreference.setDetails(WPPrefUtils.createLanguageDetailDisplayStrings(sortedValues)); + mLanguagePreference.setDetails(LocaleManager.createLanguageDetailDisplayStrings(sortedValues)); mLanguagePreference.setValue(languageCode); - mLanguagePreference.setSummary(WPPrefUtils.getLanguageString(languageCode, languageLocale)); + mLanguagePreference.setSummary(LocaleManager.getLanguageString(languageCode, languageLocale)); mLanguagePreference.refreshAdapter(); } diff --git a/WordPress/src/main/java/org/wordpress/android/ui/prefs/BlogPreferencesActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/prefs/BlogPreferencesActivity.java index 009cb2719a31..69f3683123dd 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/prefs/BlogPreferencesActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/prefs/BlogPreferencesActivity.java @@ -2,6 +2,7 @@ import android.app.Fragment; import android.app.FragmentManager; +import android.content.Context; import android.os.Bundle; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; @@ -19,6 +20,7 @@ import org.wordpress.android.fluxc.store.SiteStore.OnSiteDeleted; import org.wordpress.android.fluxc.store.SiteStore.OnSiteRemoved; import org.wordpress.android.networking.ConnectionChangeReceiver; +import org.wordpress.android.util.LocaleManager; import org.wordpress.android.util.SiteUtils; import org.wordpress.android.util.ToastUtils; @@ -38,6 +40,11 @@ public class BlogPreferencesActivity extends AppCompatActivity { @Inject SiteStore mSiteStore; @Inject Dispatcher mDispatcher; + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(LocaleManager.setLocale(newBase)); + } + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/WordPress/src/main/java/org/wordpress/android/ui/prefs/MyProfileActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/prefs/MyProfileActivity.java index 122bf0e3b0bc..a6e853833fb5 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/prefs/MyProfileActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/prefs/MyProfileActivity.java @@ -1,6 +1,7 @@ package org.wordpress.android.ui.prefs; import android.app.FragmentManager; +import android.content.Context; import android.os.Bundle; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; @@ -10,6 +11,7 @@ import org.wordpress.android.WordPress; import org.wordpress.android.fluxc.Dispatcher; import org.wordpress.android.fluxc.store.AccountStore; +import org.wordpress.android.util.LocaleManager; import javax.inject.Inject; @@ -19,6 +21,11 @@ public class MyProfileActivity extends AppCompatActivity { @Inject Dispatcher mDispatcher; @Inject AccountStore mAccountStore; + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(LocaleManager.setLocale(newBase)); + } + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/WordPress/src/main/java/org/wordpress/android/ui/prefs/SiteSettingsFragment.java b/WordPress/src/main/java/org/wordpress/android/ui/prefs/SiteSettingsFragment.java index 006bab6c0b9a..d85189b92788 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/prefs/SiteSettingsFragment.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/prefs/SiteSettingsFragment.java @@ -66,6 +66,7 @@ import org.wordpress.android.util.AppLog; import org.wordpress.android.util.HelpshiftHelper; import org.wordpress.android.util.HtmlUtils; +import org.wordpress.android.util.LocaleManager; import org.wordpress.android.util.NetworkUtils; import org.wordpress.android.util.SiteUtils; import org.wordpress.android.util.StringUtils; @@ -1371,7 +1372,7 @@ private void changeLanguageValue(String newValue) { if (TextUtils.isEmpty(mLanguagePref.getSummary()) || !newValue.equals(mLanguagePref.getValue())) { mLanguagePref.setValue(newValue); - String summary = WPPrefUtils.getLanguageString(newValue, WPPrefUtils.languageLocale(newValue)); + String summary = LocaleManager.getLanguageString(newValue, LocaleManager.languageLocale(newValue)); mLanguagePref.setSummary(summary); mLanguagePref.refreshAdapter(); } @@ -1382,15 +1383,15 @@ private void sortLanguages() { return; } - Pair pair = WPPrefUtils - .createSortedLanguageDisplayStrings(mLanguagePref.getEntryValues(), WPPrefUtils.languageLocale(null)); + Pair pair = LocaleManager + .createSortedLanguageDisplayStrings(mLanguagePref.getEntryValues(), LocaleManager.languageLocale(null)); if (pair != null) { String[] sortedEntries = pair.first; String[] sortedValues = pair.second; mLanguagePref.setEntries(sortedEntries); mLanguagePref.setEntryValues(sortedValues); - mLanguagePref.setDetails(WPPrefUtils.createLanguageDetailDisplayStrings(sortedValues)); + mLanguagePref.setDetails(LocaleManager.createLanguageDetailDisplayStrings(sortedValues)); } } diff --git a/WordPress/src/main/java/org/wordpress/android/ui/prefs/SiteSettingsInterface.java b/WordPress/src/main/java/org/wordpress/android/ui/prefs/SiteSettingsInterface.java index 6887e51b6f16..8a22062469b5 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/prefs/SiteSettingsInterface.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/prefs/SiteSettingsInterface.java @@ -24,9 +24,9 @@ import org.wordpress.android.models.JetpackSettingsModel; import org.wordpress.android.models.SiteSettingsModel; import org.wordpress.android.util.LanguageUtils; +import org.wordpress.android.util.LocaleManager; import org.wordpress.android.util.SiteUtils; import org.wordpress.android.util.StringUtils; -import org.wordpress.android.util.WPPrefUtils; import java.util.ArrayList; import java.util.HashMap; @@ -218,7 +218,7 @@ protected SiteSettingsInterface(Context host, SiteModel site, SiteSettingsListen mRemoteSettings = new SiteSettingsModel(); mJpSettings = new JetpackSettingsModel(); mRemoteJpSettings = new JetpackSettingsModel(); - mLanguageCodes = WPPrefUtils.generateLanguageMap(host); + mLanguageCodes = LocaleManager.generateLanguageMap(host); } @Override diff --git a/WordPress/src/main/java/org/wordpress/android/ui/prefs/notifications/NotificationsSettingsActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/prefs/notifications/NotificationsSettingsActivity.java index 54da64616d72..dce9be5ead13 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/prefs/notifications/NotificationsSettingsActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/prefs/notifications/NotificationsSettingsActivity.java @@ -1,6 +1,7 @@ package org.wordpress.android.ui.prefs.notifications; import android.app.FragmentManager; +import android.content.Context; import android.content.SharedPreferences; import android.os.Bundle; import android.support.v7.app.ActionBar; @@ -19,6 +20,7 @@ import org.wordpress.android.R; import org.wordpress.android.analytics.AnalyticsTracker; import org.wordpress.android.ui.notifications.NotificationEvents; +import org.wordpress.android.util.LocaleManager; import de.greenrobot.event.EventBus; @@ -31,6 +33,11 @@ public class NotificationsSettingsActivity extends AppCompatActivity { protected SwitchCompat mMasterSwitch; protected Toolbar mToolbarSwitch; + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(LocaleManager.setLocale(newBase)); + } + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/WordPress/src/main/java/org/wordpress/android/ui/publicize/PublicizeListActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/publicize/PublicizeListActivity.java index 0f7db7646d98..d08de05d968f 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/publicize/PublicizeListActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/publicize/PublicizeListActivity.java @@ -4,6 +4,7 @@ import android.app.FragmentManager; import android.app.FragmentTransaction; import android.app.ProgressDialog; +import android.content.Context; import android.content.DialogInterface; import android.os.Bundle; import android.support.v7.app.ActionBar; @@ -21,6 +22,7 @@ import org.wordpress.android.models.PublicizeService; import org.wordpress.android.ui.publicize.adapters.PublicizeServiceAdapter; import org.wordpress.android.ui.publicize.services.PublicizeUpdateService; +import org.wordpress.android.util.LocaleManager; import org.wordpress.android.util.ToastUtils; import javax.inject.Inject; @@ -37,6 +39,11 @@ public class PublicizeListActivity extends AppCompatActivity @Inject SiteStore mSiteStore; + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(LocaleManager.setLocale(newBase)); + } + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderCommentListActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderCommentListActivity.java index 5e49e8a94cf1..214b60c5c724 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderCommentListActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderCommentListActivity.java @@ -1,6 +1,7 @@ package org.wordpress.android.ui.reader; import android.app.Activity; +import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.support.annotation.NonNull; @@ -45,6 +46,7 @@ import org.wordpress.android.util.AppLog.T; import org.wordpress.android.util.DisplayUtils; import org.wordpress.android.util.EditTextUtils; +import org.wordpress.android.util.LocaleManager; import org.wordpress.android.util.NetworkUtils; import org.wordpress.android.util.ToastUtils; import org.wordpress.android.util.WPActivityUtils; @@ -92,6 +94,11 @@ public class ReaderCommentListActivity extends AppCompatActivity { @Inject AccountStore mAccountStore; + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(LocaleManager.setLocale(newBase)); + } + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPhotoViewerActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPhotoViewerActivity.java index 684259792e6c..b87db2d85489 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPhotoViewerActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPhotoViewerActivity.java @@ -2,6 +2,7 @@ import android.app.Fragment; import android.app.FragmentManager; +import android.content.Context; import android.os.Bundle; import android.os.Parcelable; import android.support.annotation.NonNull; @@ -18,6 +19,7 @@ import org.wordpress.android.ui.reader.views.ReaderPhotoView.PhotoViewListener; import org.wordpress.android.util.AniUtils; import org.wordpress.android.util.AppLog; +import org.wordpress.android.util.LocaleManager; import org.wordpress.android.widgets.WPViewPager; import org.wordpress.android.widgets.WPViewPagerTransformer; import org.wordpress.android.widgets.WPViewPagerTransformer.TransformType; @@ -37,6 +39,11 @@ public class ReaderPhotoViewerActivity extends AppCompatActivity private TextView mTxtTitle; private boolean mIsTitleVisible; + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(LocaleManager.setLocale(newBase)); + } + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListActivity.java index 76583ce3ce35..5d6f38fa6ce6 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostListActivity.java @@ -1,6 +1,7 @@ package org.wordpress.android.ui.reader; import android.app.Fragment; +import android.content.Context; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.design.widget.AppBarLayout; @@ -15,14 +16,19 @@ import org.wordpress.android.datasets.ReaderBlogTable; import org.wordpress.android.models.ReaderTag; import org.wordpress.android.ui.reader.ReaderTypes.ReaderPostListType; +import org.wordpress.android.util.LocaleManager; /* * serves as the host for ReaderPostListFragment when showing blog preview & tag preview */ - public class ReaderPostListActivity extends AppCompatActivity { private ReaderPostListType mPostListType; + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(LocaleManager.setLocale(newBase)); + } + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostPagerActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostPagerActivity.java index 786ec9b6f6ab..57f8121169ec 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostPagerActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderPostPagerActivity.java @@ -3,6 +3,7 @@ import android.app.Activity; import android.app.Fragment; import android.app.FragmentManager; +import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.Bundle; @@ -40,6 +41,7 @@ import org.wordpress.android.util.AnalyticsUtils; import org.wordpress.android.util.AniUtils; import org.wordpress.android.util.AppLog; +import org.wordpress.android.util.LocaleManager; import org.wordpress.android.util.NetworkUtils; import org.wordpress.android.util.ToastUtils; import org.wordpress.android.widgets.WPSwipeSnackbar; @@ -117,6 +119,11 @@ public enum DirectOperation { @Inject SiteStore mSiteStore; + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(LocaleManager.setLocale(newBase)); + } + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderSubsActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderSubsActivity.java index 782b2a07f182..819b13439c15 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderSubsActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderSubsActivity.java @@ -2,6 +2,7 @@ import android.app.Fragment; import android.app.FragmentManager; +import android.content.Context; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.design.widget.TabLayout; @@ -42,6 +43,7 @@ import org.wordpress.android.util.AppLog; import org.wordpress.android.util.DisplayUtils; import org.wordpress.android.util.EditTextUtils; +import org.wordpress.android.util.LocaleManager; import org.wordpress.android.util.NetworkUtils; import org.wordpress.android.util.ToastUtils; import org.wordpress.android.util.UrlUtils; @@ -75,6 +77,11 @@ public class ReaderSubsActivity extends AppCompatActivity private static final int TAB_IDX_FOLLOWED_BLOGS = 1; private static final int TAB_IDX_RECOMMENDED_BLOGS = 2; + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(LocaleManager.setLocale(newBase)); + } + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderUserListActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderUserListActivity.java index 0e8b6558b549..1b2a76ae3f45 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderUserListActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderUserListActivity.java @@ -1,5 +1,6 @@ package org.wordpress.android.ui.reader; +import android.content.Context; import android.os.Bundle; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; @@ -16,6 +17,7 @@ import org.wordpress.android.ui.reader.utils.ReaderUtils; import org.wordpress.android.ui.reader.views.ReaderRecyclerView; import org.wordpress.android.util.DisplayUtils; +import org.wordpress.android.util.LocaleManager; import org.wordpress.android.widgets.RecyclerItemDecoration; /* @@ -26,6 +28,11 @@ public class ReaderUserListActivity extends AppCompatActivity { private ReaderUserAdapter mAdapter; private int mRestorePosition; + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(LocaleManager.setLocale(newBase)); + } + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderVideoViewerActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderVideoViewerActivity.java index a643ea0df8f2..ddff87d20a69 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderVideoViewerActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/reader/ReaderVideoViewerActivity.java @@ -1,5 +1,6 @@ package org.wordpress.android.ui.reader; +import android.content.Context; import android.graphics.Color; import android.os.Bundle; import android.support.annotation.NonNull; @@ -11,16 +12,21 @@ import org.wordpress.android.R; import org.wordpress.android.WordPress; +import org.wordpress.android.util.LocaleManager; /** * Full screen landscape video player for the reader */ - public class ReaderVideoViewerActivity extends AppCompatActivity { private String mVideoUrl; private WebView mWebView; private ProgressBar mProgress; + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(LocaleManager.setLocale(newBase)); + } + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/utils/ReaderUtils.java b/WordPress/src/main/java/org/wordpress/android/ui/reader/utils/ReaderUtils.java index 976c755c761e..1fc19212df31 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/reader/utils/ReaderUtils.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/reader/utils/ReaderUtils.java @@ -15,6 +15,7 @@ import org.wordpress.android.models.ReaderTag; import org.wordpress.android.models.ReaderTagType; import org.wordpress.android.util.FormatUtils; +import org.wordpress.android.util.LocaleManager; import org.wordpress.android.util.PhotonUtils; import org.wordpress.android.util.StringUtils; import org.wordpress.android.util.UrlUtils; @@ -92,14 +93,15 @@ public static String getLongLikeLabelText(Context context, int numLikes, boolean return context.getString(R.string.reader_likes_you_and_one); default: String youAndMultiLikes = context.getString(R.string.reader_likes_you_and_multi); - return String.format(youAndMultiLikes, numLikes - 1); + return String.format( + LocaleManager.getSafeLocale(context), youAndMultiLikes, numLikes - 1); } } else { if (numLikes == 1) { return context.getString(R.string.reader_likes_one); } else { String likes = context.getString(R.string.reader_likes_multi); - return String.format(likes, numLikes); + return String.format(LocaleManager.getSafeLocale(context), likes, numLikes); } } } diff --git a/WordPress/src/main/java/org/wordpress/android/ui/reader/views/ReaderSiteHeaderView.java b/WordPress/src/main/java/org/wordpress/android/ui/reader/views/ReaderSiteHeaderView.java index a8626dda35a2..98d1bf0678df 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/reader/views/ReaderSiteHeaderView.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/reader/views/ReaderSiteHeaderView.java @@ -14,12 +14,11 @@ import org.wordpress.android.models.ReaderBlog; import org.wordpress.android.ui.reader.actions.ReaderActions; import org.wordpress.android.ui.reader.actions.ReaderBlogActions; +import org.wordpress.android.util.LocaleManager; import org.wordpress.android.util.NetworkUtils; import org.wordpress.android.util.ToastUtils; import org.wordpress.android.util.UrlUtils; -import java.util.Locale; - import javax.inject.Inject; /** @@ -130,15 +129,10 @@ private void showBlogInfo(ReaderBlog blogInfo) { txtDescription.setVisibility(View.GONE); } - try { - txtFollowCount.setText( - String.format(getContext().getString(R.string.reader_label_follow_count), blogInfo.numSubscribers)); - } catch (ArithmeticException exception) { - // See https://github.com/wordpress-mobile/WordPress-Android/issues/5568 - // In case of a "ArithmeticException: divide by zero", force the use of "US" locale to format the string. - txtFollowCount.setText(String.format(Locale.US, getContext().getString(R.string.reader_label_follow_count), - blogInfo.numSubscribers)); - } + txtFollowCount.setText(String.format( + LocaleManager.getSafeLocale(getContext()), + getContext().getString(R.string.reader_label_follow_count), + blogInfo.numSubscribers)); if (!mAccountStore.hasAccessToken()) { mFollowButton.setVisibility(View.GONE); diff --git a/WordPress/src/main/java/org/wordpress/android/ui/stats/StatsActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/stats/StatsActivity.java index 3d408a915f8a..08cd49b03310 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/stats/StatsActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/stats/StatsActivity.java @@ -39,6 +39,7 @@ import org.wordpress.android.util.AnalyticsUtils; import org.wordpress.android.util.AppLog; import org.wordpress.android.util.AppLog.T; +import org.wordpress.android.util.LocaleManager; import org.wordpress.android.util.NetworkUtils; import org.wordpress.android.util.RateLimitedTask; import org.wordpress.android.util.SiteUtils; @@ -100,6 +101,10 @@ public enum StatsLaunchedFrom { StatsVisitorsAndViewsFragment.OverviewLabel.VIEWS; private boolean mThereWasAnErrorLoadingStats = false; + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(LocaleManager.setLocale(newBase)); + } @Override public void onCreate(Bundle savedInstanceState) { diff --git a/WordPress/src/main/java/org/wordpress/android/ui/stats/StatsConnectJetpackActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/stats/StatsConnectJetpackActivity.java index cd9ebf4cffae..f23d75e6a17b 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/stats/StatsConnectJetpackActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/stats/StatsConnectJetpackActivity.java @@ -1,5 +1,6 @@ package org.wordpress.android.ui.stats; +import android.content.Context; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.ActionBar; @@ -15,6 +16,7 @@ import org.wordpress.android.fluxc.model.SiteModel; import org.wordpress.android.fluxc.store.AccountStore; import org.wordpress.android.ui.JetpackConnectionWebViewActivity; +import org.wordpress.android.util.LocaleManager; import javax.inject.Inject; @@ -25,10 +27,14 @@ * An activity that shows when user tries to open Stats without Jetpack connected. * It offers a link to the Jetpack connection flow. */ - public class StatsConnectJetpackActivity extends AppCompatActivity { @Inject AccountStore mAccountStore; + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(LocaleManager.setLocale(newBase)); + } + @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/WordPress/src/main/java/org/wordpress/android/ui/stats/StatsSingleItemDetailsActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/stats/StatsSingleItemDetailsActivity.java index 7e2c7a1a374e..d4183530b3f4 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/stats/StatsSingleItemDetailsActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/stats/StatsSingleItemDetailsActivity.java @@ -36,6 +36,7 @@ import org.wordpress.android.util.AppLog; import org.wordpress.android.util.DisplayUtils; import org.wordpress.android.util.FormatUtils; +import org.wordpress.android.util.LocaleManager; import org.wordpress.android.util.NetworkUtils; import org.wordpress.android.util.ToastUtils; import org.wordpress.android.util.helpers.SwipeToRefreshHelper; @@ -48,7 +49,6 @@ import static org.wordpress.android.util.WPSwipeToRefreshHelper.buildSwipeToRefreshHelper; - /** * Single item details activity. */ @@ -102,6 +102,10 @@ public class StatsSingleItemDetailsActivity extends AppCompatActivity private static final String ARG_AVERAGES_EXPANDED_ROWS = "ARG_AVERAGES_EXPANDED_ROWS"; private static final String ARG_RECENT_EXPANDED_ROWS = "ARG_RECENT_EXPANDED_ROWS"; + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(LocaleManager.setLocale(newBase)); + } @Override public void onCreate(Bundle savedInstanceState) { diff --git a/WordPress/src/main/java/org/wordpress/android/ui/stats/StatsViewAllActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/stats/StatsViewAllActivity.java index a70bcd04825b..bcf9afc3fd72 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/stats/StatsViewAllActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/stats/StatsViewAllActivity.java @@ -2,6 +2,7 @@ import android.app.FragmentManager; import android.app.FragmentTransaction; +import android.content.Context; import android.os.Bundle; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; @@ -15,6 +16,7 @@ import org.wordpress.android.analytics.AnalyticsTracker.Stat; import org.wordpress.android.ui.ActivityId; import org.wordpress.android.util.AppLog; +import org.wordpress.android.util.LocaleManager; import org.wordpress.android.util.NetworkUtils; import org.wordpress.android.util.ToastUtils; import org.wordpress.android.util.helpers.SwipeToRefreshHelper; @@ -51,6 +53,11 @@ public class StatsViewAllActivity extends AppCompatActivity { private Serializable[] mRestResponse; private int mOuterPagerSelectedButtonIndex = 0; + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(LocaleManager.setLocale(newBase)); + } + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/WordPress/src/main/java/org/wordpress/android/ui/stats/StatsWidgetConfigureActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/stats/StatsWidgetConfigureActivity.java index 45d2d4ab50c3..009f4a239758 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/stats/StatsWidgetConfigureActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/stats/StatsWidgetConfigureActivity.java @@ -20,11 +20,12 @@ import org.wordpress.android.fluxc.model.SiteModel; import org.wordpress.android.fluxc.store.AccountStore; import org.wordpress.android.fluxc.store.SiteStore; +import org.wordpress.android.ui.main.SitePickerAdapter.SiteRecord; import org.wordpress.android.util.AppLog; import org.wordpress.android.util.FluxCUtils; +import org.wordpress.android.util.LocaleManager; import org.wordpress.android.util.SiteUtils; import org.wordpress.android.util.ToastUtils; -import org.wordpress.android.ui.main.SitePickerAdapter.SiteRecord; import javax.inject.Inject; @@ -37,6 +38,11 @@ public class StatsWidgetConfigureActivity extends AppCompatActivity @Inject AccountStore mAccountStore; @Inject SiteStore mSiteStore; + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(LocaleManager.setLocale(newBase)); + } + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/WordPress/src/main/java/org/wordpress/android/ui/themes/ThemeBrowserActivity.java b/WordPress/src/main/java/org/wordpress/android/ui/themes/ThemeBrowserActivity.java index a1642ad76c55..fd6b9d967d95 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/themes/ThemeBrowserActivity.java +++ b/WordPress/src/main/java/org/wordpress/android/ui/themes/ThemeBrowserActivity.java @@ -2,6 +2,7 @@ import android.app.AlertDialog; import android.app.FragmentManager; +import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; @@ -30,6 +31,7 @@ import org.wordpress.android.util.AnalyticsUtils; import org.wordpress.android.util.AppLog; import org.wordpress.android.util.AppLog.T; +import org.wordpress.android.util.LocaleManager; import org.wordpress.android.util.NetworkUtils; import org.wordpress.android.util.ToastUtils; @@ -58,6 +60,11 @@ public static boolean isAccessible(SiteModel site) { @Inject ThemeStore mThemeStore; @Inject Dispatcher mDispatcher; + @Override + protected void attachBaseContext(Context newBase) { + super.attachBaseContext(LocaleManager.setLocale(newBase)); + } + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/WordPress/src/main/java/org/wordpress/android/util/LocaleManager.java b/WordPress/src/main/java/org/wordpress/android/util/LocaleManager.java new file mode 100644 index 000000000000..20dac9b93581 --- /dev/null +++ b/WordPress/src/main/java/org/wordpress/android/util/LocaleManager.java @@ -0,0 +1,263 @@ +package org.wordpress.android.util; + +import android.annotation.SuppressLint; +import android.content.Context; +import android.content.SharedPreferences; +import android.content.res.Configuration; +import android.content.res.Resources; +import android.os.Build; +import android.preference.PreferenceManager; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.text.TextUtils; +import android.util.Pair; + +import java.text.Collator; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; + +/** + * Helper class for working with localized strings. Ensures updates to the users + * selected language is properly saved and resources appropriately updated for the + * android version. + */ +public class LocaleManager { + /** + * Length of a {@link String} (representing a language code) when there is no region included. + * For example: "en" contains no region, "en_US" contains a region (US) + *

+ * Used to parse a language code {@link String} when creating a {@link Locale}. + */ + private static final int NO_REGION_LANG_CODE_LEN = 2; + + /** + * Index of a language code {@link String} where the region code begins. The language code + * format is cc_rr, where cc is the country code (e.g. en, es, az) and rr is the region code + * (e.g. us, au, gb). + */ + private static final int REGION_SUBSTRING_INDEX = 3; + + /** + * Key used for saving the language selection to shared preferences. + */ + private static final String LANGUAGE_KEY = "language-pref"; + + /** + * Activate the locale associated with the provided context. + * @param context The current context. + */ + public static Context setLocale(Context context) { + return updateResources(context, getLanguage(context)); + } + + /** + * Change the active locale to the language provided. Save the updated language + * settings to sharedPreferences. + * @param context The current context + * @param language The 2-letter language code (example "en") to switch to + */ + public static void setNewLocale(Context context, String language) { + if (isSameLanguage(language)) { + return; + } + saveLanguageToPref(context, language); + updateResources(context, language); + } + + /** + * Compare the language for the current context with another language. + * @param language The language to compare + * @return True if the languages are the same, else false + */ + public static boolean isSameLanguage(@NonNull String language) { + Locale newLocale = languageLocale(language); + return Locale.getDefault().getLanguage().equals(newLocale.getLanguage()); + } + + /** + * If the user has selected a language other than the device default, return that + * language code, else just return the device default language code. + * @return The 2-letter language code (example "en") + */ + private static String getLanguage(Context context) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + if (prefs.contains(LANGUAGE_KEY)) { + return prefs.getString(LANGUAGE_KEY, ""); + } + return LanguageUtils.getCurrentDeviceLanguageCode(context); + } + + /** + * Save the updated language to SharedPreferences. + * Use commit() instead of apply() to ensure the language preference is saved instantly + * as the app may be restarted immediately. + * @param context The current context + * @param language The 2-letter language code (example "en") + */ + @SuppressLint("ApplySharedPref") + private static void saveLanguageToPref(Context context, String language) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + prefs.edit().putString(LANGUAGE_KEY, language).commit(); + } + + /** + * Update resources for the current session. + * + * @param context The current active context + * @param language The 2-letter language code (example "en") + * @return The modified context containing the updated localized resources + */ + private static Context updateResources(Context context, String language) { + Locale locale = new Locale(language); + Locale.setDefault(locale); + + Resources res = context.getResources(); + Configuration config = new Configuration(res.getConfiguration()); + + // NOTE: Earlier versions of Android require both of these to be set, otherwise + // RTL may not be implemented properly. + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { + config.setLocale(locale); + context = context.createConfigurationContext(config); + } + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N_MR1) { + config.locale = locale; + res.updateConfiguration(config, res.getDisplayMetrics()); + } + return context; + } + + /** + * Method gets around a bug in the java.util.Formatter for API 7.x as detailed here + * [https://bugs.openjdk.java.net/browse/JDK-8167567]. Any strings that contain + * locale-specific grouping separators should use: + * + * String.format(LocaleManager.getSafeLocale(context), baseString, val) + * + * + * An example of a string that contains locale-specific grouping separators: + * + * %,d likes + * + */ + public static Locale getSafeLocale(@Nullable Context context) { + Locale baseLocale; + if (context == null) { + baseLocale = Locale.getDefault(); + } else { + Configuration config = context.getResources().getConfiguration(); + baseLocale = Build.VERSION.SDK_INT >= 24 ? config.getLocales().get(0) : config.locale; + } + + if (Build.VERSION.SDK_INT >= 24) { + return languageLocale(baseLocale.getLanguage()); + } else { + return baseLocale; + } + } + + /** + * Gets a locale for the given language code. + * @param languageCode The 2-letter language code (example "en"). If null or empty will return + * the current default locale. + */ + public static Locale languageLocale(@Nullable String languageCode) { + if (TextUtils.isEmpty(languageCode)) { + return Locale.getDefault(); + } + + if (languageCode.length() > NO_REGION_LANG_CODE_LEN) { + return new Locale(languageCode.substring(0, NO_REGION_LANG_CODE_LEN), + languageCode.substring(REGION_SUBSTRING_INDEX)); + } + + return new Locale(languageCode); + } + + /** + * Creates a map from language codes to WordPress language IDs. + */ + public static Map generateLanguageMap(Context context) { + String[] languageIds = context.getResources().getStringArray(org.wordpress.android.R.array.lang_ids); + String[] languageCodes = context.getResources().getStringArray(org.wordpress.android.R.array.language_codes); + + Map languageMap = new HashMap<>(); + for (int i = 0; i < languageIds.length && i < languageCodes.length; ++i) { + languageMap.put(languageCodes[i], languageIds[i]); + } + + return languageMap; + } + + /** + * Generates display strings for given language codes. Used as entries in language preference. + */ + @Nullable + public static Pair createSortedLanguageDisplayStrings(CharSequence[] languageCodes, + Locale locale) { + if (languageCodes == null || languageCodes.length < 1) { + return null; + } + + ArrayList entryStrings = new ArrayList<>(languageCodes.length); + for (int i = 0; i < languageCodes.length; ++i) { + // "__" is used to sort the language code with the display string so both arrays are sorted at the same time + entryStrings.add(i, StringUtils.capitalize( + getLanguageString(languageCodes[i].toString(), locale)) + "__" + languageCodes[i]); + } + + Collections.sort(entryStrings, Collator.getInstance(locale)); + + String[] sortedEntries = new String[languageCodes.length]; + String[] sortedValues = new String[languageCodes.length]; + + for (int i = 0; i < entryStrings.size(); ++i) { + // now, we can split the sorted array to extract the display string and the language code + String[] split = entryStrings.get(i).split("__"); + sortedEntries[i] = split[0]; + sortedValues[i] = split[1]; + } + + return new Pair<>(sortedEntries, sortedValues); + } + + /** + * Generates detail display strings in the currently selected locale. Used as detail text + * in language preference dialog. + */ + @Nullable + public static String[] createLanguageDetailDisplayStrings(CharSequence[] languageCodes) { + if (languageCodes == null || languageCodes.length < 1) { + return null; + } + + String[] detailStrings = new String[languageCodes.length]; + for (int i = 0; i < languageCodes.length; ++i) { + detailStrings[i] = StringUtils.capitalize(getLanguageString( + languageCodes[i].toString(), languageLocale(languageCodes[i].toString()))); + } + + return detailStrings; + } + + /** + * Return a non-null display string for a given language code. + */ + public static String getLanguageString(String languageCode, Locale displayLocale) { + if (languageCode == null || languageCode.length() < 2 || languageCode.length() > 6) { + return ""; + } + + Locale languageLocale = languageLocale(languageCode); + String displayLanguage = StringUtils.capitalize(languageLocale.getDisplayLanguage(displayLocale)); + String displayCountry = languageLocale.getDisplayCountry(displayLocale); + + if (!TextUtils.isEmpty(displayCountry)) { + return displayLanguage + " (" + displayCountry + ")"; + } + return displayLanguage; + } +} diff --git a/WordPress/src/main/java/org/wordpress/android/util/WPActivityUtils.java b/WordPress/src/main/java/org/wordpress/android/util/WPActivityUtils.java index 328bc44a5333..f73163009597 100644 --- a/WordPress/src/main/java/org/wordpress/android/util/WPActivityUtils.java +++ b/WordPress/src/main/java/org/wordpress/android/util/WPActivityUtils.java @@ -6,17 +6,12 @@ import android.content.ComponentName; import android.content.Context; import android.content.Intent; -import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; -import android.content.res.Configuration; -import android.content.res.Resources; import android.os.Build; -import android.preference.PreferenceManager; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; -import android.text.TextUtils; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -25,10 +20,8 @@ import android.widget.TextView; import org.wordpress.android.R; -import org.wordpress.android.ui.prefs.AppSettingsFragment; import java.util.List; -import java.util.Locale; public class WPActivityUtils { // Hack! PreferenceScreens don't show the toolbar, so we'll manually add one @@ -101,33 +94,6 @@ public static void setStatusBarColor(Window window, int color) { } } - public static void applyLocale(Context context) { - SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); - - if (sharedPreferences.contains(AppSettingsFragment.LANGUAGE_PREF_KEY)) { - Locale contextLocale = context.getResources().getConfiguration().locale; - String contextLanguage = contextLocale.getLanguage(); - contextLanguage = LanguageUtils.patchDeviceLanguageCode(contextLanguage); - String contextCountry = contextLocale.getCountry(); - String locale = sharedPreferences.getString(AppSettingsFragment.LANGUAGE_PREF_KEY, ""); - - if (!TextUtils.isEmpty(contextCountry)) { - contextLanguage += "_" + contextCountry; - } - - if (!locale.equals(contextLanguage)) { - Resources resources = context.getResources(); - Configuration conf = resources.getConfiguration(); - Locale newLocale = WPPrefUtils.languageLocale(locale); - conf.locale = newLocale; - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { - conf.setLayoutDirection(newLocale); - } - resources.updateConfiguration(conf, resources.getDisplayMetrics()); - } - } - } - public static Context getThemedContext(Context context) { if (context instanceof AppCompatActivity) { ActionBar actionBar = ((AppCompatActivity) context).getSupportActionBar(); diff --git a/WordPress/src/main/java/org/wordpress/android/util/WPPrefUtils.java b/WordPress/src/main/java/org/wordpress/android/util/WPPrefUtils.java index 2581921d123f..29c8496785f4 100644 --- a/WordPress/src/main/java/org/wordpress/android/util/WPPrefUtils.java +++ b/WordPress/src/main/java/org/wordpress/android/util/WPPrefUtils.java @@ -1,47 +1,20 @@ package org.wordpress.android.util; -import android.content.Context; import android.preference.Preference; import android.preference.PreferenceCategory; import android.preference.PreferenceFragment; import android.preference.PreferenceGroup; -import android.support.annotation.Nullable; -import android.text.TextUtils; -import android.util.Pair; import android.util.TypedValue; import android.widget.EditText; import android.widget.TextView; import org.wordpress.android.R; -import org.wordpress.android.WordPress; - -import java.text.Collator; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.Locale; -import java.util.Map; /** * Design guidelines for Calypso-styled Site Settings (and likely other screens) */ public class WPPrefUtils { - /** - * Length of a {@link String} (representing a language code) when there is no region included. - * For example: "en" contains no region, "en_US" contains a region (US) - *

- * Used to parse a language code {@link String} when creating a {@link Locale}. - */ - private static final int NO_REGION_LANG_CODE_LEN = 2; - - /** - * Index of a language code {@link String} where the region code begins. The language code - * format is cc_rr, where cc is the country code (e.g. en, es, az) and rr is the region code - * (e.g. us, au, gb). - */ - private static final int REGION_SUBSTRING_INDEX = 3; - /** * Gets a preference and sets the {@link android.preference.Preference.OnPreferenceChangeListener}. */ @@ -187,104 +160,4 @@ public static void setTextViewAttributes(TextView textView, int size, int colorR textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, size); textView.setTextColor(textView.getResources().getColor(colorRes)); } - - /** - * Gets a locale for the given language code. - */ - public static Locale languageLocale(String languageCode) { - if (TextUtils.isEmpty(languageCode)) { - return LanguageUtils.getCurrentDeviceLanguage(WordPress.getContext()); - } - - if (languageCode.length() > NO_REGION_LANG_CODE_LEN) { - return new Locale(languageCode.substring(0, NO_REGION_LANG_CODE_LEN), - languageCode.substring(REGION_SUBSTRING_INDEX)); - } - - return new Locale(languageCode); - } - - /** - * Creates a map from language codes to WordPress language IDs. - */ - public static Map generateLanguageMap(Context context) { - String[] languageIds = context.getResources().getStringArray(R.array.lang_ids); - String[] languageCodes = context.getResources().getStringArray(R.array.language_codes); - - Map languageMap = new HashMap<>(); - for (int i = 0; i < languageIds.length && i < languageCodes.length; ++i) { - languageMap.put(languageCodes[i], languageIds[i]); - } - - return languageMap; - } - - /** - * Generates display strings for given language codes. Used as entries in language preference. - */ - @Nullable - public static Pair createSortedLanguageDisplayStrings(CharSequence[] languageCodes, - Locale locale) { - if (languageCodes == null || languageCodes.length < 1) { - return null; - } - - ArrayList entryStrings = new ArrayList<>(languageCodes.length); - for (int i = 0; i < languageCodes.length; ++i) { - // "__" is used to sort the language code with the display string so both arrays are sorted at the same time - entryStrings.add(i, StringUtils.capitalize( - getLanguageString(languageCodes[i].toString(), locale)) + "__" + languageCodes[i]); - } - - Collections.sort(entryStrings, Collator.getInstance(locale)); - - String[] sortedEntries = new String[languageCodes.length]; - String[] sortedValues = new String[languageCodes.length]; - - for (int i = 0; i < entryStrings.size(); ++i) { - // now, we can split the sorted array to extract the display string and the language code - String[] split = entryStrings.get(i).split("__"); - sortedEntries[i] = split[0]; - sortedValues[i] = split[1]; - } - - return new Pair<>(sortedEntries, sortedValues); - } - - /** - * Generates detail display strings in the currently selected locale. Used as detail text - * in language preference dialog. - */ - @Nullable - public static String[] createLanguageDetailDisplayStrings(CharSequence[] languageCodes) { - if (languageCodes == null || languageCodes.length < 1) { - return null; - } - - String[] detailStrings = new String[languageCodes.length]; - for (int i = 0; i < languageCodes.length; ++i) { - detailStrings[i] = StringUtils.capitalize(getLanguageString( - languageCodes[i].toString(), WPPrefUtils.languageLocale(languageCodes[i].toString()))); - } - - return detailStrings; - } - - /** - * Return a non-null display string for a given language code. - */ - public static String getLanguageString(String languageCode, Locale displayLocale) { - if (languageCode == null || languageCode.length() < 2 || languageCode.length() > 6) { - return ""; - } - - Locale languageLocale = WPPrefUtils.languageLocale(languageCode); - String displayLanguage = StringUtils.capitalize(languageLocale.getDisplayLanguage(displayLocale)); - String displayCountry = languageLocale.getDisplayCountry(displayLocale); - - if (!TextUtils.isEmpty(displayCountry)) { - return displayLanguage + " (" + displayCountry + ")"; - } - return displayLanguage; - } } diff --git a/libs/utils/WordPressUtils/src/main/java/org/wordpress/android/util/LanguageUtils.java b/libs/utils/WordPressUtils/src/main/java/org/wordpress/android/util/LanguageUtils.java index c043b2f0d97c..586c1e18b553 100644 --- a/libs/utils/WordPressUtils/src/main/java/org/wordpress/android/util/LanguageUtils.java +++ b/libs/utils/WordPressUtils/src/main/java/org/wordpress/android/util/LanguageUtils.java @@ -1,6 +1,7 @@ package org.wordpress.android.util; import android.content.Context; +import android.support.annotation.Nullable; import java.util.Locale; @@ -8,17 +9,35 @@ * 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; + /** + * @deprecated Use {@link #getCurrentDeviceLanguage()}. As of API 25, setting the locale by updating the + * configuration on the resources object was deprecated, so this method stopped working for newer versions + * of Android. The current active locale should always be set in {@link Locale#getDefault()}. When manually + * setting the active locale, the developer should set it in {@link Locale#setDefault(Locale)}. + */ + @SuppressWarnings("DeprecatedIsStillUsed") + @Deprecated + public static Locale getCurrentDeviceLanguage(@Nullable Context context) { + return getCurrentDeviceLanguage(); + } + + @SuppressWarnings("WeakerAccess") + public static Locale getCurrentDeviceLanguage() { + return Locale.getDefault(); + } + + /** + * @deprecated Use {@link #getCurrentDeviceLanguageCode()}. + */ + @SuppressWarnings("WeakerAccess,DeprecatedIsStillUsed") + @Deprecated + public static String getCurrentDeviceLanguageCode(@Nullable Context context) { + return getCurrentDeviceLanguageCode(); } - public static String getCurrentDeviceLanguageCode(Context context) { - String deviceLanguageCode = getCurrentDeviceLanguage(context).toString(); - return deviceLanguageCode; + @SuppressWarnings("WeakerAccess") + public static String getCurrentDeviceLanguageCode() { + return getCurrentDeviceLanguage().toString(); } public static String getPatchedCurrentDeviceLanguage(Context context) { @@ -28,6 +47,7 @@ public static String getPatchedCurrentDeviceLanguage(Context context) { /** * Patches a deviceLanguageCode if any of deprecated values iw, id, or yi */ + @SuppressWarnings("WeakerAccess") public static String patchDeviceLanguageCode(String deviceLanguageCode) { String patchedCode = deviceLanguageCode; /*