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 3e687e7db8b4..501e055aa988 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 @@ -410,8 +410,6 @@ protected void onCreate(Bundle savedInstanceState) { // Check whether to show the visual editor PreferenceManager.setDefaultValues(this, R.xml.account_settings, false); - // AppPrefs.setAztecEditorAvailable(true); - // AppPrefs.setAztecEditorEnabled(true); mShowAztecEditor = AppPrefs.isAztecEditorEnabled(); mShowNewEditor = AppPrefs.isVisualEditorEnabled(); @@ -450,8 +448,7 @@ protected void onCreate(Bundle savedInstanceState) { } newPostSetup(); } else if (extras != null) { - // Load post passed in extras - mPost = mPostStore.getPostByLocalPostId(extras.getInt(EXTRA_POST_LOCAL_ID)); + mPost = mPostStore.getPostByLocalPostId(extras.getInt(EXTRA_POST_LOCAL_ID)); // Load post from extras if (mPost != null) { initializePostObject(); @@ -476,8 +473,7 @@ protected void onCreate(Bundle savedInstanceState) { showDialogProgress(mIsDialogProgressShown); - // if we have a remote id saved, let's first try with that, as the local Id might have changed - // after FETCH_POSTS + // if we have a remote id saved, let's first try that, as the local Id might have changed after FETCH_POSTS if (savedInstanceState.containsKey(STATE_KEY_POST_REMOTE_ID)) { mPost = mPostStore.getPostByRemotePostId(savedInstanceState.getLong(STATE_KEY_POST_REMOTE_ID), mSite); initializePostObject(); @@ -544,9 +540,8 @@ protected void onCreate(Bundle savedInstanceState) { if (mIsNewPost) { trackEditorCreatedPost(action, getIntent()); } else { - // if we are opening a Post for which an error notification exists, we need to remove - // it from the dashboard to prevent the user from tapping RETRY on a Post that is - // being currently edited + // if we are opening a Post for which an error notification exists, we need to remove it from the dashboard + // to prevent the user from tapping RETRY on a Post that is being currently edited UploadService.cancelFinalNotification(this, mPost); resetUploadingMediaToFailedIfPostHasNotMediaInProgressOrQueued(); } @@ -560,9 +555,8 @@ protected void onCreate(Bundle savedInstanceState) { mViewPager.setOffscreenPageLimit(3); mViewPager.setPagingEnabled(false); - // When swiping between different sections, select the corresponding - // tab. We can also use ActionBar.Tab#select() to do this if we have - // a reference to the Tab. + // When swiping between different sections, select the corresponding tab. We can also use ActionBar.Tab#select() + // to do this if we have a reference to the Tab. mViewPager.clearOnPageChangeListeners(); mViewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { @Override @@ -2211,7 +2205,9 @@ public Fragment getItem(int position) { if (mShowGutenbergEditor) { // Show the GB informative dialog on editing GB posts showGutenbergInformativeDialog(); - return GutenbergEditorFragment.newInstance("", "", mIsNewPost); + String languageString = LocaleManager.getLanguage(EditPostActivity.this); + String wpcomLocaleSlug = languageString.replace("_", "-").toLowerCase(Locale.ENGLISH); + return GutenbergEditorFragment.newInstance("", "", mIsNewPost, wpcomLocaleSlug); } else if (mShowAztecEditor) { return AztecEditorFragment.newInstance("", "", AppPrefs.isAztecEditorToolbarExpanded()); diff --git a/libs/editor/WordPressEditor/src/main/java/org/wordpress/android/editor/GutenbergContainerFragment.java b/libs/editor/WordPressEditor/src/main/java/org/wordpress/android/editor/GutenbergContainerFragment.java index 0f2fc01b68f0..d8e68093df73 100644 --- a/libs/editor/WordPressEditor/src/main/java/org/wordpress/android/editor/GutenbergContainerFragment.java +++ b/libs/editor/WordPressEditor/src/main/java/org/wordpress/android/editor/GutenbergContainerFragment.java @@ -14,16 +14,20 @@ public class GutenbergContainerFragment extends Fragment { public static final String TAG = "gutenberg_container_fragment_tag"; private static final String ARG_IS_NEW_POST = "param_is_new_post"; + private static final String ARG_LOCALE = "param_locale"; + private static final String ARG_TRANSLATIONS = "param_translations"; private boolean mHtmlModeEnabled; private boolean mHasReceivedAnyContent; private WPAndroidGlueCode mWPAndroidGlueCode; - public static GutenbergContainerFragment newInstance(boolean isNewPost) { + public static GutenbergContainerFragment newInstance(boolean isNewPost, String localeString, Bundle translations) { GutenbergContainerFragment fragment = new GutenbergContainerFragment(); Bundle args = new Bundle(); args.putBoolean(ARG_IS_NEW_POST, isNewPost); + args.putString(ARG_LOCALE, localeString); + args.putBundle(ARG_TRANSLATIONS, translations); fragment.setArguments(args); return fragment; } @@ -44,6 +48,8 @@ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); boolean isNewPost = getArguments() != null && getArguments().getBoolean(ARG_IS_NEW_POST); + String localeString = getArguments().getString(ARG_LOCALE); + Bundle translations = getArguments().getBundle(ARG_TRANSLATIONS); mWPAndroidGlueCode = new WPAndroidGlueCode(); mWPAndroidGlueCode.onCreate(getContext()); @@ -53,7 +59,9 @@ public void onCreate(Bundle savedInstanceState) { getActivity().getApplication(), BuildConfig.DEBUG, BuildConfig.BUILD_GUTENBERG_FROM_SOURCE, - isNewPost); + isNewPost, + localeString, + translations); // clear the content initialization flag since a new ReactRootView has been created; mHasReceivedAnyContent = false; diff --git a/libs/editor/WordPressEditor/src/main/java/org/wordpress/android/editor/GutenbergEditorFragment.java b/libs/editor/WordPressEditor/src/main/java/org/wordpress/android/editor/GutenbergEditorFragment.java index e3533641a9d6..5da912661c4c 100644 --- a/libs/editor/WordPressEditor/src/main/java/org/wordpress/android/editor/GutenbergEditorFragment.java +++ b/libs/editor/WordPressEditor/src/main/java/org/wordpress/android/editor/GutenbergEditorFragment.java @@ -6,6 +6,7 @@ import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.content.res.Configuration; +import android.content.res.Resources; import android.os.Bundle; import android.os.Handler; import android.support.annotation.NonNull; @@ -16,6 +17,7 @@ import android.support.v7.app.AppCompatActivity; import android.text.Editable; import android.text.Spanned; +import android.util.DisplayMetrics; import android.view.ContextThemeWrapper; import android.view.LayoutInflater; import android.view.Menu; @@ -41,6 +43,10 @@ import org.wordpress.mobile.WPAndroidGlue.WPAndroidGlueCode.OnMediaLibraryButtonListener; import org.wordpress.mobile.WPAndroidGlue.WPAndroidGlueCode.OnReattachQueryListener; +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Locale; import java.util.HashSet; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; @@ -50,6 +56,7 @@ public class GutenbergEditorFragment extends EditorFragmentAbstract implements IHistoryListener { private static final String KEY_HTML_MODE_ENABLED = "KEY_HTML_MODE_ENABLED"; private static final String ARG_IS_NEW_POST = "param_is_new_post"; + private static final String ARG_LOCALE_SLUG = "param_locale_slug"; private static final int CAPTURE_PHOTO_PERMISSION_REQUEST_CODE = 101; @@ -72,12 +79,14 @@ public class GutenbergEditorFragment extends EditorFragmentAbstract implements public static GutenbergEditorFragment newInstance(String title, String content, - boolean isNewPost) { + boolean isNewPost, + String localeSlug) { GutenbergEditorFragment fragment = new GutenbergEditorFragment(); Bundle args = new Bundle(); args.putString(ARG_PARAM_TITLE, title); args.putString(ARG_PARAM_CONTENT, content); args.putBoolean(ARG_IS_NEW_POST, isNewPost); + args.putString(ARG_LOCALE_SLUG, localeSlug); fragment.setArguments(args); return fragment; } @@ -94,17 +103,86 @@ private GutenbergContainerFragment getGutenbergContainerFragment() { return mRetainedGutenbergContainerFragment; } + /** + * Returns the gutenberg-mobile specific translations + * + * @return Bundle a map of "english string" => [ "current locale string" ] + */ + public Bundle getTranslations() { + Bundle translations = new Bundle(); + Locale defaultLocale = new Locale("en"); + Resources currentResources = getActivity().getApplicationContext().getResources(); + Configuration currentConfiguration = currentResources.getConfiguration(); + // if the current locale of the app is english stop here and return an empty map + if (currentConfiguration.locale.equals(defaultLocale)) { + return translations; + } + + // Let's create a Resources object for the default locale (english) to get the original values for our strings + DisplayMetrics metrics = new DisplayMetrics(); + Configuration defaultLocaleConfiguration = new Configuration(currentConfiguration); + defaultLocaleConfiguration.setLocale(defaultLocale); + getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics); + Resources defaultResources = new Resources(getActivity().getAssets(), metrics, defaultLocaleConfiguration); + + // Strings are only being translated in the WordPress package + // thus we need to get a reference of the R class for this package + // Here we assume the Application class is at the same level as the R class + // It will not work if this lib is used outside of WordPress-Android, + // in this case let's just return an empty map + Class rString; + Package mainPackage = getActivity().getApplication().getClass().getPackage(); + + if (mainPackage == null) { + return translations; + } + + try { + rString = getActivity().getApplication().getClassLoader().loadClass(mainPackage.getName() + ".R$string"); + } catch (ClassNotFoundException ex) { + return translations; + } + + for (Field stringField : rString.getDeclaredFields()) { + int resourceId; + try { + resourceId = stringField.getInt(rString); + } catch (IllegalArgumentException | IllegalAccessException iae) { + AppLog.e(T.EDITOR, iae); + continue; + } + + String fieldName = stringField.getName(); + // Filter out all strings that are not prefixed with `gutenberg_mobile_` + if (!fieldName.startsWith("gutenberg_mobile_")) { + continue; + } + + // Add the mapping english => [ translated ] to the bundle if both string are not empty + String currentResourceString = currentResources.getString(resourceId); + String defaultResourceString = defaultResources.getString(resourceId); + if (currentResourceString.length() > 0 && defaultResourceString.length() > 0) { + translations.putStringArrayList( + defaultResourceString, + new ArrayList<>(Arrays.asList(currentResourceString)) + ); + } + } + return translations; + } + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getGutenbergContainerFragment() == null) { boolean isNewPost = getArguments().getBoolean(ARG_IS_NEW_POST); + String localeSlug = getArguments().getString(ARG_LOCALE_SLUG); FragmentManager fragmentManager = getChildFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); GutenbergContainerFragment gutenbergContainerFragment = - GutenbergContainerFragment.newInstance(isNewPost); + GutenbergContainerFragment.newInstance(isNewPost, localeSlug, this.getTranslations()); gutenbergContainerFragment.setRetainInstance(true); fragmentTransaction.add(gutenbergContainerFragment, GutenbergContainerFragment.TAG); fragmentTransaction.commitNow(); diff --git a/libs/gutenberg-mobile b/libs/gutenberg-mobile index d2d8a815202f..a24a46c9d5b6 160000 --- a/libs/gutenberg-mobile +++ b/libs/gutenberg-mobile @@ -1 +1 @@ -Subproject commit d2d8a815202f3b0e495d6b560a90b877d0b9ae99 +Subproject commit a24a46c9d5b660d2c2bd0b233140fc635750fb7d